In this opening episode, we'll leverage Stripe's Checkout integration to quickly generate a "Pay Now" button to sell our fictional book. Along the way, we'll discuss the basic flow for performing charges through Stripe. It's vital that you understand this flow, so pay attention!
You may find that you need more control over the checkout process. In this episode, we'll review the manual approach for loading Stripe's checkout modal.
Now that we've transitioned over to Vue, let's figure out how to dynamically populate the checkout modal's price and details, based upon a chosen product from a dropdown menu.
So far, we've only attempted single charges. However, in this episode, we'll migrate over to associating customers with recurring subscription plans. It's easy! To start, we'll add two simple plans that mimic Laracasts, itself: monthly and yearly.
When using Checkout, Stripe will perform a preliminary bit of validation on the provided card number. However, there can and will still be situations when the
Customer object is properly created, but the charge fails to go through. It's paramount that we detect these exceptions, and alert the user. We'll tackle that very thing in this episode.
When working with Stripe, it's paramount that you store a handful of columns in your database that indicate the user's customer id, their subscription status, and, if canceled, the timestamp for when their subscription-only access should expire.
In this episode, I'll demonstrate a few refactoring strategies that I often implement in my own projects. To clean up our
SubscriptionsController, we might leverage form objects, as well as creating a dedicated
Subscription class to manage any interaction with Stripe. In incremental steps, let's review the workflow.
It's paramount that you listen for a number of Stripe-specific events. For example, what if attempts to renew a customer's subscription fails? Though Stripe will attempt this charge a number of times, eventually, the subscription will be canceled. As such, it's vital that we have a way to listen for such an event, and deactivate the user's account in our database.
In this episode, we'll register a webhook endpoint with Stripe, and learn how to listen for events and fetch the associated payload. In the process, I'll also demonstrate a useful technique for registering local webhooks.
Now that we understand the basics of how to register Stripe web hooks, let's take a few moments to refactor the code to make it more enjoyable to work with and extend. If we're planning on responding to a number of Stripe events in the future, it's vital that we structure things as elegantly as possible now, rather than later.
I'm sure you're itching to dig into more of Stripe's main API, however, it's vital that we learn how to write the necessary tests to ensure that all billing code works as expected. You never want to be in a situation, where you deploy code one afternoon...only to be woken up in the middle of the night because your subscription sign ups are broken!
To begin, let's learn how to post to our Stripe webhooks endpoint, and then verify that the
whenCustomerSubscriptionDeleted method behaves as expected.
In certain situations, it's paramount that we physically tough Stripe's API to ensure that our code does in fact work as expected. In this episode, let's verify that our
Subscription class does make a Stripe customer, associates them with the proper plan, and activates their account in our local database.
If we want to maintain our current API, that allows us to call various subscriptions-specific methods off of
$user, then we might consider extracting a helpful
BillableTrait to clean things up. Let's tackle that very thing in this episode.
We should setup a dedicated table to store all incoming Stripe charges. Not only will this provide a fast payments list for the user, but it will also makes reporting and daily summaries easier to prepare.
It would be a pretty shabby service, if we didn't offer users the ability to cancel their subscriptions. In this episode, we'll write that very logic, while also preparing a test to ensure that everything works as we expect.
It's likely that your website will at some point offer coupon codes to new subscribers. Luckily, once again, Stripe makes this all a cinch. Let's review the basic flow - including the tests to ensure that it all works properly - in this episode.
View the source code for this series on GitHub.
We need to offer users the ability to resume recently canceled subscriptions. But before we get to that point, we must first learn how to determine if a user is currently on their grace period. Let's tackle that in this episode!
In this episode, we'll update our API to allow users to resume their subscriptions. This will require that we add a
resume method to the
Subscription class. Stay tuned to the end for a bit of homework as well.
In this video, we'll take a few moments to discuss clarity, double-negatives, and the potential they have to introduce bugs. Then, we'll setup a template for rendering subscription-only content, while providing the necessary call-to-action for guests and deactivated users.
In the previous episode, we constructed multiple conditionals to determine which set of HTML should be displayed. For our basic example, the outcome was simple enough. However, for real-life projects, you may find that this quickly bloats your views and makes them difficult to reason about. Instead, let me show you a useful technique that involves applying polymorphism to dynamically load the proper view partial.