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
20. Scheduling payouts and marking as paid

Transcript

00:00
OK, so let's just talk a little bit
00:01
about how the process of payouts is going to work. Now, if we just take a look at our database schema, we've only got one payment in here at the moment. But we know that this can be paid at this particular date.
00:13
So really, all our query to find payments that need to be sent or totaled up are going to involve is having the available date less than the current date. So if we are in, say, February of 2024, this can be paid.
00:30
And then we want to make sure that paid out is null. We don't want to repay out multiple records. So that is really the condition. And in this episode, we're just going
00:39
to create the schedule command to run on the first of every month to find payments that need to be paid and just mark them as paid. And then what we can do after that
00:48
is we can take all of the payments that we've just marked as paid, and we can create that CSV or that spreadsheet to then send to us to then pay out manually.
00:57
OK, so let's get started on making this out. So we're actually going to make this out as a job. And we're going to call this generate referral payout. Great.
01:07
OK, so if we come over to generate referral payout, let's go and just die dump something in here and just say pay out. And then we want to schedule this.
01:17
So to do this, we can come over to our console kernel. And we can add in a scheduled event in here. And I'll show you how to run them in a minute. So we're going to say schedule job.
01:28
And we're going to new up a generate referral payout job. And we're going to do this monthly on the first of every month at a specific time. Now, of course, you can change this.
01:42
You might be paying biweekly. It doesn't really matter. But we're going to pay this out monthly. And this is when we're pretty much going to send the email.
01:49
OK, so now that we've done this, let's look at how we go ahead and run this command. And you're going to want to set this up when you launch this into production
01:56
to be a scheduled cron job to be run properly. So we're not going to go through that. But depending on the service provider that you're using, you just want to make sure that this command is
02:05
being run every minute. So this is php artisan and schedule run. That's pretty much all you need to schedule to be run every minute.
02:15
And that will take care of sending that every month. OK, so obviously, this isn't actually running anything at the moment. So what we're going to need to do
02:23
is manually go ahead and dispatch this so we can test it out. So to do that, we're going to use tinker. And we can even use a root in our application.
02:31
Sometimes I like to do that. And it gives us back best errors. But you can do this either way. In fact, let's go and create a root for this.
02:38
I much prefer doing this. Or you can use something like tinker well, which is a lot more suited to this. So I'm basically going to take this job.
02:45
And I'm going to go and create a new root just to test this out. So let's create this temporary root just down here called payouts. And let's create a closure in here.
02:55
And then we can just dispatch this. So we can say generate referral payout and dispatch. That's pretty much all we need to do. So let's pull that in here.
03:04
We don't need to pass any arguments through to this because this job is going to be responsible for collecting all of these up. And we can pretty much just go over to a new tab
03:13
and say payout. And there we go. Great, we get that payout died and dumped. So this is going to be helpful for seeing the output
03:20
of this command as well. OK, so inside of handle, we first want to collect up all payments that need to be marked as paid. So let's create out a variable here called payouts.
03:30
And of course, we can access our referral payment class. Let's go ahead and do query on this to pull it down to a new line so it's neater. And we are eventually going to be using this
03:41
to build up this entire payout. So we're going to say with user just to make this a little bit easier. And now we're on to the conditions.
03:49
So we're going to say where available at is less than or equal to the current start of day. So let's pull that in and make sure we fix that up. So we want to say start of day.
04:05
This is really important because our available at includes a time. So we don't want to miss any payments. We want these to be on this specific day
04:12
at the start of the day. So that's really important to include. And the other condition that we spoke about is where paid out is null.
04:21
Again, really important. Otherwise, we're going to be paying things out over and over again, even if they've already been marked as paid.
04:28
OK, so now that we've got this, let's go and just add a really quick condition in here. Because down here, we're going to be sending the email to us as an admin to go ahead and pay this out.
04:38
So I'm going to say if payouts count is zero, then I just want to return. We don't want to do anything past this. We don't want to mark them as paid out.
04:47
Down here, we don't want to send the email. There's no point if there are no payments to pay out. So to mark them as paid out, that's pretty straightforward. We just want to do a mass update on our payouts.
04:59
And we want to make paid out set to null. Now, sorry. And that's pretty much it. So there we go.
05:06
Grab all the payments that need to be paid out based on the available date and whether they've already been paid out. If there are no payouts, return.
05:13
Otherwise, update the paid out date. OK, so if we go ahead and run this now. So let's go over to here and give this a refresh. Sure enough, paid out is not set to null
05:24
because this is only available on the 22nd of next year. So what I'm going to do is I'm just going to change around the date here to be a month earlier than what we're already in.
05:35
So let's say the 22nd of the 11th. So now when we run this command, this record will be included in the payouts. And of course, you'll probably have multiple payouts in here
05:44
as well. And then paid out should be marked as true. So let's give that a refresh. Let's come back over.
05:50
And there we go. Because we included just the date in here, it's filled the date that it was paid. And that's been paid out now technically.
05:57
Now, obviously, it's not being paid out at the moment because our next job is to generate out a spreadsheet with all of the payments that we need to make to 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.