9

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.

dmulter
  • 2,608
  • 3
  • 15
  • 24
Daniel Dewhurst
  • 2,533
  • 2
  • 21
  • 39

2 Answers2

1

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.

Anas Red
  • 69
  • 6
1

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!

Dan Streeter
  • 2,259
  • 2
  • 16
  • 16
  • I have been trying to get this package running all week now, I think I am having issues with the webhook endpoint. Mind if I ask you a few questions? – Dustin Nov 08 '18 at 19:30
  • Yeh sure, its been a few months since I worked on this project, but I'll do what I can. – Dan Streeter Nov 09 '18 at 21:32
  • I am using the same Spatie package, but when I send a test webhook I get a 302 to login, I can't seem to figure out what might be blocking it. – Dustin Nov 11 '18 at 01:12
  • If I’ve got this the right way round. Your firing the webhooks on stripe at your application and getting a 302 right. Are your webhooks in the auth route group? Meaning they require an authenticated session. They should be outside of this so that stripe can just talk to your application. – Dan Streeter Nov 12 '18 at 07:06
  • Hey Dan, thanks for the response. I think that is the right idea, but not the issue. I tried moving the route to the very bottom of the web.php page, and got the same error. I have also double checked the CSRF token exception. I posted the question a few days ago here. https://stackoverflow.com/questions/53213735/stripe-webhooks-return-302-on-laravel-application-using-spatie-webhook?noredirect=1#comment93318035_53213735 – Dustin Nov 13 '18 at 14:34