Integration of Stripe Payment in Spring Boot

Posted By :Rozi Ali |31st March 2022

Stripe Payment

Stripe Payment is an online payment processing platform that allows you to perform transactions using a debit or a credit card. Stripe Payment is one of the most popular payment platform that provides secure transactions with security checks:

  1. CVV/CVV2 checks
  2. AVS (Address Verification Service)
  3. TLS (SSL) encryption


Implementation of Stripe Payment Gateway in Spring-boot Application

To integrate Stripe Payment in Spring-boot Application, first you need to register to a Stripe account. Here is the link: https://dashboard.stripe.com/register

After successfully registering, you will be led to the dashboard. From the menu, choose API keys. You will get two pairs of API keys, one for testing and other is for live. Copy the keys and add to application.properties file.

 

Next, add a maven dependency:

<dependency>   <groupId>com.stripe</groupId>   <artifactId>stripe-java</artifactId>   <version>${version}</version> </dependency>


There are two ways to perform transactions using stripe:

  1. By using tokens and not saving the cards
  2. By saving card details in Stripe

In this blog, we will perform the implementation using the second method, by saving the card details.

 

So for this, we first need to create a customer which returns you a Customer object. You can use the customer.getId() to fetch a unique customer id, using this id you can perform futher operations like to save the card details or make payment

Stripe.apiKey = environment.getProperty('stripe_key'); Map<String, Object> map = new HashMap<>(); map.put('email', user.getEmail()); Customer customer = Customer.create(map); String customerId = customer.getId();


By saving the card, we wouldn't need to enter the details everytime.

Map<String, Object> map = new HashMap<>(); map.put('number', xxxxxxxxxxxxxxxx); map.put('exp_month', 03); map.put('exp_year', 2030); map.put('cvc', 123); map.put('name', 'John Doe'); Map<String, Object> cardMap = new HashMap<>(); cardMap.put('card', map); Token token = Token.create(cardMap); Customer customer = Customer.retrieve(customerId); Map<String, Object> source = new HashMap<>(); source.put('source', token.getId()); customer.update(source);


Finally, to make a payment, Stripe provide a Class 'Charge' with method create(). 

stripeCustomer = Customer.retrieve(stripeCustomerId); Map<String, Object> map = new HashMap<>(); map.put('amount', amount * 100); map.put('currency', 'USD'); map.put('description', 'payment description'); map.put('customer', customerId); Charge charge = Charge.create(map);


And that's it. You can track the transaction statements and customers details in your stripe dashboard.

Note: Here, i have multiplied the amount by 100 because it considers the amount to be in cents (for USD currency) and i want the result in dollars.

Stripe also provides dummy card details for testing:  https://stripe.com/docs/testing


About Author

Rozi Ali

Rozi Ali is an accomplished software developer with extensive experience in the field of JAVA. She possesses a solid grasp of programming languages such as Java/Spring-boot, Python, and Typescript/Nodejs/GraphQL. Rozi has a strong background in Object-oriented programming (OOP) and is skilled in working with both relational databases like MySql, PostgreSQL and non-relational databases like MongoDb. She is proficient in REST APIs, Microservices, and code deployment, along with the development tools such as Jira, Git, and Bash. Additionally, Rozi has experience working with Cloud providers such as AWS and Azure. She has contributed significantly to a number of projects, including Konfer, VNS, Influsoft, VN Platform, QuickDialog, and Oodles-Dashboard.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us