Use cases
Every use case is a struct in internal/application/usecases that embeds Deps
and exposes Execute (or, for the Kafka-driven ones, methods on Consumer).
Commands share one shape inside a single UnitOfWork: load the aggregate with
its version, let the domain decide, call the payment provider with a
deterministic key, save conditionally on the version, append the raised events to
the outbox, return a view. A version conflict (ports.ErrConcurrentUpdate)
reruns the attempt (2 attempts for commands, 5 for consumed events) before it
surfaces as 409 concurrent-update. Errors pass through unchanged and the HTTP
adapter maps them to problem slugs (API overview).
At a glance
| Use case | Trigger | Inbound adapter | Events raised |
|---|---|---|---|
AuthorizePayment | checkout | POST /payment-authorizations | PaymentAuthorized or PaymentDeclined |
VoidAuthorization | checkout; policy VoidUnboundAuthorizations | POST /payment-authorizations/{authorizationId}/voids; policy loop | PaymentReleased (reason VOIDED, one per allocation) |
GetAuthorization, ListAuthorizations | checkout, ops, MCP, A2A | GET /payment-authorizations..., MCP, A2A | none |
RequestRefund | storefront ops | POST /refunds | RefundIssued or RefundFailed |
RetryRefund | storefront ops | POST /refunds/{refundId}/retries | RefundIssued or RefundFailed |
GetRefund, ListRefunds | ops, MCP, A2A | GET /refunds..., MCP, A2A | none |
Consumer.CheckoutCompleted (bind) | CheckoutCompleted | Kafka retailnetwork.checkout.events | none itself; applies parked facts |
Consumer.ApplyFact: capture | FulfillmentShipped | Kafka retailnetwork.order-sourcing.events | PaymentCaptured |
Consumer.ApplyFact: release | FulfillmentCancelled, FulfillmentUnsourceable | Kafka retailnetwork.order-sourcing.events | PaymentReleased (CANCELLED / UNSOURCEABLE) |
Consumer.ApplyFact: refund package | PackageLost, PackageReturned | Kafka retailnetwork.shipping-and-tracking.events | RefundIssued or RefundFailed |
ReauthorizeBeforeExpiry | clock | policy loop | PaymentReauthorized, or PaymentDeclined (REAUTH_DECLINED) |
ProcessRequestedRefunds | clock | policy loop | RefundIssued or RefundFailed |
MaintainParkedFacts | clock | policy loop | none (publishes to the unbound-facts DLQ topic) |
AuthorizePayment
- Trigger / inputs: checkout.
checkoutSessionId,paymentMethodToken(a PSP token, never a card number),currency(^[A-Z]{3}$),totalMinor,allocations[](groupNo,amountMinor). HeaderIdempotency-Keyrequired. - Flow: if the session already holds a live (non-declined) authorization, it
is returned unchanged (
200) and the provider is not called. Otherwise the draft is checked, the providerAuthorizeis called with keyauthorize:<sessionId>:0:<n>(n = attempts so far + 1), and the authorization is createdAUTHORIZED(withexpiresAtand the provider hold id) orDECLINED(with the decline code). Saved with its outbox row in one transaction;201. - Invariants: allocations sum to
totalMinorand name distinct groups (allocation-mismatch); amounts are not negative; valid ids and currency (validation-failed); one live authorization per session and per order (unique partial indexes,duplicate-authorizationon a race). - Failure: a provider outage is
payment-provider-unavailable(503) and nothing is stored; the metricpayments.authorizations.decidedrecordsauthorized,declinedorfailedwith a reason. - Events:
PaymentAuthorized(withallocationsandexpiresAt) orPaymentDeclined.
VoidAuthorization
- Trigger / inputs: checkout after a definitive rejection of its place-order
step, or the
void-unbound-authorizationspolicy.authorizationId; headerIdempotency-Keyrequired on REST. - Invariant: only an
AUTHORIZEDauthorization (no capture, not expired, not voided) can be voided, elseillegal-transition(409). The domain verdict is checked on a copy before the provider is called. - Flow: provider
Void, then every pending allocation is released with reasonVOIDEDand the state becomesVOIDED. - Events: one
PaymentReleased(reasonVOIDED) per allocation.
RequestRefund and RetryRefund
- Trigger / inputs:
POST /refundswithorderId,amountMinor,reasonand optionalgroupNo(0 = whole order); the lost / returned policies call the same internal path. - Invariants:
orderIdmust be bound to an authorization (not-foundotherwise);amountMinor >= 1; reason isLOST,RETURNED_TO_SENDERorMANUAL; refunds of the order (and of the group whengroupNo > 0) never exceed what was captured, countingREQUESTEDandSUCCEEDEDrefunds and neverFAILEDones (over-refund, 409). - Flow: create the refund
REQUESTED, save it, call the providerRefundwith keyrefund:<authorizationId>:<groupNo>:<refundId>:<attempt>. Success recordsSUCCEEDED; a provider decline recordsFAILED(with the code); an unreachable provider leaves itREQUESTED(WARN) for theprocess-requested-refundspolicy. - RetryRefund:
FAILEDtoREQUESTEDand one more provider attempt; at mostrefund.MaxAttempts = 5attempts in all. When the fifth fails the refund staysFAILEDand an ERRORALERT refund failed for good: operator action neededis logged. A refund that is notFAILEDisillegal-transition. - Events:
RefundIssuedorRefundFailed(final: trueon the last attempt).
Queries
GetAuthorization, ListAuthorizations (exactly one of orderId and
checkoutSessionId, else validation-failed; a session lists declined attempts,
an order lists its one bound authorization or nothing), GetRefund, ListRefunds
(orderId required, oldest first). The two lists return one keyset page
(Execute(..., after, limit): limit rows, ordered by (createdAt, id), and the
keyset to resume after). The same four use cases back the MCP tools and the A2A
skills, which keep their own tool schemas and follow every page through
All (up to 1000 rows) so they still return complete data.
Consumed events
Each consumer method claims the CloudEvents id in processed_events and applies
its effect in the same unit of work, so a redelivery converges.
CheckoutCompleted (bind). Inputs sessionId, orderId, authorizationId.
Invalid ids, or a sessionId that does not match the authorization, are
ErrInvalidEvent (dead-lettered at once). BindOrder is idempotent for the same
order and order-already-bound for another one. After the bind, every parked fact
of the order is applied in arrival order.
ApplyFact. Valid facts: group facts (shipped, cancelled, unsourceable) need
groupNo >= 1; package facts (lost, returned) need exactly one tracking number.
If the order has no authorization yet, the fact is parked.
- Capture (
FulfillmentShipped): the group's allocation is captured at the provider (keycapture:<authorizationId>:<groupNo>), tracking numbers are stored,PaymentCapturedraised. A captured group is left alone. Unknown group, over-capture and capture after expiry are domain refusals (dead-lettered). A shipment also re-applies facts that were waiting for its tracking number. - Release (
FulfillmentCancelled,FulfillmentUnsourceable): the pending allocation is released and the provider hold is reduced to what is still pending (ReduceAuthorization), or voided when nothing is pending;PaymentReleasedwith reasonCANCELLEDorUNSOURCEABLE. A released group is left alone; a captured group cannot be released. - Refund package (
PackageLost,PackageReturned): the group that carried the tracking number is found (if no capture carries it yet the fact waits, parked); the amount is the group's captured amount less refunds already requested or made; nothing left is a no-op; an over-refund is skipped with a WARN.
Policies (clock-driven, every POLICY_INTERVAL)
- ReauthorizeBeforeExpiry (
reauthorize-before-expiry): live authorizations withexpires_atwithinReauthLead(24 h). The provider is asked to reauthorize the pending amount (keyreauthorize:<id>:0:<n>); success raisesPaymentReauthorized(state staysAUTHORIZED/PARTIALLY_CAPTURED, newexpiresAt); a provider decline expires the authorization (PaymentAuthorizationExpired) and raisesPaymentDeclinedwith reasonREAUTH_DECLINED; a hold that already lapsed expires without calling the provider. Provider outages, concurrent updates and illegal transitions are skipped with a WARN and retried at the next tick. - VoidUnboundAuthorizations (
void-unbound-authorizations):AUTHORIZEDauthorizations with noorderIdolder thanUnboundVoidAfter(24 h) are voided. - ProcessRequestedRefunds (
process-requested-refunds): refundsREQUESTEDfor more than 30 s are settled against the provider. - MaintainParkedFacts (
maintain-parked-facts): facts parked longer than 24 h are published toretailnetwork.customer-payments.unbound-facts.dlqwith anALERT fact for an unbound order dead-letteredERROR; facts older than 30 days are purged.
Each pass handles at most 200 rows per tick.