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
19. Logging payments

Transcript

00:00
Okay, so I'm gonna go ahead and clear everything out
00:01
from the database, all of the subscriptions that we currently have, everything that's tied up together. And now we're gonna look at logging payment when it comes in from Stripe,
00:11
so we can calculate when to pay this and what we need to pay. So let's get rid of all these subscriptions. Of course, this isn't quite gonna work
00:18
because we've got this relationship here, little bit annoying, but we should be good now. Okay, so if we head over to our referrals dashboard, of course, we've got no conversions, no one's signed up yet.
00:29
You'll also wanna make sure that you've definitely got your exposed local environment, and I'm actually gonna cancel this off and rerun it because we're running out of time here,
00:38
and we should be good to go. Okay, so where are we gonna log this payment? Well, we're gonna look in our Stripe event listener. So this was created when the,
00:46
well, we created that record when the subscription was created, but now we wanna do the same thing for when a payment rolls in.
00:53
So we are going to listen under the, I think it's invoice and payment underscore succeeded, and then we're gonna say handle payment succeeded, and pass through the payload.
01:09
Okay, so we can just create this out in here like we did before for the other one, receiving the payload, and once again, I'm just gonna log out the payload to make sure that this is actually being received in,
01:24
and I think you should be good. So let's clear out our Laravel log, go ahead and sign up for another subscription using this referral code and see what we get in our logs.
01:33
Okay, let's hit subscribe and just wait for this to come through. In the meantime, head over, and yeah, everything's being done properly, great.
01:40
Okay, so now that we've got this, what we can do is grab the subscription and then get the referral code from that subscription. So we know that because we've got that tied up now,
01:51
with the subscription has a referral code attached to it. So I'm gonna do again, all of this within a retry, five times just to make things nice and easy in case anything goes wrong.
02:03
And in here, we wanna grab the subscription out. So again, we can use this, what do we call it? Get subscription by Stripe ID,
02:12
and we'll bring our payload into scope here. So let's pull that payload into scope and we'll pass the subscription ID into here via that payload.
02:22
So we are going into data, object and subscription, and that should be good. Okay, so now that we've got the subscription, we wanna grab the referral code from a subscription.
02:33
So if we head over to our subscription model, we're gonna set up a relationship in here and say referral code, let's call that. Now, the problem with this is that we have
02:44
a belongs to many relationship. A referral code can have many subscriptions. So we could sign up many people using our referral code, but a subscription is only ever gonna have
02:56
one referral code. So that's just the nature of this relationship that we've set up. It's probably, in my opinion, the best way to do things
03:03
the way that we've done it, so we can very easily store that multiplier. But in our case, we just wanna go ahead and create our relationship
03:10
and grab the first referral code for a subscription. So we're gonna say belongs to many in here, and it belongs to a referral code like so. And again, we want the pivot in here.
03:22
So we want that multiplier so we can work out when we insert the payment, how much we need to give. So let's say multiplier, and we should be good. So we've set this as a singular.
03:33
We could do referral codes. That would probably be better, actually. And then in here, we can now say referral code, and we can assign that from the subscription,
03:42
the referral codes, which will give us multiple items, but we know that a subscription doesn't have multiple referral codes. And we can just say first.
03:50
So that's absolutely fine. Okay, so now that we've got both of these things, let's log these out, go through the subscription process again and see what we get.
03:57
So let's say log the subscription, log the referral code, head over to our Laravel log file, get rid of that, and we wanna get rid of the subscription as well. So let's get rid of the relationship here,
04:09
get rid of that subscription item and the subscription, and let's go through and see what we get. Okay, so let's hit subscribe. Just while that is working away,
04:18
we'll head over to our Laravel log and check this out. And we get an error. Okay, this is absolutely fine. The reason that this is happening
04:24
is because when we look up the subscription in here, we've not updated that to use our subscription model. So let's just go up to the top and get rid of the subscription here,
04:35
and then we'll just re-import this as our model from here. So we'll actually have that relationship. Okay, so let's head over to our Laravel log, get rid of all of this, and of course,
04:44
we'll just go through the whole process again. So let's get rid of the relationship here, get rid of the subscription item, get rid of the subscription and try one more time.
04:54
Okay, so I'm gonna hit subscribe. And again, we'll head over to our log and just wait for that to roll in. And we should now get the referral code,
05:01
which is just not working because it should be a type array, which is fine. We've got the referral code and we should have the subscription in here as well.
05:10
It's errored, but we're gonna assume that both of them came in because we're logging the referral code from here. Okay, so now that we have got this information,
05:19
we wanna create this payment out. And actually, I think the reason for that is because we were accessing that as the relationship method and not the actual relation.
05:28
So let's go ahead and change that over and we can always fix this up later. All right, so let's go and just grab the referral payment and we're just gonna create this out in here straight away.
05:40
We're not gonna create this via any kind of relationship. So we're gonna say referral payment and we'll make sure that we set guarded to an empty array. And then we're gonna go ahead and create this,
05:54
but we're not gonna create this just by using create like so. Remember I said earlier that we have a webhook which could be fired multiple times.
06:05
If this gets fired multiple times and we just use create, we are gonna create duplicate payments which will then, when we run our scheduler, get paid out multiple times.
06:16
So instead, what we're gonna do is say first or create. Now, what's the unique thing in this table about each of these payments? Well, like I said, it's the Stripe payment ID.
06:26
So basically, create this data in here if this doesn't already exist. So let's fill all of this information in. Okay, so the Stripe ID comes from our payload, of course.
06:41
So let's go ahead and grab the data object ID from here. So that will get filled, but then the next time around, if this already exists at this value, this won't get created.
06:53
It will just bring the first one back. Okay, so the rest are pretty easy. It's just gonna be the user ID, which is the referral code user ID.
07:02
I'm just gonna make sure that we have that hooked up to a user and we do. The second one is the referred user ID. So let's fill that in.
07:11
And that is gonna be the user who has the subscription. So on the cashier subscription model, we have a user relationship. So we can just grab the user's ID.
07:20
The payment total here, again, comes from Stripe. So we're gonna use our array helper to grab this data from this payload. And that is gonna be data object and total.
07:36
And then we want the amount that we are paying out. So if we assign this up here to the total, we can then work this out down here. And it's basically gonna be
07:48
the total multiplied by the multiplier. So if we, from the referral code, grab the pivot. So remember, because we've looked the referral code up here, we have access to this pivot,
08:00
which will give us the multiplier. So we can just say pivot and multiplier. But with the total, what we wanna do is round this up. So let's round the total up, multiply it by the multiplier,
08:11
and that will give us the percentage that we should be paying. Now we have available at, which is totally up to you. But I'm gonna say now, end of day.
08:22
So that will take us to the end of the current day. And then we're gonna add on a month. Okay, so we've filled in all of this information. Let's go ahead and run through with another payment
08:33
and make sure this gets stored properly. And more importantly, it's only gonna get stored once. Okay, so again, over to the database, let's get rid of everything that we've got in here.
08:42
And that should be good. Okay, so we're now gonna get two webhooks handled. The first one is to hook everything up. The second is going to be to lock that payment.
08:53
Now again, just before we do demo this, this is really important, because we know that we have a handler for the subscription being created
09:01
and the payment succeeded. Now, if the payment succeeded comes in first, let's just take a look at what's gonna happen. Well, it's not gonna be able to find the referral code
09:10
because we're not gonna have the link between them two. So that's why we use this retry to look this up properly. And we should probably say here first or fail. So if the referral code can't be found,
09:21
this fails and it will retry the whole thing. So we wanna make sure that these work regardless of the order that these come in, and also regardless of how many times they come in.
09:30
Okay, let's go over and just try this out. So I'm gonna keep an eye on my logs just to make sure that we don't get any errors and let's try this out.
09:38
So let's go ahead and subscribe again and just wait for this to finish. And there we go. So the moment of truth, we don't have any errors.
09:46
Let's go over to our database. We've got our subscription. We've got our subscription items. This is hooked up.
09:52
And now inside of our referral payments, we should have everything that we need. Great. So user ID referred, user ID one.
10:01
That's because we're using our own referral code at the moment. We've got the Stripe ID in here, which is that unique thing that we need to store
10:06
so it doesn't get created twice. We've got the payment total, which is the discount. We get 20% of the discounted value, although you could change that if you wanted to,
10:16
and it's available to be paid next year. So we're coming in 2023, and this is the first of the 22nd or the 22nd of the first next year.
10:26
Of course, paid at is null because we haven't run the script to pay this out yet, but that's it. We've now got a logged payment for our users.
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.