Back Button PHP

How to Accept Payments

No longer do we require brick and mortar shops to sell our products. Have you written a book? Great! You can set up a webpage to sell it in a matter of minutes. It's that easy at this point, as long as you have a touch of programming knowledge. In this series, we'll review the ins and outs of accepting payments and subscriptions through Stripe.

20 episodes
3:15:55 hrs
Start Series
  • Latest Episode: How to Reduce Conditionals in Your Views

  • Like this series? Share the love!

    1. Run Time 15:16 Free

      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!

    2. Run Time 6:23

      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.

    3. Run Time 10:07

      As you may know, I'm a huge advocate of the Vue JavaScript framework. In this episode, we'll take the code we wrote in episode two, and transition it over to a custom Vue 2 component.

    4. 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.

    5. 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.

    6. 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.

    7. 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.

    8. 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.

    9. Run Time 12:09

      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.

    10. 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.

    11. 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.

    12. 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.

    13. Run Time 3:25

      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.

    14. 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.

    15. 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.

    16. Run Time 9:51

      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.

    17. 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!

    18. 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.

    19. 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.

    20. 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.