This episode is for members only

Sign up to access "Laravel Subscriptions" right now.

Get started
Already a member? Sign in to continue
Playing
15. Showing renewal date and next charge

Transcript

00:00
We're now going to take a look at showing the customer when their plan renews and the total of the next invoice. Now you might be thinking well we could get that from the subscription and we could output that in our subscription decorator. The only issue is the subscription object that we get back from Stripe doesn't include the information about the next charge necessarily
00:22
because this could vary. The way that Stripe works is we have an upcoming invoice which tells us when this is going to be billed and how much for. Now once again Cashier gives us a really helpful method that we can use to extract this information out. So let's go ahead and create a new upcoming value that we are going to pass down to our controller and once again we can access this
00:44
directly on the billable trait and this is upcoming invoice. So this will give us all of the information about the upcoming charge. Now you could again create a decorator for this. I'm not going to do this I'm going to just directly access this data. Let's just die dump on this and see what it looks like. Okay so we've got a Laravel Cashier invoice. So actually we wouldn't need
01:07
to decorate it because it's technically already been decorated for us by this internal object within the Cashier package. Okay so we've got an invoice object that's super helpful so let's dive into that and see what kind of things we have on here. So let's look for an invoice object under the Cashier package and let's look at some of the methods that we have. So you can see we've got
01:28
amount due, we've got any discounts that are applied to this and let's just have a look if there are any really useful ones. So we have got a subtotal that's going to be super helpful and we want some sort of date as well. So yeah due date we'll have a look in a minute and find this out. Okay so we've got this upcoming value let's add that to our details section of here. So down here we want to
01:54
check if this is enabled first of all or it actually has a value. So we'll say if upcoming. The reason it might not be is if the user has cancelled their subscription. So if they've cancelled they're not going to have an upcoming invoice so we want to make sure we protect against that. Okay so we'll do a similar thing here. Let's work on the renewal date first of all. So when this renews. So in here
02:17
we had that date method didn't we? So let's go ahead and say upcoming and date and just see what this gives us. Give that a refresh and yeah sure enough it has output the date. So I have a feeling this is going to be a carbon object. So let's say to date string and yeah it looks like it. So we can grab a much better formatted date string here and then perhaps we could show the time next to this
02:42
or really if we think about it the time here isn't important. So since we're working with a carbon object we could put in brackets the upcoming date and the diff for humans. That would be cool to show the user a week or a day amount of when it's going to renew. Perfect. So we've done the renewal date let's go ahead and work on the invoice subtotal that is just coming up or the
03:08
total. So let's go ahead and create our strong tags again and say next charge and let's show this in here. So let's open that invoice object back up again and just have a look at total and yeah we've got a total method in here which returns the total minus the starting balance that was paid. Yeah that sounds just about right. So let's go ahead and say upcoming and total and see what
03:33
that gives us and there we go. Perfect. So our next charge is $15. If we just dive into this total method. So let's open up that invoice again you can see this uses this format amount method which uses the cashier package or this cashier overall object to format this with the currency that we have provided in the invoice. This will format this if we just take a look at this method and
03:59
yeah it uses a money object here to take this and use the currency that the invoice is in. So that's going to be pretty accurate coming directly back from Stripe. Okay so there are just two examples but of course you can output as much information you want here from the invoice or from the subscription directly.
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!