This episode is for members only

Sign up to access "Laravel Subscriptions" right now.

Get started
Already a member? Sign in to continue
Playing
10. Configuring webhooks

Transcript

00:00
Setting up webhooks and keeping everything in sync is probably the most difficult part of building a platform like this. We're going to talk a little bit about what Stripe's webhooks are and why we need them, and then we're going to go ahead and set them up. So when we go through to checkout, Stripe will send a payload through to our application and Laravel Cache will create the subscription in the database for us. Okay.
00:24
First of all, what are webhooks and why do we need them? Okay. So what's going to happen is when we're forwarded over to Stripe, that's going to create the subscription, but our application doesn't know about that. If we head over to the developers section here under webhooks, what we can actually do here is create out a webhook and add an endpoint.
00:44
So let's go ahead and add an endpoint now. We're not actually going to do this because Laravel has a command that allows us to set all of this up very quickly, but let's add one anyway. And I'll show you what I mean. So our endpoint URL is going to be the same URL as our application, but obviously this is a local only domain.
01:04
So this endpoint would not actually work. Then we're going to have a specific route that we can hit like webhooks and Stripe, and then we can choose the events that we want to listen to. Let's just take a look at some of the events that we have under the subscription section, just to get an idea. So this one here is customer subscription created.
01:25
So when a subscription is created, Stripe will send a request to the URL that we've defined here with all of the information about that user. What this means is then on the backend, we can look that user up and go ahead and create the subscription in the database. So manually creating these is fine, but we actually have a command within Laravel and Cache here that we can actually use to do this automatically. Let's go ahead and run that now, and this will go ahead and hit Stripe's API to create these out for us.
02:00
So we have the Cache webhook command. Let's go ahead and run this and see what happens. Okay. So the Stripe webhook was created successfully.
02:09
Let's head back over to Stripe and give this a refresh. And what we should see is a endpoint created for us just here with the domain and the correct path with all of the events that we need to listen for. So you can see it's listening for all of these events. So effectively, anything that happens over on Stripe now, for example, if a customer cancels their subscription, this will let our application know, and we can adjust this in the database accordingly and remove that access.
02:37
Now that's webhooks done, but the other issue is how are we going to serve our local application so it's accessible via the internet? Well, to do this, there are a few options. Since I'm using Laravel herd, this has exposed functionality built into it. So exposed is a service to expose your local machine anywhere accessible on the internet.
03:00
So if you are using Laravel herd, you can just run a very simple command to get this set up. Otherwise you can use expose on its own, or there are plenty of other options that you can choose from. The main thing that you want to be able to do is make sure your local machine is accessible. Let's do that now.
03:20
And once you've done that, you can change around the URL inside of Stripe. So over in my project, I'm going to go ahead and run herd share. So once I do this, this is going to share my local machine on this domain. You can see laravelsubscriptions.sharedwithexpose.com.
03:38
Let's go ahead and open that up in our browser and just see if this works. And this is now a public URL that anyone can access, including Stripe, and you can see it comes through to our application. So once you've got to the point where you can do this, whichever solution you're using, you can go ahead and switch this over under the webhook. So we're going to update the details of the webhook here to point not to our local domain, but we're going to point to that domain that's being served.
04:03
Okay, let's go ahead and update this endpoint. And now we should be good. So what's going to happen now is when we go over to checkout, this is going to send a webhook request through to our application. Using herd specifically, we can monitor this in here and we can see these requests come through to see if they fail.
04:21
And actually at the moment, they are going to fail. There's a good reason for that. So let's talk about that first, and then we will finally go ahead and create a subscription. Okay.
04:31
So the endpoint that Cachier will automatically register for us is Stripe slash webhooks or slash Stripe slash webhook. That's a route that's created when we install the Laravel Cachier package. So we don't need to manually create this route within our web routes file, but we have cross-site request forgery protection enabled by default within Laravel. What we're going to need to do is get rid of this specifically for this URL.
04:58
So let's go over to our bootstrap and app file within the framework. And let's go down to where we have this with middleware method here. What we can do inside of here is exclude any routes that we don't want cross-site request forgery to work with. So we're going to say validate cross-site request forgery tokens.
05:20
And then in here, we're going to pass it in the accept option. So let's say accept, and we're going to choose an array of any of the routes that we want to exclude in here. Now this is going to be Stripe slash and then webhook, or you can use an asterisk to exclude anything that starts with Stripe. Okay.
05:39
Now that we've done this, we are pretty much good to go. Let's keep an eye on our database here under subscriptions, because once we create this subscription over on Stripe, a webhook request will be sent through to our application and it will create everything in the database for us. Okay. Let's go back over and let's try this out.
05:55
So let's go and sign up for a monthly subscription here and put in some fake car details. And once we've done that, let's just add in a postcode. Let's hit subscribe and see what happens. So we can actually monitor this, like I said, over in here, and we should see a few post requests come through to that endpoint.
06:16
They have now been sent through from Stripe and it looks like we got a successful with a 200 status code. So now that's done, what should have happened is, is a subscription has been created for us. Let's check the database out and have a look. And you can see sure enough, this hooks up to my user, user ID one.
06:36
It shows the Stripe ID in here, the status, the price, the quantity. And we have a additional record in here for the subscription items, which we can kind of ignore for now. So as long as you're at this point, going through the steps to set up a exposed local environment and you've set the web hook up in Stripe, everything will now be kept up to date. So for example, when this subscription gets canceled over on Stripe, if we just come over to the transaction section, you can see this is the payment that we've just made.
07:06
We can come over to the customer, we can find their subscription and we can cancel this. Let's go ahead and cancel this immediately. And once we do that, that's going to send another web hook request through to our application. So if we head back over to our database and give this a refresh, you can see sure enough, the status has changed to canceled.
07:24
So this is really useful now. Everything is being kept up to date, but the only issue we have at the moment is over on our app, we are not showing if the user is subscribed. How do we do that? Well, let's go ahead and have a look at how we do that in the next episode.
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!