Skip to main content

Architecture

customer-payments follows the fleet's hexagonal (ports and adapters) layout: dependencies point inward, the domain imports nothing from the rest of the module, and only cmd/ wires the layers together. Everything on this page is on develop.

Layout​

LayerPathRole
Domaininternal/domain/paymentauthorizationPaymentAuthorization aggregate: states AUTHORIZED, PARTIALLY_CAPTURED, CAPTURED, RELEASED, VOIDED, EXPIRED, DECLINED; allocations per shipment group (PENDING, CAPTURED, RELEASED); commands (create.go, commands.go), events (events.go), Restore.
Domaininternal/domain/refundRefund aggregate: REQUESTED, then SUCCEEDED or FAILED; MaxAttempts = 5; the over-refund invariant via Limits.
Domaininternal/domain/sharedMoney helpers, id and currency validation, Event interface, ErrValidation, ErrIllegalTransition.
Applicationinternal/application/usecasesOne struct per use case (see Use cases): AuthorizePayment, VoidAuthorization, GetAuthorization, ListAuthorizations, RequestRefund, RetryRefund, GetRefund, ListRefunds, the Consumer (CheckoutCompleted, ApplyFact) and the four policies. Deps bundles the ports.
Applicationinternal/application/portsOUT interfaces only: repositories, UnitOfWork, EventOutbox, Inbox, ParkedFacts, Clock, IDGenerator, PaymentProvider, Escalator, PaymentMetrics, IdempotencyStore.
Applicationinternal/application/{idempotency,outbox,parked,provider,views}The Idempotency-Key record, the already-encoded outbox Message, the parked-fact type and its kinds, the provider's request / hold / decline types, and the read models returned to adapters.
Inbound adapterinternal/adapters/inbound/httpchi router for apis/openapi.yaml: handlers, RFC 9457 problem mapping (errors.go), the Idempotency-Key middleware, /healthz, /readyz, /metrics. No auth middleware.
Inbound adapterinternal/adapters/inbound/kafkaThe checkout, order-sourcing and shipping-and-tracking consumers: CloudEvents decode, dispatch on the full type, retry with backoff, dead-letter topic, consumer spans. Decode only; every effect is a use case.
Inbound adapterinternal/adapters/inbound/mcpThe four read-only MCP tools over the read use cases (cmd/mcp).
Inbound adapterinternal/adapters/inbound/a2aThe two read-only A2A skills and the Agent Card (cmd/a2a, ADR 0005).
Inbound adapterinternal/adapters/inbound/requestlogThe access-log middleware shared by the three routers.
Outbound adapterinternal/adapters/outbound/postgrespgxpool repositories, UnitOfWork (pgtx), outbox repository, inbox, parked facts, idempotency store, embedded migrations.
Outbound adapterinternal/adapters/outbound/paymentproviderThe Anti-Corruption Layer: HTTP client for PAYMENT_PROVIDER_URL (10 s timeout; 402 = decline, 429 and 5xx = unavailable).
Outbound adapterinternal/adapters/outbound/outboxThe relay loop that drains outbox_events to a sink.
Outbound adapterinternal/adapters/outbound/kafkaNewWriter (acks=all, hash balancer, no fixed topic, AllowAutoTopicCreation), the event Encoder, the relay Sink, the Escalator for unbound facts and BrokerCheck (the Kafka half of /readyz).
Outbound adapterinternal/adapters/outbound/telemetryOTel setup, the slog trace handler, the payments.authorizations.decided counter.
Outbound adapterinternal/adapters/outbound/{clock,ids}Wall clock and id source.
Outbound adapterinternal/adapters/outbound/memoryIn-memory implementation of every port: a test double for the unit and godog suites. No cmd/** imports it (TestRuntimeBinaryHasNoInMemoryAdapters).
Kafka envelopeinternal/adapters/kafka/cloudeventsThe only place a CloudEvents 1.0 envelope is built or decoded: New(Spec), Decode, ContentTypeHeader, Type, DataSchema. Constants Tier = "storefront", Context = "customer-payments", Source = "/retailnetwork/customer-payments".
Supportinternal/bootretryExponential-backoff retry for the first outbound dials at boot.
Test supportinternal/testing/{kafkatest,pgtest,providerfake}, internal/adapters/outbound/repocontracttestcontainers helpers, an HTTP fake of the provider, and the repository contract suite run against both memory and Postgres.
Fitness testsinternal/architectureSource-scanning and dependency tests; see Testing.

Binaries and ports​

BinaryRolePortProbes
cmd/apiREST API, the three Kafka consumers, the outbox relay and the policy loop in one process, all always on, on real Postgres and Kafka. A worker that returns on its own exits the process.HTTP_ADDR, default :8080GET /healthz (liveness), GET /readyz (drain gate, Postgres ping, a Kafka broker answers a metadata request; never topics)
cmd/mcpRead-only MCP server (Streamable HTTP, /mcp) over the read use cases; runs the migrations, but no relay, consumer or policy; never dials Kafka or the provider.MCP_ADDR, default :8090GET /healthz
cmd/a2aRead-only A2A server: JSON-RPC 2.0 at / and /a2a, Agent Card at /.well-known/agent-card.json; same read use cases; no Kafka, no provider.A2A_ADDR, default :8093GET /healthz

There is no web/ and no analytics projector in this repository. How cmd/api boots and in which order it stops is in the Runbook; every variable is in Configuration.

Container diagram​

Source: cmd/api/main.go (wiring, consumerSpecs, startOutboxRelay), cmd/api/policies.go, cmd/mcp/main.go, cmd/a2a/main.go, internal/application/usecases/deps.go and the import lists of internal/adapters/**. The in-memory adapters are not in the diagram because no binary wires them.

Data stores​

One Postgres database, created by the single embedded migration (internal/adapters/outbound/postgres/migrations/0001_customer_payments_schema.up.sql), shared by the three binaries (cmd/api is the only writer):

TableHolds
payment_authorizationsThe aggregate root: state, checkout_session_id, order_id (bound later), PSP token, provider_authorization_id, expires_at, reauthorizations, optimistic version. A unique partial index allows one non-DECLINED authorization per checkout session and per order.
allocationsOne row per shipment group: amount_minor, state, captured_minor, tracking_numbers, provider_capture_id.
refundsThe Refund aggregate: state, attempts, failure_code, provider_refund_id, version.
parked_factsFulfilment and tracking facts whose order is not bound yet (or whose tracking number no capture carries yet), with parked_at and escalated_at.
processed_eventsThe inbox: one row per consumed CloudEvents id.
idempotency_keysStored answers of the POST routes, one row per global Idempotency-Key (the route column is always empty) with a request hash; written in the transaction of the aggregate change and its outbox rows.
outbox_eventsThe transactional outbox: encoded CloudEvents, topic, key, headers (including traceparent), published_at, attempts, last_error.

Postgres is the only store; the service refuses to boot without DATABASE_URL. Kafka is the fleet's single broker: this service produces to retailnetwork.customer-payments.events (and, for stale facts, to retailnetwork.customer-payments.unbound-facts.dlq) and consumes retailnetwork.checkout.events, retailnetwork.order-sourcing.events and retailnetwork.shipping-and-tracking.events. It never creates a topic through an admin API: the broker auto-creates each on first write, and /readyz waits for no topic.

How a command is processed​

Every command runs inside one UnitOfWork: load the aggregate with its version, let the domain decide, call the payment provider with a deterministic key (Key(operation, authorizationId, groupNo, qualifier)), save the aggregate conditionally on the version, append the raised events to the outbox, and answer. A version conflict reruns the whole attempt (twice for commands, five times for consumers) before surfacing as 409 concurrent-update. The relay publishes the outbox rows after the commit, so an event is never published for a rolled-back change and never lost for a committed one. The provider call sits inside the transaction callback but its key is deterministic, so a retried attempt cannot move money twice. See Sequence diagrams.