Video in progress

Strong Middle 🧭 Design patterns

Why wrap an external payment provider's SDK in an adapter, and what happens if you use it directly?

An adapter hides the SDK behind your own interface. Then, if the provider changes or the SDK version changes, only the adapter gets rewritten; the domain and the tests don't know about the SDK classes.

📖 Answer

I've seen many times what happens when you use it directly: the SDK's ChargeResponse, ProviderException and its enums spread into services, controllers, even entities. Two years later the provider releases SDK version 3, the packages change, and you edit 40 files.

With an adapter:

public interface CardPayments {
    PaymentResult charge(Money amount, CardToken card, String idempotencyKey);
}

@Component
class AcmePayAdapter implements CardPayments {
    private final AcmeClient client;

    public PaymentResult charge(Money amount, CardToken card, String key) {
        try {
            var r = client.charges().create(amount.minorUnits(), amount.currency().getCode(), card.value(), key);
            return r.isApproved() ? PaymentResult.ok(r.getId()) : PaymentResult.declined(r.getDeclineCode());
        } catch (AcmeTimeoutException e) {
            return PaymentResult.unknown(key); // state is unknown — checked later
        }
    }
}

The adapter's main job isn't just renaming methods. It translates: currency units, error codes, timeout semantics. This is where you answer "if it timed out, was the money charged or not", and the domain never sees these subtleties.

In tests you replace CardPayments with a simple fake, and you test the adapter itself separately with an integration test using WireMock.