enupal / stripe

Allows customers sign up for recurring and one-time payments with Stripe, perfect for orders, donations, subscriptions, and events. Create simple payment forms in seconds easily without coding. For Craft CMS
https://enupal.com/craft-plugins/stripe-payments/
Other
33 stars 19 forks source link

Set Cancel Url for Checkout Twig Tag #377

Closed malcolmhmaclean closed 6 months ago

malcolmhmaclean commented 6 months ago

Description

Hi there, is there any way to set a cancel url (and success url) for the Checkout Twig Tag?

Steps to reproduce

I am just testing the simple/standard checkout link for now (https://docs.enupal.com/stripe-payments/pro/checkout.html) but wondered if it was possible to override the one from the admin? I need to make the payment page go back to the previous page

Malcolm

andrelopez commented 6 months ago

Hi @malcolmhmaclean You override the cancel_url via the beforeCreateSession event, additionally you can add/override any Stripe Checkout session available here

use enupal\stripe\services\Checkout;
use enupal\stripe\events\CheckoutEvent;
use enupal\stripe\Stripe;
use craft\base\Plugin;
use Craft;

class YourPlugin extends Plugin
{
    public function init()
    {
        ....
        ....

         Event::on(Checkout::class, Checkout::EVENT_BEFORE_CREATE_SESSION, function(CheckoutEvent $e) {
              $isCart = $e->isCart;
              $sessionParams = $e->sessionParams;
              //overwrite $sessionParams
              $sessionParams['cancel_url'] = 'https://yoursite.com/cancel';
              $e->sessionParams = $sessionParams;
         });
        ...
        ...        
     }
}