Webhooks are triggered by Stripe on their server to your server. If your server is into a private network, Stripe won't be allowed to reach your server. Use the Stripe CLI to catch those webhook events. Install Stripe CLI
This plugin will be able to listen to webhook events which are related to a Sylius payment only. If you want to listen to other events, you will have to create your own route and controller. However, you will be able to use services provided by this plugin to handle and verify the Stripe events.
Here is how the Sylius PaymentRequest notify process is working:
- Sylius provides a route named
sylius_payment_method_notifywhich is used to listen to the Stripe events. This route needs acodeparameter which is the payment method code. (ex: https://my-shop.tld/payment-methods/my_shop_stripe_checkout) - This route is handled by a controller
sylius.controller.payment_method_notifythis controller will:- Try to find a related
Payment. To do so, eachPaymentMethodmust implement a service with\Sylius\Bundle\PaymentBundle\Provider\NotifyPaymentProviderInterface. - Create a new
PaymentRequestobject with:- The related
Payment. - The current
PaymentMethod. - The
actionequal tonotify. - The
payloadequal to the currentRequestcontent (normalized as an array).
- The related
- A
Commandis dispatch to theCommandBuswith this newPaymentRequestobject.
- Try to find a related
This plugin is hooking into the 2.i part of this process
(see service: FluxSE\SyliusStripePlugin\Provider\StripeNotifyPaymentProvider) to:
- Get the current
Requestdata. - Validate the payload from Stripe (see service:
flux_se.sylius_stripe.stripe.resolver.event_resolver). - Extract the
data.object.metadata.token_hashto retrieve the relatedPayment.
It also hooks into the 2.ii.d part to add the Stripe Event as an array
to the PaymentRequest payload under the event key.
Finally, when the NotifyPaymentRequest command is handled, the NotifyPaymentRequestHandler will:
- Retrieve the Stripe
Eventfrom the Stripe API (to get a fresh one along with the underlying data object). - Send the Stripe
Eventand thePaymentRequestto theWebhookEventProcessorservice.- This service is simply getting the inner Stripe
Eventdata object. - Set this object to the
Paymentdetails.
- This service is simply getting the inner Stripe
- Send the
PaymentRequestto thePaymentTransitionProcessorservice to transition thePaymentstate.
When the event already contains a data.object.metadata.token_hash key and the value is an existing PaymentRequest hash.
If it's not the case, go to the next chapter to listen to
NON Payment relatedStripe events.
Create a new class implementing the FluxSE\SyliusStripePlugin\Processor\WebhookEventProcessorInterface interface.
Then declare a tagged service with tag name(s) among those:
flux_se.sylius_stripe.processor.webhook_event.checkoutfor a "Stripe Checkout" related event.flux_se.sylius_stripe.processor.webhook_event.web_elementsfor a "Stripe Web Elements" related event.
If needed, remember to update the Payment details with either a "Stripe Checkout Session" or a "Stripe Payment Intent" object
depending on the factory you are targeting.
At this point, you are free to do everything you want.
You can create a new route and controller to listen to the NON Payment related Stripe events
and finally create your own Webhook event handler.
But you can also use the existing route and decorate the controller: sylius.controller.payment_method_notify
to handle the NON Payment related Stripe events on the same URL.
Using both cases, you will have to:
- Create a controller.
- Use
flux_se.sylius_stripe.stripe.resolver.event_resolverto verify the Stripe sent payload contained in theRequestcontent. - Send a
Commandto theCommandBuswith at least the StripeEventid and thePaymentMethodcode.