Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

External third party API related to Entities in a DDD way

I'm working on a new project where we will use DDD. The problem here arises on how to approach a case where an external API in my opinion is very related to one entity itself.

Imagine the entity Card. Every user can have a card, similar to a bank plastic card, and that card needs to be created, activated, charged, etc.

The problem here is that this card is related to an external API that manages the card creation, refund, activation and so on.

In a code way without thinking in the infrastructure I see this way.

new Card();
$card->isActive();
$card->refund();

Etc.

But the thing is that this entity functions needs to contact with an external API that really creates all that actions. But for me it looks like infrastructure and is something outside of the Domain Model itself.

Is it correct in a DDD point of view that this entities are capable of connect to the API and make the calls to the API inside?

Isn't it close to DDD to use a service that does some king of:

$service->activateCard($card);

Then what happens to the $card->activate() methods that seemed so natural to me in the ubiquitous language?

Thanks!!

like image 939
Jonathan Gimeno Avatar asked Dec 10 '25 10:12

Jonathan Gimeno


1 Answers

Domain events come to mind. You could have Card emit CardActivated, CardRefunded events with associated data. They would be handled by an infrastructure service that knows how to talk to the external API, leaving your domain layer happily unaware of the technical details.

like image 134
guillaume31 Avatar answered Dec 12 '25 22:12

guillaume31



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!