Integrating Stripe Payment Gateway Using Spring Boot

Posted By : Hrishabh Mishra | 30-Mar-2021

Java Spring boot

Loading...

Stripe Payment Gateway

Stripe is a popular payment gateway that enables users to send and receive payments from the client-side over the internet. Besides, it also accepts credit and debit cards to support easy payments over the internet.

Integration of Stripe Payment Gateway On Application

Create a Stripe account, it only takes a few seconds. After that, go to the developer tab which generates the API key and secret key for the integration of stripe in your application.

Add Dependency in the Application :

The following dependency add to pom.xml file .

1
2
3
4
5
 <dependency>
  <groupId>com.stripe</groupId>
  <artifactId>stripe-java</artifactId>
  <version>${version}</version>
</dependency>

Create a Customer Account :

Create a Customer account on the Stripe Dashboard by using following Classes :

1
2
3
Map<String, Object> customerParams = new HashMap<>();
customerParams.put("email", email);
Customer customer = Customer.create(customerParams);

*$ - Customer class is used to create a customer on stripe payment Gateway which is require for accept and retrieved payment of definite customer account.

Get the List of Customer Account :

By the using of following method where we get all the Customer in the list form, from the client stripe account .

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Map<String, Object> customerParams = new HashMap<>();
customerParams.put("email", email);

CustomerCollection previousCustomer = Customer.list(customerParams);

for (Customer customerCollection : previousCustomerData) {
    String customerMail = customerCollection.getEmail();
        id = customerCollection.getId();
        log.info("Customer name :  + id);
    
}

Add Card In the Stripe Payment Gateway with token :

By adding Cards ,which is thedebit or credit cards, we can payment on the stripe .

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Map<String, Object> customerParams = new HashMap<>();
customerParams.put("email", email);

CustomerCollection previousCustomer = Customer.list(customerParams);

for (Customer customerCollection : previousCustomerData) {
    String customerMail = customerCollection.getEmail();
        id = customerCollection.getId();
        log.info("Customer name :  + id);
    
}

After created Customer the above Code have create the map then generate the token for the payment i.e save with the customer id.

Payment :

We are going to create charge on the following token generated by the customer .

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
Map<String, Object> chargeParams = new HashMap<>();
Map<String, Object> address = new HashMap<>();
address.put("line1", creditCardForm.getAddress().toLowerCase());
address.put("country", creditCardForm.getCountry().toLowerCase());
Map<String, Object> shipping = new HashMap<>();
shipping.put("name", email);
shipping.put("address", address);
chargeParams.put("amount", amount * 100);
chargeParams.put("currency", token.getCard().getCurrency().toLowerCase());
chargeParams.put("description", description);
chargeParams.put("source", token.getId());
chargeParams.put("shipping", shipping);
charge = Charge.create(chargeParams);

In the above code we create map for required parameter for charge and charge class have a method .create() .so given amount is deducted from you credit card account which is given in amount parameter .

Conclusion :

Stripe payment gateway is easy to use for credit and debit charges from the card and it is also easy to integrate with the application using api key and secrete key. To test entire flow we did not need to real card stripe provide mock card for testing purpose.

We are an Web development company that specializes in building performance-driven business apps with custom features. Our end-to-end Web application development services enable enterprises to streamline their inbound/outbound operations with advanced problem-solving capabilities. For more information, contact us at [email protected].