jeudi 13 juillet 2023

Dynamic Provider in Nest JS

Today I have a payment service based on Nest JS.

I have 3 payment methods: PIX (brazilian payment), Boleto (brazilian payment) and credit card.

These payment methods are linked to a payment gateway (PagSeguro (Brazilian), PayPal etc.)

So I created a service like: createPixCharge, createBoletoCharge, createCreditCardCharge. These services communicate with the payment gateway API.

To make the code clean and scalable, I used the repository pattern. So, there is an abstract repository with methods like: createPixCharge (which returns a code that the customer will pay in his Brazilian bank - this code is universal)

I did this because today I use API 1, tomorrow I can switch to another platform.

Therefore, what could I do to, according to the chosen payment method, inject the desired repository dependency?

For example:

if customer chooses payment method X, it will inject API dependency 1. If client chooses payment method Y, it will inject API dependency 2

As I said, I created a service like createPixCharge, createBoletoCharge and createCreditCardCharge, which when invoked, depending on the payment method, will call the repository that integrates with the API.

But I don't know how to relate it all.

Aucun commentaire:

Enregistrer un commentaire