Integration Stripe Payment Gateway in Spring Boot

Posted By : Rahul Chauhan | 29-Aug-2019
Integration Stripe payment gateway in spring boot.
 
1. Overview
Stripe is a third-party application which is used to send or receives payments over the internet and offers both client-side libraries (JavaScript and native mobile) and server-side libraries (Java, Ruby, Node.js, etc.).
 
here I am showing how to integrate stripe payment gateway in spring boot application and then later will charge the card for a certain amount using the Stripe API for Java.
 
2. Dependencies
Adding the following dependency to communicate with the stripe:
<dependency>
    <groupId>com.stripe</groupId>
    <artifactId>stripe-java</artifactId>
    <version>4.2.0</version>
</dependency>
3. API Keys
Before the integration of stripe and execute credit card charges, we need to register a Stripe account and obtain secret/public Stripe API keys.
 
4. Create Customer
Now the time to create the customer in stripe for the payment:
 
Map<String, Object> map = new HashMap<>();
map.put("email", user.getEmail());
Customer customer = Customer.create(map);
 
here we are creating a customer in the stripe payment gateway using the email.
after creating the customer we can retrieve that particular customer by using the following API.
Customer cust = Customer.retrieve(user.getCustomerId());
 
5. Add card
After creating a user we have required to add a card fro the customer for the charge.
 
Map<String, Object> map = new HashMap<>();
map.put("number", cardDto.getNumber());
map.put("exp_month", cardDto.getExp_month());
map.put("exp_year", cardDto.getExp_year());
map.put("cvc", cardDto.getCvv());
Map<String, Object> cardMap = new HashMap<>();
cardMap.put("card", map);
Token token = Token.create(cardMap);
Customer cust = getCustomer(cardDto.getUserid());
Map<String, Object> source = new HashMap<>();
source.put("source", token.getId());
cust.getSources().create(source);
 
In the above code, we have created a map for the card then generate a token for that and that particular token is saved with the customer.
 
6. Payment
Here we are going to charge the particular amount from the card. Here we need to amount and currency in which we are required for the payment.
 
Customer cust = getCustomer(paymnetDto.getUserId());
Map<String, Object> map = new HashMap<>();
map.put("amount", paymnetDto.getAmount()*100);
map.put("currency", paymnetDto.getCurrency());
map.put("customer", cust.getId());
Card card = Card.retrieve("card_1Edd6iFkCQJpvWhBw3iJ28aF");
Charge.create(map);
 
Conclusion
In this tutorial, we have shown how to use the Stripe Java API to charge a credit card. 
To test the entire charge flow, we do not need to use a real credit card (even in test mode). We can rely on Stripe testing cards instead.

About Author

Author Image
Rahul Chauhan

Rahul Chauhan is Java Developer having good knowledge of java, have a knowledge of Spring Boot.

Request for Proposal

Name is required

Comment is required

Sending message..