How to make code reuse possible in payment gateway microservice
The problem I am solving is wanting to reuse code between different payment gateways in microservice. Suppose I want to have a makePayment() method in my controller. Adding some rough way on how the code might look:
Controller:
Response makePayment(@RequestBody request){
if(request.gateway == PAYU){
// call PayU service
}
else if(request.gateway == Paypal){
}
}
Service
Entity payuservice(request){
// request.method ??
}
Entity
class Entity {
String mode; // CC,DC, Wallet
String status;
...
}
Repository
interface PayuRepository {
}
So my questions combined would be,
- I want to reuse code across different payment gateways. What can I do here when I have the gateway name in the request along with payment details from the form?
- Do I need to consider different methods on the backend? The entity would only have a different values for different methods? For e.g UPI would have one extra field 3)Do I need different repositories for different gateways or different tables?
You can achieve something nice with a mix of the Adapter and the Strategy design patterns. The Strategy would determine what gateway-specific code to run, and the Adapter would translate the payment request to the gateway model (and vice-versa).