Integrating Stripe on Backend- (Part 1)

Vignesh Jeyaraman
3 min readFeb 13, 2020

--

Hello Guys, this is my first story hope you guys like it. So let’s start.

** All piece of code I write here will be on Python but Stripe support various languages including Ruby, Java, Go, etc.

Stripe is one of the most used payment gateways that we have. One of the best things that I loved about Stipe is their documentation, It is the best and one can easily understand by reading it.

Stripe is available in around 34 countries and it is growing. You can check whether your country is supported or not by clicking here.

There are two types of events
1. Charges

2. Subscriptions

The procedure for both is the same with only slight changes. Let’s discuss each of them one by one.

Before explaining the above two events let me explain what is the sequence of operation we have to do with Stripe and what all detail will be stored in the database.

  1. Customer:- The first piece of code we have to write is to create a customer on Stripe. We can place this logic when we register the user on our application.

Stripe provides SDK for Python using which it is just a function call to use various Stripe services.

Once we create a developer account on stripe it will provide a secret key that is needed to call all the functions.

First, initialise the stripe with the secret key we got.

import stripe

stripe.api_key = Secret key

stripe.Customer.create(
description="My First Test Customer (created for API docs)",
email = email,
name = username)

If this call is successful then stripe would be returning customer id which we can access like customer.id. It’s better to keep this id on our database mapped against user_id because it will be needed in the future to add a card or create a subscription.

2.Card:- Once customer creation is successful next thing is to add their credit or debit card. The frontend will be using stripe.js to take customer card information in return they will be getting an encrypted token. Creating this frontend requires publishable_key which we will get along with sercret_key.

stripe.Customer.create_source(
stripe_customer_id,
source=token_from_frontend,
)

** First added card will be the default one, that is all the subscription amount will be credited from that. If at any point of time customer want to change their default card we have write below logic to do that.

stripe.Customer.modify_source(
stripe_customer_id,
default_source= card_id)

Once the above call is successful stripe will return card id as well as something called fingerprint. We have to save both on our database.

But what happens when customer try to add same card again??

Great question, right I know, I know!! Thank me later :P. Remember I mentioned above that once the card is added successfully, Stripe returns card_id as well fingerprint. Here this fingerprint will be the same every time the customer adds the same card. So we have to check whether this fingerprint exists in our database if yes it means the customer has already added this card we then have to delete this card from Stripe as it allows the duplicate card to be added without throwing an error. To delete we can call delete card like below.

stripe.Customer.delete_source(
stripe_customer_id,
card_id,
)

3. Charge:- Now we have created a customer and added their card on stripe next thing is to charge their card by calling below function.

stripe.Charge.create(
amount=2000,
currency="usd",
customer= stripe_customer_id,
source=card_id,
description="My First Test Charge (created for API docs)",
)

** The amount here is in cents, it means if we want to charge our customer $20 we have to pass 2000 in amount.

I hope you find this informative. Stay tuned to learn about a subscription I will post about it soon.

** If you liked it give me a clap and leave some review it will motivate me to write more. Also, if you have any comments, please share.

** You can read part1 here

--

--

Vignesh Jeyaraman

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