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
11. Adding plans

Transcript

00:00
Okay, so we're now moving on to actually checking out with Stripe, so we can actually create some payments. But first of all, we need some plans that we can actually list through and show to the user.
00:09
Now, this isn't strictly related to the referral system, but it does, of course, come into the wider picture. Okay, so first of all, I've gone and created out a new account on Stripe just to test this out. And I've gone ahead and created out a product here, which is monthly, and we have a monthly price. So you could create an overall product and have lots of different prices.
00:31
But the key thing here is when we're copying and pasting values over to our database, we need this price ID. That's the most important thing. So we're not gonna worry too much about this now, but let's get the schema working so we can actually iterate through the plans that we have. So we'll go ahead and create out a new model in here.
00:48
And of course, we're going to call that plan and we'll create a migration with that as well. So let's create this plans table out and see what we need to add. Okay, so the first thing is just going to be a title for the plans. That will be something like monthly, yearly, whatever it is.
01:04
And then we're going to have a slug in here, which we can pass through to URLs. Now, because this is a slug, we kind of want this to be unique. That's important. And then we're going to have a string in here, which is going to be the Stripe price. That's the thing from the API. So when we pass this over to actually check out, we know which plan we're signing up for.
01:25
Okay, so the next thing is just going to be the actual price of this. We're going to make this an integer. The reason being is that we don't store float in the database for prices. That's a really bad thing to do in terms of precision and how things are displayed. But we're going to be putting in a package to display this properly from the cents or pence value.
01:45
So we're just going to call this price. Okay, I think that's pretty much it. Let's go ahead and migrate this and then we'll just start to fill this in. So if we head over to our database, let's go over to our plans table and let's create a new row out for the monthly plan. And we'll fill in a slug, which is going to be monthly. The Stripe price here, we're not sure of yet.
02:04
We'll copy and paste that over in a minute. And this one happens to be 100. So, of course, that's going to be 10,000 cents. And we'll just manually fill in the created at and updated at value. Okay, let's pull this open a little bit and let's grab that Stripe price over here.
02:21
So we know what we're linking through to. Okay, so let's pull that into there and we're pretty much done. Great. Okay, so let's go ahead and iterate through these on the dashboard. So we'll head over to our dashboard controller and let's just output all of the plans that we have in our database. I'm not really going to bother ordering these or doing anything special with them.
02:40
We're just going to go ahead and grab them all. So over on the dashboard here, let's go and output all of these plans. So let's do a for each on plans as plan and that for each here. We're going to have a link which will eventually go through to our Stripe checkout.
02:57
We're not sure what that's going to be yet, but let's add in the plan title in here and then in brackets or somewhere. We'll add in the plan price, which at the moment is going to be the cents value. So it's not going to make too much sense. Let's go over here. And there we go. Great. Okay, so to get these values formatted
03:17
and this is going to be really important for the rest of the course because of course when we're dealing with payments, we want this to be accurate. We want this to be formatted properly. So important that we get this right. We're going to go ahead and use the Laravel money package.
03:31
So let's get this installed and then I'll show you how to use this. So let's pull this in. You can publish the config if you want to change around the default currency or anything like that. I'm not going to do that because I'm just going to assume we're working in dollars here.
03:44
Okay. So now that we've got this package installed, this is very very easy to use. We're going to head straight over to the plan that we've got here and we're going to cast the price to some class that comes back from this package. So we know that we're working with a decimal.
04:00
So what we're going to do in here is add in a cast for that price column. So let's pull this in here and this is a money decimal cast and that is pretty much all we need to do. Now don't worry too much about this pricing stuff yet.
04:14
We'll look at how we can access this in different ways a little bit later. But what we should now have when we refresh the page here is a implicitly formatted value since we're just dumping this out on the page. There are a ton of other methods that you can use to format this in specific ways.
04:29
If you want to and configure it, but really at the moment that is great. We've just got $100 value from the cents value in the database and we can see the plan that we can go to click through to. So now what we need to do over the next couple of episodes is set up Laravel Cashier
04:45
so we can actually generate out a checkout link for this particular page. We don't need to use Laravel Cashier, but it's going to be really helpful because when we get our webhooks back from Stripe when a payment comes in,
04:56
Cashier is going to allow us to very easily detect them webhooks and everything will be set up for us. So let's go over to the next episode and install Laravel Cashier and then we'll eventually get to the point where we can click through here to check out with this plan.
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.