Integrating Stripe on Backend- (Part 2)

Vignesh Jeyaraman
3 min readFeb 13, 2020

--

Hey Guys, Welcome back, in our first part we have learned about what all are the steps we need to follow and what we have to save in our database to integrate Stripe successfully on the backend. Also, we have learned how we can create a charge. In case you missed reading it please click here and give it a read it will take approx 3 min and you will get a fair idea on how Stripe works.

Before moving on how to create a subscription let’s first understand what is Product and Plan.

Product:- We can call the product as a parent to plan. If we create a plan without providing product id it will create a new product each time. So, it is advisable to create a product on the Stripe dashboard and map its ID against all the plans.

To create a product go to the Stripe dashboard under the billing section go to the Products tab from there we can create a product. Stripe will give product id once it is created.

Plan: Plan is a child of product. We can have various plans under a product like Silver, Gold, Platinum with different amounts. We can create a plan dynamically like below.

stripe.Plan.create(
amount=5000,
currency="usd",
interval="month",
product= product_id,
nickname= "name of your plan")

** Amount here is in cents be careful about it

If above call is successful then Stripe will be returning us plan_id we have to keep that in our database.

4. Subscription:- Ah… finally. Now, we have a plan to which our customers will be subscribing.

Subscription is something that renews after a certain period of time. It is the same as our Netflix subscription.

To create a subscription we can write below piece of code.

stripe.Subscription.create(
customer=stripe_customer_id,
items=[{"plan": plan_id}])

If successful this will return subscription id which we have to save on our database.

I have said subscription is auto-renewable but how we will know that whether customer’s second-month subscription is successful or they cancelled it?

Well well well…. Stripe webhook comes to our rescue.

Webhook:- Webhook is where we provide an endpoint(localhost won’t work) against some events which we create on the Stripe dashboard. Stripe provides various events for which we can create an event but most generally we will be requiring two events when it comes to subscription.

  1. Payment_failed
  2. Subscription_updated

You can read about all the events that Stripe provides in the below links.

https://stripe.com/docs/api/events/types

Stripe will be calling our endpoint when it receives the above two events. It will be a post-call and it sends a JSON in the request body in that JSON we will be having a flag with name paid, it will be true if payment is successful else it will be false. Depending upon this flag we can decide whether we will allow the user to use our app or restrict their usage.

As always I hope you find it informative and I hope it helped you in your project. If so please give a clap and leave a review which will motivate me to write more.

** If you have any comments, please share.

--

--

Vignesh Jeyaraman

Engineer by profession. Love to write about random stuff sometime write geeky stuff 🤪