craftcms / stripe

Sync and extend Stripe products and subscriptions.
MIT License
5 stars 0 forks source link

Feature/17 add stripe event #20

Closed i-just closed 2 weeks ago

i-just commented 1 month ago

Description

When a webhook is created via Control Panel > Stripe > Webhooks, it’s created with the following events enabled: https://github.com/craftcms/stripe/blob/1.1.0/src/controllers/WebhooksController.php#L147-L176. Currently, the plugin listens and reacts to them but doesn’t expose those events to other plugins and modules.

This PR adds a StripeEvent event and craft\stripe\services\Webhooks::EVENT_STRIPE_EVENT so that modules and plugins can also listen to the events sent by Stripe that the plugin already listens to.

It also ensures that the successful response (200) is sent back to Stripe asap before the event starts being processed (as per the docs).

The event is called after the Stripe plugin has processed the incoming event(s).

If you need to listen to other events, you can either adjust the webhook configuration in Stripe’s dashboard to enable more events or create a separate webhook and implement your own mechanism for handling the events coming from Stripe.

How to use:

use craft\stripe\services\Webhooks;

Event::on(
   Webhooks::class,
   Webhooks::EVENT_STRIPE_EVENT,
    function(StripeEvent $event) {
        $stripeEvent = $event->stripeEvent;
        $eventObject = $stripeEvent->data->object;
        // process the event based on its type
        switch ($stripeEvent->type) {
            case 'product.created':
                $productStripeId = $eventObject->id;
                // do something
        }
    }
);

Related issues

17