Stripe payment gateway integration in Spring Boot
Posted By : Amit Maurya | 31-Dec-2021
Stripe is a cloud-based service that provide us the service to receive payments over the internet. Stripe provides a layer of abstraction that reduces the complexity of receiving payments. As a result, we don't need to deal with credit card details directly – instead, we deal with a token symbolizing an authorization to charge.
In this tutorial we will learn how we can configure stripe payment gateway into a spring boot project.
1. API Keys
First of all we need to create a stripe developer account which will provide us private and public API keys. we need to add these keys in application.properties file.
a .) STRIPE_SECRET_KEY
b.) STRIPE_PUBLIC_KEY
2. Maven Dependency
Then we need to add maven dependency. Search in the maven central repository for the latest version.
<dependency>
<groupId>com.stripe</groupId>
<artifactId>stripe-java</artifactId>
<version>4.2.0</version>
</dependency>
3. Creating Entity
Create a POJO to get data from the client application. Here stripe token is a token which will be used to complete the payment. it will be provided by the client.
@Data
public class ChargeRequest {
public enum Currency {
EUR, USD;
}
private String description;
private int amount;
private Currency currency;
private String stripeEmail;
private String stripeToken;
}
4. Initialize Stripe
Create a service to initialize stripe. This will configure the stripe payment gateway with our stripe developer account by the secret key.
@Service
public class StripeService {
@Value("${STRIPE_SECRET_KEY}") private String secretKey;
@PostConstruct
public void init() {
Stripe.apiKey = secretKey;
}
}
5. Payment method
Now we need to create a method which will complete the payment. Here we take data as charge request from the client and this method return a charge object provided by stripe form 'com.stripe.model.Charge'. In this method we are setting all the params in a charge params map. Charge.create() method processes the payment and returns a charge object having transactionId and payment status etc.
public Charge charge(ChargeRequest chargeRequest)
throws AuthenticationException, InvalidRequestException,
APIConnectionException, CardException, APIException {
Map<String, Object> chargeParams = new HashMap<>();
chargeParams.put("amount", chargeRequest.getAmount());
chargeParams.put("currency", chargeRequest.getCurrency());
chargeParams.put("description", chargeRequest.getDescription());
chargeParams.put("source", chargeRequest.getStripeToken());
return Charge.create(chargeParams);
}
6. Testing
In order to test Stripe payment gateway integration we can use testing token or card :
1. token => tok_visa
2. Card => Card No. = 4242 4242 4242 4242
Expiry Date = any upcoming date
CVC = 123
This is how we can integrate and test Stripe payment gateway in spring boot application.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Amit Maurya
Amit Maurya is a highly skilled Backend Developer with more than 2 years of experience in developing RESTAPIs and Microservices. He has expertise in using Spring Boot framework, Hibernate, Java 8, and JavaEE, and he has worked with various databases such as MySQL, PostgreSQL, Oracle, Redis, and more.He has also worked on implementing payment gateways such as Stripe, Paypal, Cryptocurrency (Metamask), Android In-App Purchases, and Apple in App Purchases. Furthermore, he has experience in implementing and maintaining Streaming servers like Ant Media and Agora. He is proficient in using version control systems like GIT and source code management tools such as GitHub and GitLab, including command line applications.He has worked on several projects, including TutorX, Fabtrack, Toosi WhatsApp Chatbot Integration, and Virtuosica.