In this episode, we're hooking up an automated email to be sent out whenever a Stripe subscription ends. The focus is on handling the "subscription ended" event, but the same approach works for other types of Stripe events, too.
We start by setting up a webhook listener, specifically looking for the customer.subscription.deleted
event from Stripe. When that event comes in, the first thing we do is make sure it matches what we want—otherwise, we just ignore it and move on. Once we've got the right event, it's time to send the email!
At first, we hard-code a recipient email just to test things out. After confirming the email actually sends (we check our Laravel log for that), the next step is to make it dynamic—meaning, we want to send the email to the actual user whose subscription ended. Stripe only sends us the Stripe customer ID, so we use Laravel Cashier's handy findBillable
method to locate the user tied to that ID in our database. To tidy things up, this gets pulled into its own helper method.
Once that's sorted, we wire up the email to send to the correct user and do another test—success! The episode wraps up by mentioning that you can now pass whatever event data you need to your email templates, and this technique works for any Stripe events, not just subscription endings.
So by the end, you'll know exactly how to react to Stripe events in your app and trigger customized emails to your users.