Part II

Hi Readers! This article is a continuation of my previous article on integration of “Payment gateway with the stripe”. As promised in my last article that I will implement the stripe with customer card information saved for future transactions. Before starting the article let me show you the topics we will cover in this article.

Topics we will cover:

Creating a stripe card token(covered in previous blog PART I, if you are new please refer to Part 1) Creating customer with stripe API Adding a card to the existing customer Get customer data from Stripe API Charge customer with card added to his account

Note: Before reading any further. I highly recommend going through my previous article to have a better understanding of the basics of Stripe.

Let’s begin

2. Creating customer with stripe API

Creating stripe customer means you are registering your customer with the stripe so that customer can manage their payment and do not need to enter the same information every time they visit your portal for shopping.

In order to create a customer with stripe, you need to make a POST request to the following stripe API Create Customer with its card token by sending form-urlencoded data request:

description (i.e a short description for adding the card to the customer account.) source (card token generated with previous blog step)

#1 Create customer postman API response

You can see the postman response that a new customer is created and now you can perform the rest of the operations with this customer token cus_xxxxxxxxxxxxxxxxx.

Note: Do not forget to add the Headers with keys mentioned in the previous blog, to your API request else you will end up with error and wondering why your approach is not working.Hope you will take care of this.

3. Adding card tokens to the customer created

To add a new card to the existing customer you need to create a new card token by again sending the card info to the POST API Create Card Token.

Once you get a success response use this new card token and add this token to the customer created in the last step. You need to make a POST request to the API https://api.stripe.com/v1/customers/{Customer token created in last step}/sources by sending form-urlencoded data request:

(Refer to screenshot)

#2 Add a card to the existing customer

You will get success response like the above screenshot and card will be added to that customer.

4. Get customer data from Stripe API

To get the customer account details, all the card added, payments made, etc. We need to make GET API request to https://api.stripe.com/v1/customers/{Customer id i.e cus_asda32hj4b}.

#3 Get Customer Information

5. Charge customer with card added to his account

Once you have added multiple cards to the customer then you can charge the customer on any card token they want their payment to be done. Once you are done with the above steps you can get customer info and list all the cards added to his account. Customer needs to select a card in the application GUI and on the basis of which a card token will be picked and used to charge the customer.

Charging a customer is similar as we have discussed in the previous blog but this time the only difference is that once you add a card to a customer account you can not create a charge on that card token without a custmer token.

You need to make a POST request to the API Charge Customer by sending form-urlencoded data request:

amount currency description source customer

#4 Charging a customer

Once the customer is charged successfully you will get a response like this in the above screenshot.

Postman collection for this implementation is on github repository and you can refer to that for more clarity.

That's all for this tutorial and stripe implementation you can explore more with Stripe Documentation.