mardi 28 mai 2019

Abstraction of API client

How do you deal with abstractions for the API clients, when you need to create multiple nested builders for staff like request builders.

I need to use an external payments service in a business logic to deposit user's internal account. Generally I need to send a JSON that looks like this:

{
  "amount": {
    "value": "200.50",
    "currency": "RUB"
  },
  "description": "Some text",
  "confirmation": {
    "type": "redirect",
    "return_url": "https://site.ru"
  },
  "payment_method": "bankcard"
}

I make a bunch of contracts (interfaces) that allows me to do this:

$request = $gateway->createPayment()
    ->setAmount('200.50', 'RUB')
    ->setDescription('some text')
    ->confirmation()
        ->redirect()->setReturnUrl('https://site.ru')

$request->paymentMethod()->bankcard();

return $request;

confirmation() and paymentMethod() are problems. These are separate interfaces but also they contain methods like redirect() inside them, which return another interfaces.

There are another request options that can contain bigger depth of nesting. And for the every nested level I should create an interface.

It would be more comfortable if I will use something like $payment->setConfirmation(new Redirect('https://site.ru')) where Redirect is an implementation of ConfirmationInterface, but I cannot do this, because the Redirect is a concrete implementation, but I want to fully decouple my business logic services from implementations, so I cannot create any instances that doesn't rely on the business services layer.

So maybe you know another ways how to make it robust and beautiful?

Aucun commentaire:

Enregistrer un commentaire