Uncategorized

Getting started with Stripe Subscriptions

Cumulations.

02 Jun 2022

Subscriptions are perfect for any book memberships, gym memberships, donations & many other similar things which have recurring payments. To begin with, implementing stripe in any project is not all difficult now. Firstly you need to understand its flow and how Stripe actually works. Here we at Mobile App Development Company in bangalore will describe the functional flow of a Stripe subscription of a plan and Webhooks.

Before we move ahead, you should have little knowledge about the WebHooks on Stripe.

Webhooks:

WebHooks is just an HTTP Post Callback that sends you a notification if any new event occurs on Stripe.  In other words,  if Stripe Charges any customer, then WebHook will send a notification of “charge.succeeded” to the registered WebHook URL. Similarly it does for “charge.failure”, “invoice.created” and much more you can find it out on Stripe documentation. It may be a case that for a single payment charged by stripe, there may occur many events. Like as soon as charges have been deducted from the customer.  Stripe creates an invoice for that charge. We will get two WebHooks notifications one for “charge.succeeded” and other for “invoice.creation.succeeded”.

Steps to create webhooks:

  1. Identify the events you want to monitor and the event payloads to parse.
  2. Create a webhook endpoint as an HTTP endpoint (URL) on your local server.
  3. Handle requests from Stripe by parsing each event object and returning 2xx response status codes.
  4. Test that your webhook endpoint is working properly using the Stripe CLI.
  5. Deploy your webhook endpoint so it’s a publicly accessible HTTPS URL
  6. Register your publicly accessible HTTPS URL in the Stripe dashboard.

webhooks

Also, Stripe lets you pick what all events you want to get notified of, as per our requirement. Like you can only select an event of “charge.succeeded” so you will only be notified just for that particular event. By WebHooks, We can be assured of charge success and invoice created for a particular customer. 

To Subscribe to any plan on Stripe user has to follow below 4 steps in following order:

  • Get card details of a user (at this point user is not a customer in terms of Stripe) (No need API for this).
  • Create a card token from the above card details:
    • Creates a single-use token that represents a credit card’s details. This token can be used in place of a credit card object with any API method. These tokens can be used only once: 
    • https://stripe.com/docs/api/tokens/create_card

create card

  • Next create a Customer from above card token on Stripe

customer Creation

  • Now create a Subscription to a plan with above Customer id

subscriptionCreation

  • Before using Subscription you have to create some plans on Stripe. It can be created from your Stripe dashboard or from calling Stripe API
    • Log into your Stripe Dashboard and navigate to the Products dashboard
    • Click on the + Add Product button.
    • Add the relevant details like name of the Product, optional description, optional image , 
    • Add the Pricing model -> For subscription select standard pricing. 
    • Fill all the other details like Price , currency etc . 
    • https://stripe.com/docs/products-prices/pricing-models

 

Here you go now,  you can create stripe subscriptions keeping in mind the above four steps. All these steps can be accomplished by calling their respective API’s. 

Stripe as the source of truth

 Our code needs to know which user subscribed to which plan, is the payment made, is the user canceled the subscription, etc. The source of truth for all these details should remain in stripe.

  • Stripe webhooks are incredibly reliable for pushing necessary details to our application
  • We will be able to find and manage all the users from the stripe dashboard and we need very few lines of code to check if every user has a subscription of some sort. 
  • Stripe will take care of reminding about billing information, next billing data, etc. We don’t need to worry about them.