Payment gateway implementation using spring boot

Edvin Ruseckij
2 min readJul 6, 2021

--

Let’s image what, you are working on a payment project where you need to integrate with 3-ed payment providers to process payments. Your project owner come and says “Hey We need to enable payment for our customers, so we need flexible solution with ability to add as many payment providers as we want”.

As a software developer, you need to evaluate current requirements and select the proper solution. First, you need to create convenient payment architecture with ability to add any payment provider without code refactoring. Also, code should be testable and maintainable. Below described payment gateway architecture.

In this article, we will use spring to manage our components and project lombok to avoid boilerplate code.

Payment gateway architecture

Let’s start, first we need to create a payment service that accepts generic payment request and produces generic payment response.

Now we have a common way to add any payment service as we need. So, the next step is create payment provider interface to enable selection of payment service.

Code snippet that displayed above implements payment provider that returns from spring application context payment service depending on the provided type.

It is time to merge our code to one interface that accepts payment request and automatically selects suitable payment provider depending on the input data and finally process payment. To solve that, we will create a payment gateway interface that contains a payment provider which returns an appropriate payment service implementation.

Finally, we can create controller that accepts valid payment request and use one common payment service to process payments.

Conclusion

Pros of this solution are that we can add any payment providers as we need without code refactoring, all we need is just adding new payment service implementation and extend generic payment request class.

All code of this article you can find on my GitHub account.

--

--

Edvin Ruseckij
Edvin Ruseckij

Written by Edvin Ruseckij

Software Engineer with travel addict

Responses (5)