Stripe Payment Gateway Integration In Java

Posted By : Sidharth Sagar | 07-Aug-2023

Java ERP saas


Stripe Payment Gateway Integration in Java

Stripe is a type of payment gateway that accept credit and debit cards for transactions. Stripe payments are very suitable for that type of business that makes their sales online. Stripe allows an individual to accept different types of cards such as Mastercard, visa, China UnionPay, American Express, Diners Club, etc. Stripe also provides some advanced features like buy now, accept payments from e-wallets and pay later services. Stripe also accepts payments in different types of currencies. It also provides various types of additional services such as sales tax automation, invoicing, and billing.

Also, ReadSorting With Lambdas In Java

Steps To Integrate Stripe Payment Gateway In Java

Step 1: First of all you have to add a stripe dependency in your pom.xml file


Step 2: After that mention the currency and secret key of stripe in your file. For getting a secret key you have to create an account or already have an account on stripe is mandatory


Step 3: Now create a function to add a customer on stripe

import com.stripe.model.Customer;
import java.util.*;

Map<String, Object> customerParameter = new HashMap<String, Object>();
customerParameter.put("name", "abc");
customerParameter.put("email", "[email protected]");

Map<String, Object> address = new HashMap<>();
address.put("line1", addressLine1);
address.put("line2", addressLine2);
address.put("city", city);
address.put("state", state);
address.put("postal_code", zipcode);
address.put("country", country);

customerParameter.put("address", address);

For adding a customer on stripe you should use Stripe customer class function i.e. Customer.create()

Customer customer = Customer.create(customerParameter);

For getting a customer from stripe you should use retrive() function of stripe customer class

Customer cus = Customer.retrieve(customerId);

Also, ReadThe Important Features In Java 8

Step 4: After adding a customer now you should add a card on stripe for a customer

Map<String, Object> retrieveParams =new HashMap<>();
List<String> expandList = new ArrayList<>();
retrieveParams.put("expand", expandList);
Customer customer = Customer.retrieve(customerId,retrieveParams,null);

Map<String, Object> params = new HashMap<>();
params.put("source", "tok_visa");

Card card = (Card) customer.getSources().create(params);

step 5: After adding customer and card, now you can processing a payment

Map<String, Object> params = new HashMap<>();
params.put("amount", amount);
params.put("confirm", true);
params.put("off_session", true);
params.put("customer", customer != null ? customer.getId() : null);
params.put("currency", currency);

PaymentIntent paymentIntent = PaymentIntent.create(params);

Note: if the paymentIntent.getStatus() is equal to "succeeded" that means your payment is successful, otherwise your payment has failed.