FREELessons: 9Length: 50 minutes

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

3.3 Subscriptions and Coupons

In this lesson we'll start by setting up some subscriptions. Then we'll subscribe some sample users so we can look at how Stripe handles the subscriptions. We'll also be looking at subscription quantities, coupons, swapping subscription plans, and canceling subscriptions.

3.3 Subscriptions and Coupons

So now that we've processed our first payment, we can now dig into the meat of the cashier component, Subscriptions. Cashier managers stripe subscriptions with very minimal effort on our part. Not only does it handle the subscribing side of things and payments, they will also handle grace periods, trials, expirations, and coupons. All we really need to do is plug in some numbers and away we go. Just a heads up, but I've already gone ahead and deleted from test data from stripe from our previous lesson, so we're working with fresh numbers here. Now you might remember a few lessons ago, when we ran our migrations, we ran one for our stripe columns. Let's take a look at them again. At the moment, for all of our users, these columns are all set to null or zero for the stripe_active column. So how about we go ahead and set up a subscription for one of our users? First, we need to define the subscriptions themselves from within stripe. On the left, under SUBSCRIPTIONS, we can create a new plan, so let's create a couple of them. For our first plan let's give it an ID of monthly, this will be the unique identifier we use when referring to plan. We'll name it Monthly and we'll set the amount to 4.99. And the interval for this plan will be monthly. We're not going to worry about a trial period, so let's just go ahead and create the plan. Now let's add another one. This one will have an ID of monthlyPremium. We'll name it Monthly Premium. We'll set the amount to 9.99 and the Interval will be monthly. So now it's time to add a subscription to a user. First, in our routes file, let's make a post Route to a subscribe URI. In the Route, let's grab the authenticated user with Auth::user(), then we'll call the subscription method. We need to give this method the unique identifier we get our plan. For starters let's use the monthly premium plan, so that we can see how downgrading a plan works. Then we just need to call the create method and process stripeToken to it so that we can authorize the payment. And we'll simply return subscribed for one premium month. Again you'll most likely want to implement some sort of redirect to a confirmation page. So now I need a way for these to update this subscription. So let's add a route get to the subscribe URI. Let's grab the Auth::user, so that we can pass it along to the view. Then we'll simply return a view called subscribe with the user as the data. And lets go ahead and create a new file in resources views called subscribe.blade.php. We'll start with the basic HTML file structure as usual and we'll set our title. At the top of the page, let's add a heading saying Manage Your Subscription. Underneath, all we're going to do is simply copy the checkout form from our cart view, and we'll paste it into this new view. We're going to need to update the action of the form, so that it points to the subscribe URI. And for the amount, let's set it to 999, which is how much our premium monthly subscription costs. We'll just leave the e-mail as it is. Let's add the description back in and we'll set it to Premium Monthly Subscription. Okay, that's the view done. So now, let's just head over to our browser and we'll navigate to the subscribe URI. We'll click on the pay with card button and we'll fill out the form with the dummy credit card details. Once we are ready, we'll hit the pay button and wait for the ajax request to go through. Now once the request has gone through the form will be submitted and our API request is being processed to set the user's subscription. And it's done. If we now head over to our Stripe dashboard and refresh, we'll see that we now have a new payment and we've also gained a new customer. There is one thing that I'd like to point out here and that's if you're managing subscriptions and one-off payments with cashier, then you might notice an auditing when a user makes a one-off payment and then subscribes to your service. They will actually end up as two separate customers. Users that do not have a stripe ID in the database when making a payment are essentially treated as a guest. With a little bit of custom code, you can actually create a stripe customer for the registered user and then process their one-off payment. But you'll need to take this into account when creating a subscription for them as well. I'll be sure to include a branch and source code which demonstrates how we can achieve this. Anyway, back on track. Let's check what happened to our user in our database. If we open up MySQL Workbench and refresh the Users table, we'll see that the user now has an active Stripe subscription. So now that the user is subscribed, we can use several other handy methods when dealing with that user. For example, we could use the subscribed method to restrict access to certain pages. If we check with the account authenticated user this method will dump out true, because yes, they are subscribed. We can also check if the user has only given plan using the on plan method. If we check to see if they are on the monthly plan then it will return false, because we are subscribed to the monthly premium plan.. Or we can see if they have cancelled and are on their grace period by using the onGracePeriod method. Again, because this user has not yet cancelled, this method will return false. Another handy method you might find a use for is the everSubscribed method, which will determine if a user has ever held a subscription for your service. For this user, this will return true, because they currently hold a subscription. Next, let's look at how we can handle swapping out to another subscription. In our case we'll downgrade to the monthly plan. We're going to create another route to do this, where we'll simply assume that the user has authorized us to swap their plan. You'd normally want to put this behind a form and ask them if they're okay with swapping. What we need to do is grab the authenticated user, call the subscription method again, except this time we'll pass in monthly as the plan identifier. And instead of calling create, we'll simply call the swapAndInvoice method. Now what's going to happen here is when we hit this route the user's subscription will be swapped from the 9.99 premimum monthly subscription to the 4.99 monthly subscription. This means that the user's already paid us 9.99. So what we should see is a $5 credit to the user's account on Stripe. This will mean that their next invoice will deduct from their account balance before trying to charge their credit card. It's also worth noting that the swap and invoice method will sweep the line items. This keeps it tidier on the stripe side of things. If you'd prefer, you can simply use the swap method. The results will still be the same. After we swap the plan, let's just return swap to monthly. Now, let's try this out. If we navigate to the swap URI, the API request will be sent off, and we'll then be told that yes, we have swapped our plan to the monthly one. If we now refresh the Stripe dashboard, we won't see any change. If we instead visit our customers, and choose our customer from the list, we can see their payments. So far, this customer has made one payment of $9.99, which was for the premium monthly subscription. We can also see that the customer has a credit of $5. We can then see that the customer has an active monthly subscription. Following this we have the pending invoices, which this customer does not have, because we used the swap and invoice method. If we did not invoice the customer immediately, then we'd have a couple of lines here describing the subscription change and the related charges. Instead we have two invoice items, the first was our 9.99 payment and our second is a $5 credit. Below that is our next invoice which is listed as 4.99. If we click into this invoice we can see that the starting customer balance is $5, which means the total due for the next invoice will be $0. Excellent. So the next thing we're going to look at quickly is Subscription Quantities. At the moment, our implementation will be for something similar to a service that you pay a monthly fee for, such as Spotify. But imagine that we wanted to implement a service like web hosting, where a single user or a customer can subscribe to a plan multiple times. This is where our subscription quantities come into play. When creating a subscription we can call the quantity method before we create it and set it to whatever we want. When swapping to another subscription plan, by default the quantity will be maintained. However you can't pass a quantity to the swap or swap in invoice methods to have a new quantity set. If you already have an active subscription and you simply want to increase the quantity, then you can use the increment method. The same applies if you wish to decrease the quantity, you'd just use the decrement method. Both of these methods also have an AndInvoice equivalent, which will immediately issue an invoice after adjusting the subscription quantity. Now let's quickly touch on coupons before we finally look at cancelling a subscription. Coupon must be first created from within Stripe. So let's create one now. We'll give this coupon 50% off, it will be usable once and we'll call it special. Now, when we create a new subscription, we can use the withCoupon method to pass in the unique identifier, which in that case would be special. Let's try this with a different user, so that we can create a new customer and see the coupon applied. Now in our browser, we'll head to the subscribe URI and we'll enter in our details. Once the payment has gone through, we'll head to our Stripe dashboard and check out our customers. We'll click through to the new customer Straight away we can see that even though they subscribed to the premium monthly plan, they've only paid $4.99. If we actually click through on their invoice, we can see in the summary that the coupon has been applied. But what about existing customers that area already subscribed to a plan? Well they can use coupons too. In our route file, let's create a new route for the coupon URI. We'll simply grab the authenticated user and we'll call the applyCoupon method passing the special coupon identifier. This coupon will then be applied to the customer's next invoice, which the customer with the ID with 1 is already null due to them downgrading to the premium plan to the regular monthly plan. Once the coupon has been applied, we'll just return saying coupon applied. So let's see what happens. If we hit the coupon URI, we'll eventually see the coupon applied message. Now, back in and Stripe, if we navigate to our other customer we can see that their next invoice has changed. The coupon has been applied giving them a discount of $2.50 off the next invoice, which means they'll still have a $2.50 credit for the invoice after that. All right, so the last thing we're going to touch on in this lesson is canceling subscriptions. In our route file let's create a new route for the cancel URI. All we'll do is grab the authenticated users subscription, and we'll call the cancel method on it. Then we'll just return cancelled. Now, let's go ahead and cancel this user's subscription. Once it's done, we can check the customer's subscription details from Stripe. The subscription is still active, and will remain active until the original end date. Until that date the customer is on their grace period. Once again if we check to see if our authenticated user is subscribed by hitting that testing URI it will still return true. And we can now check to see if they're on their grace period and this should also return true. Excellent, and there's quite a bit more to subscriptions than what we've covered. However, I'll leave that up to you. I'd recommend you dig through the billable tray, but also the Stripe gateway class, which contains a lot of the methods you use when interacting with the strap API. In the next lesson, we'll take a look at how you can manage a user's invoices.

Back to the top