This episode is for members only

Sign up to access "Laravel Subscriptions" right now.

Get started
Already a member? Sign in to continue
Playing
22. Subscription trials

Transcript

00:00
So it's really easy to enable trials within Stripe,
00:03
and therefore it's super easy to do this with Laravel Cachier. Let's do this now, but we'll take it a step further and we'll show the user how many days they have left in their trial.
00:12
And then, of course, what you can do is take what we've already covered with the webhook event, and you could potentially send an email to the user once their trial expires.
00:20
OK, let's go ahead and get started. So we're going to come over to the checkout controller where we added in hooks like this, Allow Promotion Codes. What we can also do is set the amount of trial days
00:34
here, which just takes in a simple number. So let's go ahead and say that we're going to give a four-day trial. To set up a trial, that is all we need to do.
00:43
Once this gets created, after four days the user will be charged and Stripe will send the webhook to update everything within Cachier. OK, let's go over and see what this looks like.
00:53
So I'm going to go ahead and choose the monthly plan. When we hit this area, you can see that we've got three days free, and then we charge on the fourth day.
01:01
So let's go ahead and enter our information in here and see what this looks like. Just before we do this as well, let's go ahead and clear out some of the excess subscriptions and subscription items
01:10
that we have in the database, just to make things a little bit easier. OK, let's go ahead and fill out these details, and we'll go ahead and subscribe for a trial
01:18
here and see what happens. OK, so we are back in our app. And if we head over to our subscription section here, you can see in three days from now this renews.
01:30
That's one of the benefits for using the upcoming invoice. If we were to use the subscription plus one month, this would show that we'd get charged in about 30 days. OK, so we can do everything that we would normally do.
01:42
We can cancel our subscription. We do get an invoice generated for the trial, but we can kind of ignore that for now. What I want to do, though, is show how many days
01:51
we have left in our trial. Now, all of this information is stored over in the database. So if we check the subscription here, you can see when the trial ends.
01:59
So we can use this information to show the user how many days they have left of their trial. And we're going to do that over in the navigation. But of course, you can put this anywhere.
02:09
So let's go over to where our settings dropdown is, and we'll just put this as the first item in the dropdown. Of course, it could go absolutely anywhere. So let's go ahead and create this out in here.
02:20
We'll set some text size, and let's set a margin on the right of 6 to keep this away. We'll set this to text gray 500. So what we want to do is say trial ends in x days.
02:33
Let's just take a look at that and see what it looks like. That will do for now. OK, so how do we grab this information? Well, we already know that we have that trial ends at value.
02:44
So we can just output it in here. But the key thing is we only want to do this if the user is actually on a trial, first of all.
02:50
So let's go ahead and wrap this whole thing in an if statement. And the first thing we want to do is kind of grab the subscription at the same time as if we're checking it's on trial.
03:00
Now, to check if a subscription is on trial, we just use the currently authenticated user, grab the subscription object, which we've already seen, and then we just have this on trial option.
03:11
So this will only be rendered if we're on a trial. Now, we are still on a trial at the moment. So you can see it does still show. But our job now is to actually show the date.
03:22
So we are going to assign a subscription up here just because we're working in Blade. We want this to be a global thing. You could use Blade partials.
03:30
But we'll just leave this as it is for now. So we're going to go ahead and assign the subscription here at the same time as checking if that subscription is on trial. And then what we can do down here
03:43
is access the subscription trial ends at date. Let's just leave it as it is for now. And then we'll format it in just a second. So obviously, that's just the date itself and not when it
03:55
ends. OK, so one thing I think if we log out here, you can see that we get a call to member function subscription on null.
04:02
That's just because we don't have a user. So just go ahead and make that conditional and see that. We'll do the same thing on trial as well. And we should be good.
04:14
And of course, you could wrap that entire thing in auth if you wanted to. OK, let's go ahead and sign back in with our account. And here we are.
04:23
Great. So let's figure out how to work out how many days are left of this trial. So what we could do with this is we
04:31
could use just ignoring this code for now. So let's just cut this out completely. We could use now, so that's the current date and time, diff in days.
04:43
And then we could paste in trial enter. Let's see what this looks like. And then we tweak it around if we need to. OK, yeah, so sure enough, we've got 3.9964 days.
04:53
That's kind of not what we want to do. We want to show this as three. So we could go ahead and just use the PHP floor function to get that down.
05:03
And of course, you can tweak this around depending on how you want to display this. If it's the last day of the trial, you could show ends today.
05:10
It's entirely up to you how you format this. OK, so there we go. We have enabled trials, but we're also showing how long we have left until we're charged.
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!