1

Regarding Event: EVENT_AFTER_CREATE_SUBSCRIPTION https://docs.craftcms.com/commerce/api/v2/craft-commerce-services-subscriptions.html#events

The document states "The event that is triggered after a subscription is created. Plugins can get notified after a subscription is created."

Using the commerce 2 as an example:

I just need an expert to confirm that a subscription is created or EVENT_AFTER_CREATE_SUBSCRIPTION has executed only when a user has successfully made payment to a subscription.

  • 1
    I'm building a subscription site at the moment and wondered this too recently, as it's not clear from the docs. Looking through the code at /vendor/craftcms/commerce/src/services/Subscriptions.php ~ line 389ff suggests to me that it should only execute after a payment, but I can't say for sure, as it's only checking for $response->isInactive() -- there might be other ways the response could fail. The alternative might be to use the Stripe plugin's EVENT_RECEIVE_WEBHOOK and listen for customer.subscription.created. I'd like someone more experienced than me to answer though! – James Smith Oct 14 at 10:32
2

I've looked into this in more detail now, and whilst it used to be the case that EVENT_AFTER_CREATE_SUBSCRIPTION only worked for successful payments, it's now (post Commerce 2.2.0), no longer the case due to the fact that a subscription can get created in a "Suspended" status, which still triggers that event (https://github.com/craftcms/commerce/issues/1105).

I'm still using that event, but in Commerce 2.2.7 (and beyond unless they change it again!), you'll need to make an additional check inside your code to make sure the subscription is not Suspended.

Event::on(
    Subscriptions::class,
    Subscriptions::EVENT_AFTER_CREATE_SUBSCRIPTION,
    function(SubscriptionEvent $e) {

        // --------------------------------------------
        // BAIL OUT on Suspended subs!
        // --------------------------------------------

        if ($e->subscription->isSuspended){
            return;
        }

        // --------------------------------------------
        // Continue...
        // --------------------------------------------

        ...
    }
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.