Stripe Integration Spring Boot
05.02.2024
15 min
Registering a Stripe account and getting keys
The first thing to do to integrate Stripe is to go to stripe.com and register, then go to the section Developers -> API Keys, and there you can see the Publishable key and Secret key.
Creating a Spring boot program
Go to Spring Initializr and create a new application with Spring Web and lombok dependencies.
After creation, a new field must be added to application.properties, which should have the secret key value and look like this:
stripe.apikey=your-key
After that, we will create a controller that will read this key and for now, just display it on the screen, for this we will use the following code:
And after starting the program and going to localhost:8080, we can see our key:
Adding a Stripe dependency
To add a new dependency, you need to find the necessary dependency in mvnrepository and copy it to the dependencies block in pom.xml.
Dependency: https://mvnrepository.com/artifact/com.stripe/stripe-java
In our case, the current version is 24.22.0.
Type of pom.xml dependencies:
Creating the CustomerEntity class
Let’s create a CustomerEntity in which there will be fields name, email, customerId, and all of the String types, and also add annotations from lombok to have getters and setters for all fields, currently, our class looks like this:
Changing the controller
Let’s remove the method that shows the key and change our controller:
Testing via Postman
Namely, we will write the path to our endpoint, in our case, it is localhost:8080/api/customers, after which we will choose the request method: POST, we will go to Body, there we will choose raw and JSON, and then we will write our object that looks like this:
Click the Send button and see the result:
As we can see, our object has been created and returned with our values.
Working with the Stripe object
To work with the Stripe object, visit the documentation site for this: https://docs.stripe.com/api/customers
Let’s go to the POST method and after that select Java from the drop-down list:
We copy this code and change it a little for our method, we get the following result:
Now after restarting the application and sending a request to Postman, we see the following response:
After that, we go to Stripe, and in the Customers section, we can see that our Customer has been created:
Getting a list of all Customers
To do this, we will perform similar actions as in the previous point, but for simplicity, you can go to this link: https://docs.stripe.com/api/customers/list
There we copy the Java code and insert it into the new method, and after that, we map Customer to CustomerEntity, we get the following code:
After that, restart the program.
Before testing the new service, we made another Customer using the request from the previous point.
In Postman, we send GET localhost:8080/api/customers and get the following result:
Deleting Customer by customerId
To delete Customer, go to the documentation site again: https://docs.stripe.com/api/customers/delete and copy the code from there into the new deleteCustomer method and get the following result:
Now let’s try to create and delete a new Customer:
Getting Customer by customerId
We perform the same steps as in the previous methods: we go to the site: https://docs.stripe.com/api/customers/retrieve and create a new method based on this:
We check that everything works:
Result
Now we have an application that is integrated with Stripe and can be used to interact with users.
Similar post
All posts10 min
Read more