This episode is for members only

Sign up to access "Laravel Subscriptions" right now.

Get started
Already a member? Sign in to continue
Playing
07. Creating a checkout session

Transcript

00:00
Now that everything is set up, let's go ahead and actually go through to a checkout page. So to do this, we're going to need a URL that we can point to, and we need to be able to pass in the plan that the user wants to subscribe to. We're going to create one controller that will
00:16
handle any of the plan frequencies that you have within your product. Okay, let's get started with a controller. So let's go ahead and make out a controller here. And I'm just going to call this subscription controller. Let's head over to our web routes and register this out. So I'll just grab all of this, bearing in mind that at some point, we're going to need to protect these routes
00:38
in some way if a user already has a subscription, but we'll deal with that a little bit later. Okay, let's go ahead and just call this checkout. We will reference the checkout controller here that we've just created. I think we call that subscription controller. So let's go ahead and actually create our checkout controller out in here, and we'll go ahead and reference that,
01:01
and we'll keep the same index method. And of course, we're going to change the name here just to checkout. Okay, so heading over to the checkout controller, let's go ahead and create our index method in here. And we don't need a view for this because the only job of the checkout controller is to redirect us over to Stripe. So let's try that first of all, and then we'll see
01:22
how we can customize this by being able to pass in the plan. Okay, so we're going to go ahead and access the user in here, either by using auth and user, or we can bring in our request object, which gets passed in, and we can access the user directly from the request. Either way works fine. Now, remember earlier over on the user model, we added this billable trait, and we already saw a
01:48
little bit earlier when we looked at the manages subscriptions trait, that we have a new subscription method. This goes ahead and returns a subscription builder. Now, this is really important because the subscription builder allows us to chain on a bunch of things, like for example, if we want this subscription to have a trial, if we want to allow promotional codes, and we'll be diving into that
02:12
a little bit more in the next few episodes. So we want to set up here a new subscription. Now, this takes in two arguments. The first one is going to be the name of your subscription. You might have different types of subscriptions within your app, in which case you can give them very specific names. So just as an example, you might have a standard subscription type, which gives you access to some
02:36
features. That might be chargeable monthly and yearly, but you might also have, say, a gold subscription, which gives you a lot more features, and that might also have a price for monthly and yearly associated with it. For us, we just are going to have a standard default subscription type, so I'm going to go ahead and use the default value of this as just the string default. The next
03:00
argument here is the price ID, which we know we are storing. Just for now, let's go ahead and hard code one of these in. So I'm going to go over to our subscriptions here, and I'm going to say that we want to sign up to a monthly price. Okay, so now that we've done this, what we can do is we can generate out a checkout session link using the checkout method. Now, this requires an array with
03:25
at least these two arguments. Success URL, which is where the user is going to be redirected back to when they have successfully purchased a subscription. We know that we can just redirect them back to their dashboard once they've done that if we want to, and we also have a cancel URL as well, which is the URL that we use when either the payment is cancelled or the user clicks
03:49
back to our app. For us, again, we can just redirect to the plans page, which we should have come from. Okay, so by using this method, this is now going to return to us a URL. So let's go ahead and check this out. Let's just give this a value of URL or a variable value of URL just for now, and let's just die dump on this URL to see what we get. So if I come over here now, we're
04:12
going to hook up monthly to click through to this page. Obviously, it's not going to work properly at the moment because we are just hard coding in monthly, but we'll set that up in a second. Okay, so this by plan under monthly, let's go ahead and hook this up to now go through to our checkout, and we should be good. Let's head over and try this. Okay, so I'm going to click by plan,
04:35
and let's go over, and there we go. So what's actually happening here when we do this, it's generating out a session, a checkout session over on Stripe for us. That's why the request took a little bit of time. Okay, so instead of dying and dumping on this URL, what we're going to do is just return this. That's going to be converted into a request with the URL as the string that
04:56
will be forwarded over. So let's go back over and click by plan now, and we should be redirected over to Stripe with the product that we created and with the price that we created as well, and we can enter all of our card information here, hit subscribe, and that will create a subscription on Stripe for us. Now at the moment, this won't be reflected in our app. That comes a
05:19
little bit later with webhooks, but now at least we're going through to checkout. Okay, so this isn't great at the moment because we're hard coding this in with the monthly plan. Let's look at how we can customize this slightly. So over on the plan section here, ideally what we want to do is pass through in the query string the type of plan that we're subscribing to, and we'll do that
05:42
by the key that we set inside of that subscription config. So monthly and yearly. Let's do exactly the same thing for yearly, and then we'll look at plucking the plan out. So let's change the URL here over, and of course swap this over to yearly. Now that we've done that, what we can do is over in our checkout controller is pluck out the plan that the user is trying to subscribe to
06:05
from the config that we have inside of our subscriptions plans. So we're going to do pretty much exactly what we did over in our plans controller. We're going to collect up this config, so that turns that into a Laravel collection, and then we're going to go ahead and say get. Now in this case what we can do is from the request pass in the plan that we get from the request within
06:27
the query string, and that should pluck out the correct plan. Let's try this out by dying and dumping plan, and we'll see what happens. Okay, so I'm going to click monthly. We get monthly, and we'll click yearly, and sure enough we get yearly. So now all we need to do to pass through that new subscription method is pluck out the price ID here. Let's do that now. So if we come
06:48
over here and change this up here to plan and price ID, we should now be choosing the correct plan to generate over on Stripe, then go through to checkout. Okay, let's try this out. So I'm going to go back and I'm going to hit monthly. That should take us to the monthly plan. Let's go back here and choose yearly, and yeah sure enough we can now choose between both of them.
26 episodes2 hrs 38 mins

Overview

Learn to start accepting subscriptions with Laravel and build a solid foundation for your next application. In fact, it’s exactly how we do things here on Codecourse!

Even if you’re completely new to Laravel, by the end of this course you’ll have a platform where customers can purchase subscriptions and gain access to restricted areas of your application.

We’ll also cover managing subscriptions, cancellations, invoices, giving customers trial periods — and more.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!