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
18. Referral payments schema


So anytime a payment comes into our system with a Stripe webhook we want to log it because that is
going to first of all allow us to show the payments to the user but more importantly calculate what needs to be paid out and then send us as an admin an email with a spreadsheet in with all of the payments that we need to make. So this is a really really important table and it's something we need to make sure we get right and include all of the relevant information.
Okay so let's just go ahead and create this out. We're going to create this as a model with a migration so let's go ahead and make out a model here and we're going to call this referral payment. So let's create that out and we're going to really focus on the schema here to see what we need to include. So create referral payments table and let's think about
what we need in here. So this is going to belong to a user because we obviously need to know who to pay that's really important we need to grab their PayPal email address so we can include that in the spreadsheet. So we're going to have a user id in here which is pretty obvious but we're also going to have the referred user id so the user who was referred so we know if we need to include
them at all. So we need to constrain that to users just because we're not using the standard so we need to actually pass the table in because it won't be able to work this out. Okay so next up is going to be the payment id from Stripe so we can easily look that up if we need to. So we'll go ahead and include the payment id for that this is also really important because when we get to the
point where we're recreating or we're listening to our webhook and we're actually creating this record we do not want to create duplicate records and the Stripe payment id is going to be the unique thing in this entire table so we only want to create this once. Like I said if the webhook gets fired more than once and this creates an additional record in the database we might end
up with having to pay $40 to a user instead of $20 and of course we could remove that later but we just want to make sure it doesn't get in there in the first place. Okay so the next thing is going to be just the payment total that comes through from Stripe so we'll set this as an unsigned integer again we're going to store this in cents so that's going to be the payment total and then we're going
to have the amount that we're actually going to pay out to the user so of course you can change the names of these if they make more sense. Okay so the really important thing is a timestamp to for when this can actually be paid so we're going to call this available at now basically the way this is going to work is when this payment rolls in we're going to set available at to the
current date and time but we're going to add maybe a month on to account for any refunds or anything that comes in between that so we want to make sure that we don't pay this out instantly of course it's not going to work we want to add a month on to the time and you can increase this or decrease it it's entirely up to you. Okay so the next thing is going to be marking this as paid so once the
whole spreadsheet has been generated and sent to us we're going to mark this as paid obviously it's up to us to then actually pay the money out to the user but this is really important so we're just going to have a date in here to say when this is paid at so the reason that we're not including a timestamp is because we're going to pay on a day not necessarily on a day in time
so i just want this to be the day date that it was paid at not we don't really care about the time here so that's going to be nullable because of course it's not always going to be paid out and that's pretty much it for the schema so let's go and migrate what we have here and then we'll just hook this relationship up to the user so we'll add this into has referrals and let's create
a referral payments relationship and just say that this has many and referral payment now we're not really going to use this until we start looking at the stats because when we pull this in from our webhook all we really need to do is just create all this stuff out we're not going to look at creating this via relationships it kind of gets a little bit too complicated so
we'll just dump this information into the database when a payment comes in make sure we're adjusting the timestamps properly and we should be good to go so now that we've got the schema down hopefully all that makes sense but let's go over and actually look at receiving the webhook and creating this in the database
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.