This episode is for members only

Sign up to access "Build a Referral System with Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
14. Webhooks and Stripe event listener

Transcript

00:00
OK, so the goal for this episode is once the user has clicked on this link and gone ahead and signed up for a subscription, we want to get back the data via a webhook into our application so we can see if a referral code was used for this subscription.
00:13
And then that means we can link this up to the subscription that was created in our app so the user can see and we know what subscriptions have been created via that referral code. So let's take this one step at a time.
00:26
First of all, get webhooks set up. So for local development and exposing my local server, I'm using Expose. It doesn't matter which solution you use, but I'm going to show you how to go ahead and run
00:39
the command to actually get this set up. So if you want to use Expose, go ahead. So what we want to do is over somewhere in our application, I'm going to set up a new tab for this.
00:49
We're going to go ahead and say Expose Share, and then give the name of our app or the URL of our app. So when I go ahead and run this, what this is going to do is it's going to create a public URL to my application, which
01:02
then I can use within Stripe testing. So we can grab this link here, and we can go ahead and create a webhook over on Stripe. Now, I've already gone ahead and done this,
01:11
but you just want to go ahead and click Add Endpoint. And you want to make sure that you have specific events in here. Now, all of these are in the Laravel Cachier documentation, so you want to make sure you add every single event in here
01:24
when you set this up. Let's actually take a look at what this looks like. So you paste your URL into here, and the endpoint that you want is going to be Stripe webhook.
01:33
So let's just go back to see what it is. So slash Stripe slash webhook, that's the webhook that Laravel Cachier, or the route that Laravel Cachier registers. So we are going to go ahead and use that.
01:46
And I think it's Stripe webhook, but we're not actually going to create this one. And then the events that we want, we just want to go ahead and select and find all of the ones
01:56
that are in the Cachier documentation. So those are, we go back to this one and have a look at the events. We're listening to all of these. So make sure they're all added, and you'll
02:06
be set up for subscriptions. So once you've got this set up with slash Stripe slash webhook, a really important thing that we need to do is head over to the middleware section in our application under HTTP.
02:20
And we want to go ahead and ignore Cross-Site Request Support to Read Protection for that specific route. So we basically want to say, anything that starts with Stripe, ignore the Cross-Site Request Forgery token,
02:31
because otherwise it's not going to work. OK, so once we've done that, we then want to grab the signing secret for the webhook that we've created.
02:39
And we're going to go ahead and add that to our config that we saw a little bit earlier. That just means that when the Stripe hook comes back into our application, we can actually verify
02:48
that this did come from Stripe. That's really important. OK, so once we've done that, once you've set the webhook up with all of the events pointing to the exposed URL
02:58
in your application with slash Stripe webhook, you should start to receive these events in. So let's just test and see if this works. So I'm going to go ahead and open this up.
03:06
This will log everything that we get back in. And let's go ahead and sign up for this subscription and see what happens. So let's go ahead and enter some test information in here.
03:14
It doesn't really matter what we put in, as long as we do 4242 and a valid date. And let's enter our postcode. And let's hit Subscribe.
03:25
Now, we want to make sure we're keeping an eye on this just here. Hit Subscribe. And we should start to get some webhook events come
03:31
into our application as this works. Now, once we do have a successful payment, that's going to go through to the dashboard again. We can use that to check whether the user is now subscribed.
03:40
We'll do that as well. But as you can see, Stripe has forwarded over a couple of webhooks. Now, a really important point that I'm going to note now
03:48
is that webhook events can be duplicated. So we might get a successful subscription creation event come through a couple of times. And we need to really be careful with that,
03:57
because when we're dealing with something like referrals, if you think about it, when Stripe says someone has paid their bill this month, we don't want to add that twice or more to our referral payouts.
04:09
Or we're going to be paying out multiple amounts of the amount that we should be paying. So it's really, really important. But we will discuss that later.
04:17
OK, so now that all of this is coming through, what can we do? Now, the stuff has been done in the database. So the subscription's been created.
04:25
The subscription items have been created. The user's Stripe ID has been updated. But how do we get this Stripe webhook data? That's really, really important.
04:36
Well, what happens is in the Stripe webhook controller, if we just open it up under Cachier Package here, we can see that when something comes in, let's just find it, we get this webhook handled event dispatched.
04:50
Now, what that means is once Laravel Cachier, with its controller that's tucked away in its package, is allowed to, well, does its thing, it fires this event so we can continue to do stuff
05:01
and extend this functionality, rather than having to create our own controller or extend this controller as a base. So what we can do is we can come over to our event service
05:11
provider. And up here under Listen, we can use that webhook received event that gets sent by Laravel Cachier. And we can hook this into our own listener.
05:22
So let's go ahead and create out a listener. And we will dump this just to make sure we're getting this data back. So let's create out a listener called Stripe event listener.
05:32
You can call it anything. It doesn't matter too much. But let's go ahead and say Stripe event listener. And we're done.
05:41
So now over in our Stripe event listener, what this is going to allow us to do is handle this with the payload we get back. So what we're going to do for now is just log out
05:51
the event payload. So event is going to be the object that's sent from here. And we can just log that out. So we're going to make sure our Laravel log is completely
06:02
clear. We're going to sign up again. And we're going to see this in action. OK, so now that we've hooked everything up,
06:07
let's go and just clear the database out of subscription items and subscriptions. And let's sign back up. So I'm going to click on this.
06:15
We're going to sign up. And we should get all of the data that is forwarded through Laravel Cache into our own logs, which means we can use that data.
06:24
So let's go ahead and sign up again. And just wait for this to finish. Great, so that's worked. Let's just make sure that that was successful
06:33
and the webhook requests were successful. And if we open up our Laravel log, we now get all of this data in from Stripe. So obviously, we're not going to log this.
06:42
We're going to take this data. And we're going to find out some information that we need from it. For example, when we set up our checkout controller,
06:50
remember we sent that metadata over with the referral code. Let's search for referral code. And as you can see, it is in here. So basically, now when a subscription gets created
07:01
and we're forwarded back and the webhook hits, we can look up the referral code. We can attach that to the subscription. And we're good to go.
07:09
We'll do that over the next few parts, but at least we're getting this data back now, which is really handy. Okay, so inside of our Stripe event listener,
07:16
this is where we're going to be doing all of that. We're not going to do that just yet. One thing we do want to do is just make sure we change around the dashboard
07:23
to show that a user is actually subscribed. So let's go over to our dashboard really quickly to tidy this up, and then we'll move on. So we only want to show all of this stuff here
07:33
if we're not subscribed. So again, with our user and having that billable trait, we can detect whether we're subscribed to the plan that we created.
07:43
So let's end that if there. And of course, because we are now subscribed, this shouldn't show on the dashboard. And then otherwise, we can just show
07:51
you are subscribed down here, because we don't really need to do much else. So let's say you are subscribed. Great, so this user is subscribed now.
08:00
While we're testing this out, of course, what we can do is get rid of the subscription items and the subscription, and we're back to this again. We'll end up with a load of test data,
08:08
but that's absolutely fine. Okay, so we are now successfully receiving these webhook events, and we've got this data, but we actually need to do something with this now.
24 episodes2 hrs 39 mins

Overview

Let's build a feature complete Laravel referral system, completely from scratch.

We’ll cover generating referral codes, setting a cookie to track the referral process, hooking up referrals to subscriptions and displaying detailed referral stats in a dashboard, so your users can see how they're doing.

On the admin side, we'll set up a job to automatically generate a CSV with all the amounts you need to pay out each month, automatically mark referrals as paid, and display historical referral payments for users.

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

Comments

No comments, yet. Be the first to leave a comment.