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
15. Linking subscriptions to referrals

Transcript

00:00
Okay, here's how this is going to work. A little bit earlier over in our referrals dashboard, we saw a list of people who are currently subscribed from our referral code. That's
00:10
pretty much what we want to link up in this episode. But the reason this is useful is, if further payments roll in, so if next month we have a payment and the month after we have a payment, in our database we're already going to have a link to show which subscription is attached to which referral code. And what we can then do is we can then create a referral payout in another
00:33
database table to show that that was a successfully paid referral in the future. So every single month we're going to be receiving money for each referral. So what we're going to do in this episode is set up the database schema for this, so we can understand how these two things link together. And then we're going to go ahead and handle our subscription created event
00:54
back from Stripe. And what that will mean is that when we get that back we can look the referral code up and attach the two together. So let's get started with the schema first of all. Okay, so we're going to go ahead and make out a migration here and we are going to create a table that links up the referral code to the subscriptions that Laravel Cashier creates. So we're going to
01:16
call this create referral code subscription table. Okay, let's create this out and we'll head over to that create referral code subscription table and let's fill this in. Okay, so we need to know which referral code was used when a user signed up for a subscription. So we're going to say foreign id, referral code id, and I'm sticking to Laravel standards here so we don't get into
01:43
trouble and having to and have to rename stuff. And let's go ahead and use the subscription id here as well from the subscriptions table. Now really importantly what we want to do here is add in the multiplier. So that's just going to be the percentage that this user gets. I mentioned at the start of the course that we want to store this on an individual level as these are created because
02:05
for example over in our referral config if we create a multiplier in here of 20% for example we might increase that later on so any new subscriptions now need the 20%. Now if we hard code into our entire application that users always going to get 20% then it's going to be really difficult to change this later on. So the multiplier we're going to store in config here and
02:28
I'll leave that in there because we'll need that later. So this is going to be the multiplier that's stored and that's just going to be an unsigned float and we'll just call that multiplier and there we go. Okay so now that we've got this let's go ahead and migrate this so php artisan migrate and just before we do anything let's take a look at how this is going to look in practice.
02:50
So let's think about this a user signs up for a subscription when cashier creates the subscription it will create a record in this table which we're not going to touch. Our referral codes obviously we created ourselves but this referral code subscription table will now link this up. So let's say I send a referral code over to Mabel and my referral code id is let's just find that
03:13
5001 her subscription id is say one maybe it's the first subscription and the multiplier will get inserted based on the config we've set so at that point in time how much percentage are we giving to these users. Now I'm not going to save this because we don't have any subscriptions here but that's pretty much how it's going to work and now from this what we can do from the user's
03:34
referral code here we can work out how many subscriptions are currently active based on that referral code. So let's go and just get rid of this because we do not need this anymore and let's look at the actual event that we need to get this working and we'll be done by the end of this episode. So we're going to come over to our stripe event listener and the handle and we first of all
03:58
need to work out which event gets sent in here so this will send this specific event or dispatch this event for every single stripe event that gets pulled in so inside of our payload if we look in our laravel log we've got a type in here so let's find that there we go so this is the stripe event that gets sent in so inside of handle what we could do is add in a match on the event
04:22
payload type now unfortunately this is an array so we're going to have to access this obviously like an array which is okay and in here with this match statement we can say invoice and oh no i think it's customer subscription and it might be dot created we'll check this out in a second if the payload type that we're getting through is a new subscription creating we're going to defer this
04:48
over to another method in this class so we can keep things nice and tidy the last thing we want is a bunch of if statements in here so we're going to say handle subscription created and we're going to pass through the payload so we can further read that data okay we don't have this method at the moment so let's go ahead and create out a method in here called handle subscription
05:09
created and that will receive in that payload really importantly we also need to set a default here which we're just going to set to null if we have an event that we're not handling okay so again what i'm going to do and we're going to be doing a lot of this i'm just going to log the payload out here or in fact let's just log out sub created just so we can see this roll in
05:33
and now when we sign up for a subscription this will detect the type it will defer to this method and we should be good so let's go over to our laravel log clear it out and again let's sign up for a subscription and let's hit subscribe that webhook will pull in it will detect the event type and it should log that out for us there we go great so we now know that we're handling just when
05:56
a subscription was created so what do we need to do here well we need to first of all look up the referral code from that metadata so let's create out a referral code in here we'll go ahead and reference our referral code model and let's just say where code and in here this is really tricky so i'd recommend dumping the payload so you can actually have a look at it or use a third-party
06:24
solution like ray which is really good for inspecting these things but effectively what we want to do is use the laravel array helper to pluck this out really easily so from the payload we are going to get a nested object the array the outer array is called data inside of that is object and then we have the metadata that we sent along with the subscription creation
06:47
which will have that referral code so hopefully now this starts to make sense if it didn't before that's why we're sending that referral code over in our checkout controller just here so when the webhook comes back that data is in there and we can pick it up it's really helpful so in here we'll just say first and that now we have our referral code that we can use to attach with that user
07:11
now a really important part about this is what we want to do is make sure the subscription actually exists first of all now there are times when this will get fired and it hasn't quite been created in the database yet so what i'm going to do is i'm going to create out a retry in here and this is just a helper function in laravel that allows us to retry something again and again unless we get
07:32
an exception and we're going to go ahead and create our closure in here so we're going to retry what we're about to do again and again and we're going to have a 500 millisecond delay between each retry so what that means is in here we're going to hook up the referral code to the subscription now if the subscription doesn't exist so if we don't have a subscription then this will just
07:57
retry because we won't be able to find the subscription this will throw an exception and this will try again so we want to make sure that this definitely gets hooked up because it's really important when we're building something like a referral system for this to actually work properly okay so the first thing we want to do in here is actually look up the subscription in
08:15
the database which obviously might not exist so let's create our method for this because we're going to use this a couple of times so let's say get subscription by stripe id and we'll pass in the stripe id into here and we'll go ahead and return a subscription which is the subscription model from laravel cashier so that's really important we need to pull this in from laravel
08:40
cashier that's fine it still exists within our application and we're going to say where stripe id equals the stripe id that we are passing in and we're going to say first or fail now first or fail is really important because if we look the subscription up in here and it doesn't exist yet retry will try again until it does actually exist now just a bit about this stripe id if we head
09:05
over to our database that is just in here that represents the actual subscription itself so that's why we're looking at the stripe id because in this payload we'll have the stripe id sent back for us okay so now we can actually look this up so we'll say this get subscription by stripe id and again we're going to have to use our array helper here to pluck this out of the payload
09:28
and this exists within data object and then it's just id so again if this doesn't make too much sense just dump the entire payload out in your logs and you can find it now we because we're using a closure here we just need to bring our payload into scope and we should be good so once again what we're going to do is go through the subscription flow just to make sure that this
09:49
subscription is actually getting pulled back and we know that we've got the correct subscription so let's go ahead and log this subscription out in here we'll go over to our laravel log and clear this out we'll go over and we'll retry this again so we'll need to get rid of our subscription and let's go over and sign up and hopefully get that subscription logged out okay so let's go
10:11
ahead and hit subscribe we'll head straight over to our app and we'll just wait for that log to roll in whether it's an error or in our case we've done everything correctly and we actually get that subscription object back great okay so we've now got the subscription but what we now need to do is hook up the subscription to the referral code now the referral code itself we didn't actually
10:31
create a relationship out on this for the subscriptions themselves so let's go ahead and create out the subscriptions relationship for that migration that we just created in this episode and this is going to be a belongs to many because we're using a pivot table here so belongs to many and here we're going to say cashier and then subscription model now the reason that we're
10:55
doing that is because in config this subscription model can change so we're going to make sure that we reference it directly like that rather than it directly from the class so we're going to say with pivot and multiplier now the reason that we're doing that is because as we know in our referral code subscription we included this multiplier by default that won't be included when we get this
11:17
data back so we want to make sure we include any additional columns that we've added to our pivot table okay so now that we've got the subscriptions relationship we can go ahead and sync these two things up now i say sync this is really really important we don't want to create this record more than once really important it's only done once in the database so we're going to say from the referral
11:39
code which we just need to bring into scope here access the subscriptions relationship and then we want to sync this without detaching so what this will allow us to do is pass in an array of ids that we want to sync up so we want the subscription id in here like this now normally what this would look like is something like this so let's say i wanted to attach subscription one
12:09
two and three to this i could do that very easily with this we're just attaching one but we need a dynamic id for this directly inside of here but we also so we're going to create a nested array here want to add in the multiplier which will just be the currently configured multiplier we have in our application so we can say config referral and multiplier great okay we're going to go over this
12:34
in a bit because i realize that sometimes this can be a little bit confusing but now we're going to go and again sign up for another subscription make sure that this gets synced up properly so let's go over to our database and get rid of the current subscription that we have of course this table is empty but by the time we're done it should be filled with everything
12:53
that we need so let's go ahead and hit monthly and let's sign up and hopefully this is all good so let's just wait for that to process great that's done we'll come over to our laravel log nothing's in there which is good news and there we go great so the referral code that we used for that newly created subscription with an id of five which is the latest one has now been hooked
13:18
up to that referral code with that multiplier now we've not got the created and updated updates if you did need them for any reason what you can do is say with timestamps but we don't really need them we don't really care when this was attached but you might need that data okay so now that we have done this let's roll through this just one more time just so it makes a sense if it didn't
13:42
so when our subscription gets created cashier handles the creation of the subscription in the database and all that stuff but with our referral system we want to know which referral code signed up which subscription so when the payload comes back for a subscription created we access the referral code using the referral code we sent off with the checkout what we then do inside of this
14:05
retry just to make sure that this subscription can actually be found we look the subscription up in the database by the id that comes back from stripe and then we attach the subscription to that referral code we use sync without detaching because if this webhook gets fired more than once what we don't want to end up with is multiple records in here we don't just want to
14:28
create this like a normal relationship so we want to make sure we sync this without detaching so this gets created properly and we pass the stripe subscription id in here which gets filled in here and we additionally give some additional pivot columns which is the multiplier that we added to config because of course if the next person signs up and over in our referral config we want
14:50
to increase this at some point to 30 the next subscription that signs up is going to get 30 okay so now that we've done that we are successfully hooking everything up let's take a break from webhook stuff and what we're going to do now that we've got all this data in here is we're going to output some stats on our dashboard
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.