Playing
01. Introduction and demo

Transcript

00:00
OK, in this course, we're going to be building out a referral
00:02
system in Laravel completely from scratch. And this will involve, obviously, generating a referral link for a user. This will hook up to the subscriptions
00:11
that we have in the database, so we know how much to actually pay out. We'll be looking at Stripe webhooks to actually add these records to the database
00:20
when a payment comes in and then assign that to a user. And then finally, we'll look at generating out a spreadsheet of all of the payouts that we need to pay out with something
00:30
like PayPal mass payouts. So we're covering a huge amount here. And even if you're not building a referral system, some of the techniques that we're
00:37
going to be using in here are really applicable to other applications as well. So I'm going to start by just showing you what it looks like in my account at the moment.
00:45
I've already had some referrals, and I've already converted some users. So let's go ahead and just sign in to my account and let's see what we've got.
00:53
So you can see that I've got this referrals option enabled We've got our referral code at the top, which, of course, we can just click to copy. And then we can paste it, give it to other people.
01:02
And these are our basic stats. So this will be how many times someone actually landed on the referral page, which I'll just show you here. It looks like this.
01:12
And then we have how many times this was clicked, so how many times we actually clicked to set that cookie, which will last for a month when someone clicks this.
01:20
And then we have the amount of conversions, so how many people signed up with our referral code. So they're our basic stats. But then more interestingly, we've got our active referrals.
01:29
So we can actually see who is currently on a subscription and who we referred. And it will tell us how much their plan costs, and it will tell us the percentage
01:38
that we're receiving for that. So this will just log all of the subscriptions that you are responsible via your referral code. Down here, we've got our payout.
01:46
So you can see I signed this user up on the 13th here. And then if we go down, I've received a payout on the 10th because that's sort of a month afterwards. We have a delay to how much we can pay
01:59
or when we can actually pay out a referral because we want to make sure that nothing was canceled or no refunds were made or anything like that. So you can see I've got 16 here because when a user does sign
02:11
up, they get 20% off. So we get a 20% cut of what they had 20% off. But you can adjust that if you want to. So here, we've got the next payout,
02:20
which was 20 because, of course, that's 20% of 100. So that is the full amount. And that will just keep on going. Now, how do these payouts actually work?
02:29
How does this actually happen? Well, we have, if we just take a look at our code really quickly, a scheduled job here, which will generate out a referral payout spreadsheet.
02:39
So what this will do is it will run monthly on the 1st at whatever time you want. And this could be any day. And this will build up a spreadsheet
02:47
of all of the referral payouts that you have to pay out. So you could either use this to do some sort of PayPal mass payout if you wanted to, or you could just manually pay these out, depending
02:57
on your method of payment. It doesn't really matter here. The technical stuff is all going to be the same. How you pay your referrals or your people
03:05
who are referring is up to you. Okay, so now that we've had a look at this, I'm going to clear everything out and we're going to go through the flow of this, of how someone would get referred.
03:15
And we'll go through the Stripe checkout and we'll see all of this work. So I'm going to clear all this out and I'll be back with you in a second.
03:21
Okay, so I've gone ahead and cleared everything out. Let's go ahead and sign up for an account and get our referral link so we can actually refer someone. So let's sign up here with some really basic details.
03:30
And you'll see on the dashboard here, we have this monthly option. When I click this, this is going to go through to a Stripe checkout for this user to purchase this plan.
03:37
Now I'm not going to do that at the moment because I want someone else to go ahead and sign up and then see our referral come in. But this is basically a really rough implementation
03:45
of different plans that you can have with Laravel Cashier. And we hooked that up with Stripe. Okay, so I'm going to head over to my menu here and you'll notice that we don't have
03:54
this referral section shown at the moment. Now again, this is entirely up to you, but what this requires, the middleware and the checks that are working here
04:02
is that we have to have a PayPal email so we can start to actually generate referrals. Now behind the scenes, before we've done anything here, we just check the database.
04:11
You can see that we have actually generated a referral code for this user already when they signed up. So that's all working nicely. Okay, so I'm going to go ahead
04:18
and add in my PayPal email in here, hit save. And now I have access to the referrals. So I can go ahead and click on this. And as you can see, this is exactly what we saw before,
04:27
but of course I haven't referred anyone, I haven't converted anyone. So we're going to go ahead and copy this link. Now for now, what I'm going to do is sign up myself
04:34
using my own referral link. I've just disabled the functionality to stop us actually doing that temporarily just so we can do this
04:42
without having to sign into a new account. So I'm going to go ahead and pretend to refer someone. Technically, this is myself. And when I click sign up for 20% off,
04:51
what that is going to do is it forward the user over to the application. And once they're signed in, they will receive a discount. So you can see that this is changed now,
05:00
and it says you'll receive a discount due to referral. Obviously it looks very basic, but you can change the flow of this. You can change pretty much anything.
05:07
So I'm going to go ahead and sign up for a plan here. And we are going to just enter some information here. So let's do 42 all the way along. And let's just enter anything in here.
05:23
And just enter a couple of details. And hopefully that's good. So we get 20% off here when we subscribe, which is great. The user that you're referring gets 20% off
05:38
for using your referral code. And let's just wait for this payment to go through. And we're back to our app and we are subscribed. So now obviously this is going to completely change
05:46
and you can see that we've got visits one, clicks one, and conversions one, but obviously that's going to be different. We've got our active referrals.
05:53
So we have signed this user up at that plan. Obviously it's myself, but that would be someone else. And then of course we don't have any payouts at the moment. I'm not going to go through the entire payout flow
06:03
just yet, we'll get to that. But effectively, like I said, this is going to happen every month on a specific date. But let's just have a peek inside the database
06:11
just to see what we've got here. So we've got our referral code that was generated when the user signed up. We've got this referral code subscription,
06:18
which links this to that particular subscription. Now, the reason that we're using an intermediate table here is that we could change the multiplier frequently. So at the moment it's 20%.
06:30
We don't really want to have this as a global value because we want to change this on a perhaps yearly or monthly basis. So we might want to change the multiplier discount
06:41
to be 20%, 40%, something like that. So we're storing this specifically at the point in time the user signed up so we can change it later on.
06:49
That's really important. So this hooks up to the user's subscription. Now, we're going to leave these tables alone. They're from Laravel Cashier.
06:56
We're not going to modify these tables at all. We're going to keep them as they are. And this is the subscription that's been signed up. And then we've got this referral payments.
07:04
Now this has already been generated. And the reason this has already been generated is I have a webhook set up within Stripe and I've got this running
07:13
and this is actually sent through from Stripe. And it's already generated a potential payout in the future. So this is available at the first month of next year. So we're in December at the moment
07:24
and that goes over to next year. And that's when this can be paid out. Now, the reason that's the case is because if this gets refunded,
07:31
I can just go ahead and delete this record from the database or from my admin panel. And that won't pay out for that user because it's been refunded.
07:38
We don't want to give people money for refunded payments. Now, paid out will be changed when that script runs to generate out that spreadsheet, which allows us to pay. So all of this seems quite complicated
07:51
and there are some bits of this application that are gonna get a little bit tough to get our heads around, but we're gonna work through it step by step,
07:57
build this out and even change bits around here depending on the flow of your application. So we're gonna keep things as super simple here. So you can go ahead and change it, extend it
08:08
and do what you want with that. So there we go, a really, really long introduction, but that is what we're gonna be building. Let's go ahead and get started on building this out.
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!

Episode discussion

No comments, yet. Be the first!