This episode is for members only

Sign up to access "Laravel Subscriptions" right now.

Get started
Already a member? Sign in to continue
Playing
13. Redirecting to the billing portal

Transcript

00:00
Now that we're subscribed and we know how to check if we have a subscription,
00:03
let's go ahead and create out a billing area that the users can access within our application to see some details about their subscription and also go back over to Stripe to manage their subscription as well. Things like canceling, updating their payment details and anything else that is offered. Okay, so we'll start out with our controller which I think we already
00:24
created earlier. Let's just have a look in here. We do have a subscription controller which we can use. So let's start out with a root group in here. So let's go ahead and create a new group because we're going to have a bunch of roots that we want to register in here and this is going to have a prefix of subscription. So all of the roots that we add in here are going to have that prefix
00:46
of subscription. Let's go ahead and create the closure out in here and before we forget let's make sure that we apply some middleware to this because this is going to be authentication only. Users have to be authenticated to see their subscription details. Okay, so we'll start out with the most basic which is going to be a get root to forward slash which will be slash subscription
01:09
and that will use our subscription controller which we've already created and let's go ahead and wrap this and reference some sort of index method just inside of here and let's give this a name of subscription and we'll go ahead and create that method out and hook this up in the URL. Okay, so let's go ahead and create out an index method in here and let's return a view here.
01:33
Let's put this in a subscription directory and call it index. Okay, so let's go ahead and create out a view for this. So make view and we'll call that subscription and index and once again what we'll do is copy over a template but we're going to copy over the profile edit section here. So let's just take a look at this. This just gives us a title and then includes a bunch of sections.
01:59
This is exactly what you see over here. So this is the kind of thing that we want. So let's go ahead and copy this and we'll put that over in our subscription index template that we've just created and let's paste that in and what we can do is get rid of a few of these and this one is going to be our subscription details.
02:23
So let's give this a title of subscription and let's go ahead and hook this up within our navigation. So let's find the logout link and this will just go just before profile or just after profile. So let's hook this up to subscription and index and let's change this over to say subscription. Okay great so now that we've done that it looks like subscription index is not defined.
02:51
I think we just call that subscription actually so let's just change that over. Yeah there we go. So if we head over to subscription now we get subscription and we can add in any of the subscription details in here. Let's go over and look at the structure of what we have here under our resources and views and for the profile you can see that we have the edit page which we've
03:11
just copied over but we have a bunch of partials. So let's just grab one of these it looks like this it has a header a description and then a bunch of information and let's copy this over and create a partial for our subscription section as well. This will just help to keep everything organized. So I'm going to create a partials directory in here and then in here we're going to have a
03:32
new file called details.blade.php and this will be all of our subscription details. So let's just change this over to subscription details and we'll just say these are the details of your subscription we'll just keep it super simple. Let's get rid of the majority of this so we can get rid of this form and I think all of that can go actually yeah so let's just get
03:57
rid of all of that and create a section in here for the details. What we can now do is just include this partial over on index just underneath here and that will just help to keep everything nice and tidy. So let's go ahead and include our subscription section partials and details and we'll end up with something that looks like this so very similar to the profile section just here
04:19
and we'll have all of the information just down here. Let's also tidy this up a bit so let's go over to partials and any of these and just see what the styling looks like on here so it's got a margin top of six so let's copy that over to our details as well on this container and that's going to space that out nicely. Okay so first job is just checking if the user doesn't have
04:44
a subscription so we already know how to do that and we can do that directly within blade as well if we want to. So we can say if and we'll use auth and user and we'll say subscribe so if they're not subscribed we're going to go ahead and show a message to choose a plan so let's go ahead and use our x primary link button once again and in here we'll just say choose a plan this will be
05:12
for people that don't have a subscription yet but have come through to the subscription area and we know that we want to link that over to our plans index let's go over to just our plans page okay that should be enough for now let's just check this out by just undoing the falsy check here and that's what it's going to look like if a user is not subscribed now if they are subscribed
05:37
of course we're going to want a similar if statement up here but we're going to check if they are subscribed and then we can show anything to do with their subscription so we're going to show some subscription details in here but the thing that we're focusing on in this episode is linking through to stripes portal so how do we do this well we're going to need a standard link
05:55
which goes through to generate a link for the portal kind of like what we did with our checkout so let's bring in our x primary link button as well and let's say manage your subscription now we don't know where this is going to link through to just yet but this is what it's going to look like so i've signed up perhaps i want to change my card details i want to be able to click on this
06:16
to do that so how do we do this well we're going to need another controller for this so let's go and just take the subscription controller that we already have and let's create a new method out in here that generates a link to the portal so for now let's just return and do nothing in here and let's hook this up in our web routes okay so we're going to grab this here and we'll call this subscription
06:39
portal let's reference the portal method that we have just created and let's do forward slash portal so we now have somewhere that we can link to let's go ahead and link that up in here so let's go and generate a route out to the subscription portal and we should now be able to click on this to go through to that new route so now we've got to figure out how do we actually get
07:01
to this subscription portal over on stripe again what we're going to have to do is let laravel cashier generate this url by sending a request to stripes api that sounds more complicated than it is it's actually very easy to do so all we need to do in here is again access the currently authenticated user so either by the request or just auth user and we just use the user and again
07:26
because we have that billable trait on here we have a redirect to billing portal method now the argument that we pass in here is going to be the page that we want the user to be redirected back to if they click back once they're over in stripe so we can use our root helper here to just redirect the user back to the page that they're currently on okay let's try this out and see what it looks
07:49
like so i'm going to go ahead and click manage your subscription and yeah sure enough at the moment we can't create a portal session with the stripe api in test mode until we save our customer portal settings so let's go over to this url and do that now so let's go back over to stripe paste this in and let's have a look at what we need to do okay so let's come down here looks like we need
08:11
to activate this link and we should be good here now that's just a test one and i don't think we need to do anything else in here let's just retry this so we can either refresh that or click through again okay so this time it looks like it's worked and here we go so you can see that i'm currently subscribed to the $15 a month plan i can go through to cancel this i can update my payment
08:34
details i can see my billing information here i can see my invoice history although these invoices might not be suitable we might want to do that over on our end as well but you have the ability to do anything in here and of course when we return back that's going to go back to the page that we specified when we use that redirect to billing portal method
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!