Events

Stripe Payments allows listen for events like: afterOrderComplete and beforeSendNotificationEmail

# afterOrderComplete

use enupal\stripe\services\Orders;
use enupal\stripe\events\OrderCompleteEvent;
use enupal\stripe\Stripe;
use craft\base\Plugin;
use Craft;

class YourPlugin extends Plugin
{
    public function init()
    {
        ...
        ...
         Event::on(Orders::class, Orders::EVENT_AFTER_ORDER_COMPLETE, function(OrderCompleteEvent $e) {
              $order = $e->order;
             // Do something
         });
        ...
        ...        
     }
}        

# beforeSendNotificationEmail

This event is fired before the notification for admin or customers is send

use enupal\stripe\services\Emails;
use enupal\stripe\events\NotificationEvent;
use enupal\stripe\Stripe;
use craft\base\Plugin;
use Craft;

class YourPlugin extends Plugin
{
    public function init()
    {
        ....
        ....
     
         Event::on(Emails::class, Emails::EVENT_BEFORE_SEND_NOTIFICATION_EMAIL, function(NotificationEvent $e) {
              $message = $e->message;
              // admin|customer        
              $type = $e->type;      
              $order= $e->order;
              $fields= $order->getFormFields();
              // Retrieve any form field
              $myField = $fields['handle'] ?? null;
             // Do something
         });
        ...
        ...        
     }
}       

# afterProcessWebhook

This event is fired after receiveย a notification event from Stripe. Please check all Stripe events types (opens new window)ย available.

You can add the next code to the main class of your plugin in the init() method. The following example listens to theย EVENT_AFTER_PROCESS_WEBHOOK and checks if the subscription is canceled and remove all user groups from the craft user.

use enupal\stripe\services\Orders;
use enupal\stripe\events\WebhookEvent;
use enupal\stripe\Stripe;
use craft\base\Plugin;
use Craft;

class YourPlugin extends Plugin
{
    public function init()
    {
        ....
        ....
                    
        Event::on(Orders::class, Orders::EVENT_AFTER_PROCESS_WEBHOOK, function(WebhookEvent $e) {
            $data  = $e->stripeData;
            $order = $e->order;
        
            if ($order){
                $user = Craft::$app->getUsers()->getUserByUsernameOrEmail($order->email);
                // Or if the order have logged in users:
                // $user = Craft::$app->getUsers()->getUserById($order->userId);            
        
                if ($user) {
                    switch ($data['type']) {
                        //Occurs whenever a customer is signed up for a new plan. (If SCA is disabled)                   
                        case 'customer.subscription.created':
                            // Add a user group id
                            $newGroups = [1];
                            Craft::$app->getUsers()->assignUserToGroups($user->id, $newGroups);
                            break;
                        //Occurs whenever a customer's subscription ends.                                        
                        case 'customer.subscription.deleted':
                            // Removes all groups
                            $newGroups = [];
                            Craft::$app->getUsers()->assignUserToGroups($user->id, $newGroups);
                            break;
                        // Occurs whenever a customer is signed up for a new plan. (If SCA is enabled) 
                        // Stripe sends the checkout.session.completed (where the order is created) after customer.subscription.created, 
                        // so we need to validate new subscriptions here
                        case 'checkout.session.completed':
                            /** @var \enupal\stripe\models\Subscription $subscription */                
                            $subscription = $order->getSubscription(); 
                            // Do subscription logic to determine the user group id                               
                            // Add a user group id
                            $newGroups = [1];
                            Craft::$app->getUsers()->assignUserToGroups($user->id, $newGroups);
                            break;              
                    }
                }
            }
        });
        ...
        ...        
     }
}    

# beforeProcessTransfer

This event is fired before transfer the $comission->totalPrice to the vendor

use enupal\stripe\services\Commissions;
use enupal\stripe\events\BeforeProcessTransferEvent;
use enupal\stripe\Stripe;
use craft\base\Plugin;
use Craft;

class YourPlugin extends Plugin
{
    public function init()
    {
        ....
        ....
     
         Event::on(Commissions::class, Commissions::EVENT_BEFORE_PROCESS_TRANSFER, function(BeforeProcessTransferEvent $e) {
              $commission = $e->commission;
              $vendor = $e->vendor;
              $order = $commission->getOrder();
              // update the total price if needed
              $commission->totalPrice = 13.24;
              
         });
        ...
        ...        
     }
}       

# beforeCreateSession

This event is fired before a checkout session (opens new window) is created.

TIP

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;
         });
        ...
        ...        
     }
}