This episode is for members only

Sign up to access "Laravel Subscriptions" right now.

Get started
Already a member? Sign in to continue
Playing
20. Custom webhook events

Transcript

00:00
Let's take this episode to talk about custom webhook events with Laravel Cache. Chances are when you're building a platform like this, you're going to want to respond to certain events that you get back from Stripe within your webhook.
00:12
So the first thing that we're going to do is have a look at the webhook controller that we get with this package. Remember, the root was already registered for us. This is just the controller that's hooked up to that root.
00:25
So if we just take a look at this down the method list, you can see that we get a handle webhook method. This is the first thing that is called. What this will do is it will break up what comes through from Stripe.
00:38
So this is this payload just inside of here. When that gets it through, it's going to go ahead and dispatch a webhook received event directly in here. If the method exists within this file, it will go ahead and dispatch a webhook handled.
00:54
Now, everything that you really need to keep the database in sync is handled within this webhook controller. But if you want to do something custom, you can actually hook in to this webhook handled event.
01:06
So let's open this up. And this gives us the payload. So what we're going to do is create a new listener within our application. And we're going to listen to this webhook received or webhook handled event.
01:16
It's probably best to hook into the webhook received event. So let's do that. OK, so let's go ahead and create out a listener in here. And let's say handle subscription created.
01:28
Perhaps we wanted to send an email when the subscription was created. And we'll take a look at an example of that in the next episode. OK, so we've got a handle subscription created event. Let's go ahead and open this up.
01:40
And what we want to do is listen in here to the webhook received event. So let's go ahead and pull this in. Now, what Laravel will do is it will automatically detect that you're trying to listen to this particular event using this handle method.
01:56
So this should now work. To test this out, we can go ahead and just log out some info in here. And we'll just say handled. Let's go over to our Laravel log and just make sure it's cleared out.
02:06
And let's go ahead and create a subscription and see what happens. OK, so at the moment, I do have a subscription active. So I'm going to go ahead and cancel this. And we'll cancel that immediately.
02:15
Let's go back over to the app, give this a refresh. And yeah, sure enough, we need to buy a plan. OK, so I'm going to go ahead and fill in all this information. And once we have done that, we should be able to see that in our log file.
02:28
OK, let's fill all of this in. And let's subscribe. OK, so we're going to head straight over to our log file. And we should see handled.
02:39
Now, notice this comes through multiple times. What is happening here? Well, we have multiple webhooks that are being sent through to our Laravel application. So what we really need to do is inside of a specific event handle like this
02:53
with handle subscription created, we need to take the event and the payload type and work out whether we need to specifically respond to it within this particular listener. So that's going to look like this. Let's create out an if statement in here.
03:08
And we will go ahead and from the event, grab the payload, which is the payload that comes through from Stripe. And we're going to check the type. So we're going to compare the type to make sure it's the subscription created event
03:22
so we can handle it properly. So the event, as we've seen over on Stripe, is customer subscription and created. And that means now that when we go ahead and log something out, this will only log when we have created a subscription.
03:36
Now, you could do this the opposite way around as well, if you wanted to, if you didn't want all of this stuff in here. So you could say if it doesn't equal, you could go ahead and return. And then you could do all of your logic down here.
03:47
It doesn't really matter what you do as long as you're only responding to what you need. OK, let's try this out one more time. So I'm going to go ahead and cancel this subscription for the account I have. And then let's go ahead and sign up one more time just so we know that this is working.
04:01
OK, so I'm going to go back over. Let's sign up for a plan again. Let's go ahead and fill in all of our details. And let's go ahead and hit subscribe.
04:10
Now, what we should see over in our log file is the handled event only being fired once or the created event only being fired once. Let's go ahead and just reopen that. And there we go.
04:23
So we've got this output once now. So anything that we want to do now, we can just do directly inside of here. We could send an email. We could log something.
04:33
We could create additional things in the database if we wanted to. Absolutely anything you need to do. Let's go through this flow in slightly more detail in the next episode, where we're going to look at sending an email when the customer subscription gets cancelled.
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!