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
13. Checking out with a referral coupon


So now that we've got our plans in the database and we've installed cache here we can now make
this link go through to generate a checkout session and then of course the user can pay. So just before we do this let's just tidy things up a little bit so I'm going to go ahead and set just a little bit of spacing on here and I'm going to add a color to this just so we can distinguish it so let's set that to text indigo 500 and we're pretty much good.
Now if we look at the other user that we've got who has received a referral code and that's stored in their cookie of course it says you will receive a discount due to referral so what we're going to do first is just build the checkout out so we can actually go through to the checkout and then we'll add in the ability to tack on a discount for this person who's using
this referral code. So let's come back over to just make this work first of all. So to do this we need a controller so we need to generate all of this out in code we can't just link through to it so let's go ahead and make out a controller and we'll just call this checkout controller. Let's go over and add this in here we do need to be signed in for this so let's go and create this
out it's just a get route here and of course we'll call this checkout link this up to the checkout controller and we'll give this a name we'll just call this checkout index great okay so over in our checkout controller let's create out an invoke magic method we won't do anything there just yet but let's go over to our dashboard and hook this up so very easy to just link that
through to the checkout great now we need to pass a plan in here because we need to know which plan the user is actually going to be on so I'm going to pass the plan in here and then let's update our checkout controller here to accept that plan in by its slug and then in here we should now get a plan through so we know what we're signing the user up to so let's just dump the plan and let's
click on this and see what happens okay great we go through to checkout monthly and now we can start to build up this url so we're going to go ahead and access the currently authenticated user and we're going to say new subscription now the new subscription method takes in a name of the description so we're just going to call this default because we're only going to have sort of
one type of subscription here and then from the plan you guessed it we need the stripe price that's what we need to determine what to link over to now once we've done this what we can do is return and we can assign this a variable here just called subscription and we can return using the subscription and generate out a checkout link and then into this we can pass in some
checkout options now the only two that we need to add just for now before we get to our referral code stuff is a success url so that's what where the user is going to be forwarded to when they successfully sign up for this subscription and then we're going to have in here a return url or a cancel url i think it's called and that's going to be what happens when they click back
or cancel everything off now for either of these what we're going to set these two of the dashboard because we just want the user to go back to the dashboard once they've signed up if they cancel it they go back to the dashboard if they are successful they go back to the dashboard and the reason for this is that laravel cashier takes care of creating everything in the subscriptions table for you we
don't need to do anything at all okay let's try this out and see what happens so i'm going to go ahead and click here and that should forward us over to a stripe checkout link and it does great so you can see here that we're subscribing to this plan we're not getting the discount yet we're going to look at the referral code in a minute and we can go ahead and enter our details
now when i click back that goes back to the dashboard because that was our cancel url great so what happens if the user has a referral code we have a referral code over here but when we click on this we are just going to be charged the full price we want to take a percentage off of this so what we want to make sure we have is under our product catalog a referral coupon so we'll
grab this referral coupon there's an id here for it which you can set or have auto-generated and this is giving us 20% off for one month or one year whatever plan they sign up to so we're going to grab the id of this code and then what we're going to do is check if this exists now just to make this a little bit easier we've not added the ability yet to stop ourselves from using the
referral code but i'm going to go ahead and just so i don't need to keep switching browsers just go and use my own referral code just so we can test it directly in here so when i click this now i basically want to attach a coupon to this subscription so the first thing we want to check is if we have a valid referral code we're not just going to check if that value exists within
the cookie because it might not then exist in the database if we've got rid of it for a particular user so let's just do this really roughly inside of here there is probably a better way to share this between different controllers rather than having to keep looking it up but we'll go ahead and roll with this so we'll go ahead and at the same time as grabbing out the referral code by
its code so we're going to say referral code where the code equals that value from the cookie so let's go and say request cookie and referral code so if that exists and we get an actual item back from the first one then we have a code in here so let's go over and give this a refresh click on here and there we go so we know that we've got a referral code now and it's at this
point we can then go ahead and reassign the subscription to the subscription but with a coupon and that's going to be the coupon code from stripe just here now what i don't want to do is hard code this stripe coupon directly into my code because it might change in the future so now is a good time this is a good candidate to set this out into config so let's go over to
our config directory here and let's create out a referral config this is just going to be like standard config and we're going to go ahead and say promo code and we're going to set that to the promo code and of course that could go in an environment variable if you wanted to so you could very easily change that over so you could say referral coupon code and set that directly
in there and then you could grab this and reference that in there okay so now that we've got that in config what we can do is use the config helper and say referral and promo code and we could probably call that something better but that should be enough now to get this working so now when we have a referral and we click through onto here we should have that coupon applied and there
we go we've got our referral coupon and that's giving us twenty dollars off now the only problem with this is if we just go ahead and clear out our cookies so if we come over to our storage section and get rid of our cookies we're going to have to sign back in here of course so let's just very quickly do that let's go ahead and check out this will work but the next thing that we're going
to do is not quite going to work so what we want to do is when we get our webhook back from stripe to say that a subscription has been created we need some way to tell stripe can you keep hold of this referral code so when i get the webhook back i can look this up properly because when the webhook comes back we're not going to have access to the user's browser cookie so what we want to
do inside of this checkout area is set some additional subscription data so we'll go ahead and say subscription data and this is all just stuff that gets passed over to stripe and we can actually set some metadata inside of here so this can be any key value pair so you can call this pretty much whatever you want so we're going to go ahead and say referral code
and access the referral code code which we have here and in fact i'm just going to break this up just so it's a little bit easier to understand it's a referral code yeah that looks a lot better so we've got our referral code if we have a referral code attach the coupon that's fine but we want this referral code in here now at this point we don't have a referral code so of
course we're trying to access the code on a null value if i were to say something like an empty string here and i try this again it might actually work but i kind of want to just not send this down if there's no referral code so let's just go ahead and just make this a little bit better so we're going to say subscription options and we're going to set them to the
defaults that we always want to see which is the success and the cancel url then what we can do is take the subscription data and only merge that into them options when we have a referral code it just means that we're not sending additional data down to the stripe api that we don't need so now we can do another if statement here and say referral code so we can say something like array
merge and we can take the subscription options and we can merge in the subscription data just inside of here so that should be okay let's just die dump on our subscription options just so we can see that this is working let's come over and click on this and yeah we just get the standard success and cancel let's go ahead and use our own referral code and sign up so let's come back over
here to our dashboard click on here and yeah there we go so we've now got that in there if that code exists so let's go ahead and just set this up again so we can actually link through everything looks good so what's going to happen now is once this subscription gets created that metadata is going to exist on that subscription object so when we get the webhook back we've got a reference to
the referral code that was actually used for this sign up which is really great so that means that when we get on to reading our webhooks we can grab the referral code and we can create some more records in the database we could send an email to the user we could do pretty much anything that we need now that we've got reference to that referral code
24 episodes2 hrs 39 mins


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!


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