This episode is for members only

Sign up to access "Laravel Subscriptions" right now.

Get started
Already a member? Sign in to continue
Playing
21. Sending an email when a subscription ends

Transcript

00:00
So let's go ahead and send an email when something happens over on Stripe. We'll use the example of a subscription getting cancelled for this, but you can apply it to absolutely anything. So the first step is to go ahead and create a listener again. So we're going to say handle subscription.
00:15
And let's do this for a subscription being cancelled. And actually it might be better to say ended because cancelled is when the user cancels. Ended is when the actual subscription ends at the end of the billing period. OK, let's create this out.
00:29
We know what we need to do now, so let's go over to handle subscription ended. And we want to listen specifically for the webhook received event. Then we want to go ahead and compare this to the event that we're after. So let's go ahead and say the event payload type.
00:48
If that doesn't equal customer subscription ended or in Stripe's case is deleted, then we're going to go ahead and return. Otherwise, down here, we're going to send an email. So let's go ahead and build up an email really quickly to send. And then we'll work out how we're actually going to get the user that we want to send the email to.
01:09
Remember, the data that we get through from the payload here is all over in Stripe. We want to look that user up in the database to be able to send them an email. So let's go ahead and make out a mail. And we'll call this subscription ended.
01:21
We'll set this as a markdown email with the location under email subscription and ended. You can go ahead and modify this. I'm not going to. You just want to go over to ended.play.php and update that with anything you need.
01:34
OK, so to send an email, then we're going to go ahead and say mail to. Now, the only problem is we don't know who we're sending it to at the moment. We don't know how to plug the user out. So let's just go ahead and hard code this to make sure this sends.
01:47
And then we'll go ahead and adjust it. OK, so let's go ahead and send out a new subscription ended email. And that should be just about it. You can pass any dependencies you need through into here to go into the subscription ended email to then be sent through to the template.
02:05
OK, let's make sure this works first of all. So I'm going to go over to the Laravel log, make sure that's clear. I'm going to go over to EMV and search for mail. And we're going to make sure this is set to log.
02:15
So it just comes through to our log file and we should be good. OK, let's try this out. So we currently have a subscription. So I'm going to go ahead and cancel this immediately.
02:24
That should trigger that webhook and we should see an email roll through. Let's go back over to our Laravel log file. And yeah, so sure enough, we have not pulled in mail properly, but we know it's getting to this point of sending. Let's go ahead and just import the mail facade just so we know that this is working.
02:42
Let's just try it one more time. OK, so I'm just going to go ahead and sign up for a subscription again really quickly. And once that's done, we should be back into our app and we'll go ahead and try this again. OK, so let's make sure our log file is clear and let's head back over.
02:56
Give Stripe a refresh and we'll go ahead and get this cancelled. OK, let's cancel this subscription immediately and let's head back over and check out our log file. There we go. So there is the email that comes through. But of course, we can customize and hook up to actually send an email.
03:16
Now, the only problem we've got at the moment is we're hard coding the email address directly into here. Ideally, what we want to do is be able to get the actual user based on the Stripe ID. Now, that's pretty easy because we could just say user where Stripe ID. But actually, Cashier has a really good built in way to do this.
03:34
So we can do this like this. We can use the Cashier object just here and we can use find billable. And that passes or allows us to pass in the Stripe ID of the customer. Now, inside of the payload that we get back from Stripe and all of this is in the documentation.
03:52
So we have already seen the type just here. That's the type of webhook inside of this webhook data. So we're going to access inside of the data. We have an object and then we have a customer.
04:06
So if you need to reference any other data that you want to find, you could either just log out the payload or you could go over to the Stripe docs and see what data you get through. Now, this feels a little bit messy. So I'm going to go ahead and just cut this out and I'm going to extract this out to another method. So let's go ahead and create our method here called get user by Stripe ID.
04:28
And of course, into this, we're going to pass in a Stripe ID and we'll just return exactly what we did there. So Cashier find billable by the Stripe ID. Now, that's a little bit neater. We can just go ahead and inside of here, say this get user by Stripe ID and we can pass that in and go ahead and send that email off.
04:49
So now when we send this over, it should get sent to me. We don't have a subscription at the moment. Let's go ahead and just very quickly sign up for another one. And just while that's finishing, let's go over to our Laravel log and clear this out so it's completely empty.
05:01
Now that we have a subscription, let's go ahead and cancel this and we should get the email sent to that specific user. So let's go over here, cancel this immediately and let's head over directly to our log file. And there we go. It's been sent to the right person.
05:17
So all of these events now and all of this data you can just take and potentially send down to the email that you're actually sending. So, for example, I could take this user that we're plucking out and I could separate that out just here. I could send that to that user, but then I could also pass that user down as a dependency to this email, accept it in, and then I could extract the name of the user, anything else that I need out.
05:45
So there we go. That's how we check for events, which we've looked at for the last couple of episodes, but also fetch the user out of the database using this specific caching method and then go ahead and do something like send an email. And of course, you can apply this to any of the events that you get through.
26 episodes2 hrs 38 mins

Overview

Learn to start accepting subscriptions with Laravel and build a solid foundation for your next application. In fact, it’s exactly how we do things here on Codecourse!

Even if you’re completely new to Laravel, by the end of this course you’ll have a platform where customers can purchase subscriptions and gain access to restricted areas of your application.

We’ll also cover managing subscriptions, cancellations, invoices, giving customers trial periods — and more.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!