Customerobject 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.
SubscriptionsController, we might leverage form objects, as well as creating a dedicated
Subscriptionclass 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.
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.
Subscriptionclass 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'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.
resumemethod to the
Subscriptionclass. Stay tuned to the end for a bit of homework as well.
*Series still in development. Check back often for updates.