I'm using Laravel cashier 7.0, and I'd like to fire some methods after a subscription is successful. I hoped there would be some events I could listen for, but that doesn't seem to be the case (unless I missed them).
Stripe's the payment provider I'm using, if that makes a difference. (stripe-php package.)
Am I missing something obvious?
I know there are many ways of doing this myself, but I'm looking for an elegant solution.
It handles almost all of the boilerplate subscription billing code you are dreading writing. In addition to basic subscription management, Cashier can handle coupons, swapping subscription, subscription "quantities", cancellation grace periods, and even generate invoice PDFs.
Laravel Cashier is a free open-source package that provides a fluent interface for working with Stripe and Braintree subscriptions: Laravel Cashier provides an expressive, fluent interface to Stripe's and Braintree's subscription billing services.
Handling webhook requests using eventsWhenever a valid request hits your app, the package will fire a stripe-webhooks::<name-of-the-event> event. The payload of the events will be the instance of WebhookCall that was created for the incoming request. Let's take a look at how you can listen for such an event.
Just build an API endpoint in your routes/api.php and then add them to your stripe account. Stripe will treat those APIs as webhooks. Once someone subscribes to one of your services then Stripe will call the supplied webhook on your server to let it knows about the new subscription event.
In your routes/api.php or (better) in a separate API controller, add whatever event you need to fire there.
in Laravel, make sure to:
1- remove middleware('auth:api')
on these webhooks.
2- then analyze the payload that sent by stripe to verify the payment
you can refer to Stripe documentation here for more details.
The guys over at Spatie have created a beautiful package for this exact purpose.
I've just implemented it within my own subscription management app for Stripe webhook handling and its, well - a breeze!
Their blog post on it is here: https://murze.be/handling-stripe-webhooks-in-a-laravel-application
And the GitHub repo is here: https://github.com/spatie/laravel-stripe-webhooks
Took about 5 minutes to get up and running and creating my own handling of the inbound webhooks, including reading it!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With