The Leaky Payment Port
Hard About 40 min Free, no account needed
A checkout service is welded to a Stripe client and passes vendor DTOs around. Someone half-extracted an IPaymentGateway port, but it leaks Stripe types through its own signature. Make the port vendor-neutral with a real anti-corruption layer, not an interface that smuggles the SDK through.
The brief
You've inherited the checkout flow. Checkout.CheckoutService.Pay(amountCents) takes a payment by
constructing a concrete Vendor.StripeClient, building a Vendor.StripeChargeRequest, and reading a
Vendor.StripeChargeResult straight off the call. The domain service is welded to the Stripe SDK:
swap processors, or take a breaking SDK upgrade, and the change tears right through the heart of
checkout.
Someone already saw the smell and started the obvious fix. There's an IPaymentGateway interface
sitting next to the service. But look closely: its method is StripeChargeResult Charge( StripeChargeRequest req). The "abstraction" is typed entirely in the vendor's own DTOs. So
CheckoutService never even adopted it; depending on that port would buy nothing, because the vendor
would still ride straight through its signature.
The tempting move is to finish that half-built extraction: point CheckoutService at the existing
IPaymentGateway, stop new-ing StripeClient yourself, and seal up the adapter. Do exactly that
and most of the rules go green. But one stays stubbornly red, and it's the one that matters. An
interface whose signature speaks Stripe* types is not an abstraction over the vendor. It is the
vendor SDK wearing a trench coat: the coupling didn't leave, it just moved into the contract.
Make the port a real seam. Figure out what CheckoutService actually needs to say to take a
payment, in your vocabulary rather than Stripe's, and express the port in those terms. Then give
the Stripe knowledge exactly one home. The behavior must stay byte-for-byte identical: same
"charged:..." string for the same amountCents.
Your submission must (1) still pass the behavior tests (identical charged: strings) and
(2) satisfy every rule shown in the results, including the one pinning that CheckoutService
references no Stripe* type at all, not even through the port's signature. The naive
finish-the-extraction clears the easy rules and leaves that one failing; only a port typed in neutral
terms, with an adapter translating to and from the vendor, turns them all green.
"We extracted an interface" is only half the story. An abstraction that leaks the type it was meant to hide hasn't decoupled anything. It has just relocated the dependency into the contract.
How it is graded
Executable structural checks grade the shape of your types as well as their behavior.
What it practices
- Anti-corruption layer
- Translate a legacy or external model at the boundary, and replace it incrementally behind that port.
- Dependency inversion
- Point dependencies at abstractions the core owns, never at outer mechanisms.
Do these first
Related katas
- Config Is Infrastructure Medium Free The shipping core reads a framework configuration type directly, welding the domain to infrastructure. Invert it behind a core-owned options port, and don't just rename the framework wrapper into the core, which trips a second rule.
- Move the Port Home Medium Free A
*Portabstraction is declared beside its adapter, so the core reaches outward to see its own port. Move the single port into the core; duplicating it leaves a stray copy that fails the role-placement rule. - The Layer Skip Medium Free A controller reaches past the application layer straight into infrastructure for the order total. Route the call back through the middle layer with a port the application owns; a thin pass-through that still touches infrastructure only moves the smell, it doesn't remove it.
- Compensate Every Completed Step Hard Pro A booking coordinator knows concrete suppliers. Turn it into a saga over published step capabilities, preserving reverse compensation and draining remaining undo work when one release fails.