This episode is for members only

Sign up to access "Laravel Subscriptions" right now.

Get started
Already a member? Sign in to continue
Playing
14. Displaying plan details

Transcript

00:00
If a user has a subscription, let's also go ahead and show some details in a list with information about their subscription. We'll start with the basics and then we'll go on to look at the upcoming invoice. So how much they're going to be billed next time and all of that stuff. Let's create out an unordered list, a list item, and then we'll just create out a strong tag here with the plan that they're currently on.
00:22
So this is going to be either monthly, yearly, or anything else that you've chosen. So at the moment, of course, we have just hard coded it in. Let's also go ahead and add in a space Y of six in here just to space everything out nicely. And let's figure out how we get this value.
00:38
Now, the problem we've got is we don't really know from our database which plan the user is subscribed to as we've defined it in our config. What we do have is we have a subscription ID here, which isn't too useful. And under subscription items, we have the stripe ID for the item itself. We have the product, but now we have the price.
01:02
So this is what we supplied in our config, which was required to create a subscription. Now, what we somehow need to do is map up the stripe price to the value in our config or in the database if we're storing it in the database and then extract the details out from here. So let's go ahead and look at how we do that now. It's a little bit complex, but once you've done this, it means that you can access any of the details about the subscription that you need.
01:29
So we'll do this at the controller level. Let's go over to our subscription controller here where we are showing this index and let's pass down the plan that the user is currently on. So let's just hard code this in for now. So let's grab the request out here so we can grab our user and let's say request user and subscribe.
01:50
So if they do have a subscription, then we want to show something. So let's just show monthly for now. Otherwise, we're just going to pass down null. So if they don't have any subscription, we don't want to show this information.
02:05
And the reason this is useful, although inside of the template itself over here, we are using this if statement. The reason that we do this is because when we choose the logic here to extract this information from stripe or from our database, we don't want this to happen unless the user is subscribed. So before we do that, let's just take a look at how we grab the details of the subscription directly from stripes API, but via the cashier package. So I'm going to go ahead and say request user will grab the subscription, which if remember that subscription model that we looked at earlier onto these, we have really useful methods.
02:44
And this is the same for the user as well as stripe subscription. We also have that directly on the user. So as stripe customer, and what this will do is it will just send a request to the stripe API and grab back the subscription object directly from stripe. Let's take a look at that in the browser.
03:03
If we give that a refresh, you can see that this gives us now the response back from the stripe API with all of the details that we could possibly need. So let's use, if we just have a look at here, let's use the information about the plan that we're currently on. If we just have a look inside of here, this gives us the price ID. Now we do have this stored in the database, but if you wanted to extract any other information from here, like the currency, when it was created, anything like that, I'm going to show you a really nice way to decorate this.
03:34
So you can easily create methods to extract this information out. Okay, let's go ahead and do that now. So I'm going to take this line here, cut this out. And what we're going to do is we're going to pass the response we get back from stripe into a decorator object, which will give us that payload in the object, but allow us to pull information out via some methods.
03:57
So just before we change this up, let's go ahead and create that out now. So inside of app, let's create out a new directory called subscription, and we'll just put any of our subscription custom functionality in here. Let's create out a class called stripe subscription decorator. So let's create this out and then into this, we want to receive into the constructor, that subscription object that we get back from stripe.
04:25
So this is under stripe subscription, and let's go ahead and pull that in as subscription and we'll make this protected. Let's just die dump this within the constructor, just so in getting the right thing in and we should be good. Okay. So over to our subscription controller.
04:41
Now, what we're going to do is if we are subscribed, we're going to new up that object that we've just created. So stripe subscription decorator, and we're going to pass in the subscription. Let's just pull this down a little bit. So it's a bit neater and we should be good.
04:57
Okay. So let's pull that down here. And there we go. Great.
05:02
So if we're subscribed, return this to us. Okay. So let's go over, give this a refresh and we should get die dumped exactly the same thing, but this is now happening inside of our decorator. What we can now do is we can create any methods we want in this decorator to return any information we want to extract from this subscription.
05:23
So let's get rid of this in the constructor because we're not going to need to use that. Let's say that we want to do grab the currency. So let's say that we wanted the currency out in here. Well, we can just extract this directly from the subscription object that we have through in here.
05:38
So let's pull out the currency and maybe you wanted to do a string to upper on this to uppercase it. Well, that's nice now because we're getting in the object from stripe, but we're not directly formatting this inside of our blade templates or inside of our controllers. We're just doing it in a single class. So now that we've got this, what we can do with this information is just extract this out.
06:02
So for example, if you did want to show the currency somewhere inside of your templates, now you now have a decorator to access their methods. So as an example, let's just dump this out in here and then we'll actually look at grabbing the plan. So in here, we're going to say plan and currency, and there you go. So now you've extracted that information from stripe, use the decorator.
06:26
So you don't directly work with the stripe object and you can plug any information out that you want from that response. Okay. So let's do this not with the currency. We'll leave that in as an example, but we'll do this with the plan.
06:40
So over to our decorator, what we need to do is read the plan from stripe, fetch this out from our config or from our database. If you're storing your plans in your database and then return that. So let's go ahead and create out a plan method in here, but now we'll just return monthly and let's go over and return this here. So let's say plan.
07:05
And actually this is going to be more like name or title, isn't it? So let's go ahead and change the name up just here to title. And that shouldn't make any difference. We're still using the decorator.
07:18
We've just hard coded it in. So to access the details from our plan, what we effectively need to do is over in the subscriptions config that we created. Find the overall object here or the array here via this price ID. We can do that very easily with Laravel collections.
07:37
So let's create out a helper inside of here called plan from price ID and let's accept in a string with the price ID. And let's go ahead and return. We're going to use the once helper here, because if we're accessing this multiple times, we don't want to keep doing this over and over again. So this will just cache this for us so we can access it over and over again in the same request lifecycle without having to do this multiple times.
08:05
So let's go ahead and return. We'll do exactly what we did before by collecting up from our config. Let's use the config helper here, these subscription plans. And then once we've collected them up, we want to use the first collection helper to grab out the first plan where the price ID matches.
08:25
So we can just use an inline closure in here, grab the plans out that we're looking for. And we're going to compare from our config, the plan price ID, which is what we defined and check where that equals the price ID that we are passing into this method. And we're going to need to bring that price ID into scope. So just kind of ignoring the once helper just for now, collect up our configuration and then grab the first plan where it matches the price ID that we're looking for.
08:58
But cache it so we can use it multiple times inside of this decorator without having to do this over and over again, just speed things up very slightly. Okay. So let's try and grab the plan from the price ID here and then extract the details we want. So in here, we're going to say this plan from price ID.
09:18
We know that the price ID exists within that subscription object when we dumped it out. And I think that was just ID and we'll check this out again, actually, because I've forgotten what it's called. So let's go ahead and say this subscription and just dump on that. And let's have a look here.
09:33
So we've got the subscription ID. This goes into a plan and then it's the ID of the plan. Great. So we know that we can say subscription plan ID, and then now we have got not the title, but we've got the entire object that we created earlier under here.
09:53
So we can just pluck out the name now. So let's go ahead and pull out the name from here and we should be good. There we go. We are on the monthly plan.
10:02
It's found that via the Stripe API, but looked it up in our own data that we have stored in config. Okay. So now that we've done this inside of this decorator, feel free to add in any other methods that you want to decorate in terms of the Stripe object that you get back. So you don't directly access that.
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!