Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Cashier Events

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.

like image 298
Daniel Dewhurst Avatar asked Oct 16 '17 13:10

Daniel Dewhurst


People also ask

How does laravel Cashier work?

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.

Is laravel Cashier free?

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.

How do I get stripe Webhook response in laravel?

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.


2 Answers

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.

like image 172
Anas Red Avatar answered Oct 11 '22 18:10

Anas Red


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!

like image 33
Dan Streeter Avatar answered Oct 11 '22 20:10

Dan Streeter