Charging customer without payment information on the further transaction

Stripe May 03, 2019

On our day to day basis, we do a lot of transactions. Let it be for buying socks, groceries, transferring from one account to another. We buy repeatedly from the same site. Notice you don't have to add the payment info again? Amazon, Digital Ocean, Netflix, etc all such website have subscription based items and we are charged on certain interval without adding any payment info again & again.


Today, we will learn how can we take recurring charge from our user using Stripe.

Key Terms:

Customer : Customer object in a stripe allows us to perform recurring charge for the same user. It also helps us to track the expenses for the same customer.

Token:When payment info(CVC, expiry date, account no) is provided to the Stripe, it provides us with a token number which is used to charge the user.

Step 1: Get the token

There are two ways of creating a token from the user payment info. I have explained in the previous tutorial about this.

- Using Card Element/ Bank Element using Stripe.js library (link to tutorial)

- Using Token Object (link to tutorial)

Step 2: Create Customer

Now we use Customer.create to create customer for our user.

import stripe
stripe.api_key = "your_stripe_token"

customer = stripe.Customer.create(
  description="Customer for [email protected]",
  source="token" # obtained with Stripe.js or Token Object
)

Step 3: Charge the Customer

Customer is created, now instead of using a token to charge a certain customer, we use the customer id to charge the user. customer id can be retrieved from   Customer.create .


we will charge $20 for the customer. amount parameter is the smallest unit available. So if $20 needs to be deducted. Amount key must have 20000 as the value.

stripe.Charge.create(
  amount=2000,
  currency="usd",
  customer= customer.customer, # obtained with Customer.Create
  description="Charge for [email protected]"
)

you also get an invoice URL from the stripe in the response while creating a charge. In the response, look for key receipt_url


the URL will redirect you to the invoice, which will be as shown in the example below:

Rohit Shrestha

have a keen interest in building web applications Currently, I work as a consultant for various health-related agencies to leverage the power of data and improve their system for maximum efficiency.