Skip to main content

Aggregate Design Canvas

Released at v0.1.4, applied to the k3s cluster

Derived from model/model.yaml. The customer-payments service is built on develop (IQVO/customer-payments at 054b3de, checked 2026-12-02), released at v0.1.4 and applied to the k3s cluster: the api and the A2A agent are deployed, the MCP server is registered in warehouse-infra and switched off. Every Err... name, REST route and CloudEvents type of this context named on this page exists in the service; where the built service differs from the model's design, the page says so (As built) or the context's Service and operations section lists it.

Following the ddd-crew Aggregate Design Canvas v1.1. The model has two aggregate roots in this context, PaymentAuthorization and Refund, and one read model, ParkedFulfillmentFacts. Go packages as built: internal/domain/paymentauthorization, internal/domain/refund (plus shared); the design named internal/domain/authorization, internal/domain/refund.

PaymentAuthorization​

1. Name​

PaymentAuthorization (paymentauthorization.PaymentAuthorization), identified by authorizationId.

2. Description​

One checkout session's authorization: a unique checkoutSessionId, an orderId (bound later, by BindOrder), the allocations (groupNo, amountMinor, state PENDING, CAPTURED or RELEASED, and a nullable trackingNumber), the provider references and expiresAt. The aggregate state is derived from the allocations. Value objects:

Value objectFieldsNote
AllocationgroupNo, amountMinor, state (PENDING, CAPTURED, RELEASED), trackingNumber?The share of the authorization that belongs to one shipment group; the unit of capture and release. The nullable trackingNumber is set at capture; PackageLost and PackageReturned map to a group by it.
ProviderRefproviderAuthorizationId, providerCaptureIds[]The provider's references; the only place provider ids live in the aggregate.

The aggregate holds no card data; the PSP token is an argument of the provider port.

3. State Transitions​

Derived from: model.yaml → contexts[customer-payments].aggregates[PaymentAuthorization].states, .transitions, .commands, .notes.

The entry arrows are the creating command AuthorizePayment: an accepted authorize creates the record AUTHORIZED, a declined one creates it DECLINED, and the checkout session stays open for another token. The state is derived from the allocations: PARTIALLY_CAPTURED while some are captured and some pending; CAPTURED when every allocation is captured or released with at least one captured; RELEASED when every allocation is released (H3). ReauthorizePayment keeps the state AUTHORIZED and replaces expiresAt and the provider reference. BindOrder sets the order and changes no state. A provider decline on re-authorization moves the authorization to EXPIRED and raises PaymentDeclined{reason: REAUTH_DECLINED}.

4. Enforced Invariants​

InvariantEnforced by
One live authorization per checkout session; a sequential second authorize returns it (200)AuthorizePayment returns the existing authorization; ErrDuplicateAuthorization (409 duplicate-authorization) only when two concurrent first calls race on the unique index
Allocations sum to the authorized total and name the session's groupsAuthorizePayment → ErrAllocationMismatch
Captured per group never exceeds the allocationCaptureAllocation → ErrOverCapture
No capture after expiresAtCaptureAllocation → ErrCaptureAfterExpiry
A capture or release names an existing groupCaptureAllocation, ReleaseAllocation → ErrUnknownGroup

Derived from: model.yaml → contexts[customer-payments].aggregates[PaymentAuthorization].invariants. Shared problem types apply on top: illegal-transition (409) for a command the state does not allow, concurrent-update (409) and validation-failed (422).

5. Corrective Policies​

PolicyOwnerRuleEffect on this aggregate
BindOrderOnCheckoutCompletedthis contextOn CheckoutCompleted bind orderId to the session's authorization, then apply any parked fulfilment facts.BindOrder; then the parked CaptureAllocation, ReleaseAllocation or refund commands run.
CaptureOnShipmentthis contextOn FulfillmentShipped{orderId, groupNo, trackingNumbers} capture that group's allocation; also store the tracking number against the group's allocation.CaptureAllocation; PaymentCaptured.
ReleaseOnCancellationthis contextOn FulfillmentCancelled release that group's allocation.ReleaseAllocation; PaymentReleased (reason CANCELLED).
ReleaseOnUnsourceablethis contextOn FulfillmentUnsourceable release that group's allocation.ReleaseAllocation; PaymentReleased (reason UNSOURCEABLE).
ReauthorizeBeforeExpirythis contextAt expiresAt minus 24 h re-authorize the uncaptured allocations; a provider decline moves the authorization to EXPIRED.ReauthorizePayment; PaymentReauthorized, or ExpireAuthorization and PaymentDeclined (reason REAUTH_DECLINED).
VoidUnboundAuthorizationsthis contextNo CheckoutCompleted after 24 h: void.AUTHORIZED → VOIDED; PaymentReleased (reason VOIDED).
Idempotent consumptionthis contextEvery consumed event is applied once (inbox on the CloudEvents id).A repeated FulfillmentShipped does not capture twice; provider keys are also deterministic per (operation, authorizationId, groupNo).

6. Handled Commands​

CommandTriggerTransition
AuthorizePaymentcheckout, REST POST /payment-authorizations (provider Authorize)[*] → AUTHORIZED, or [*] → DECLINED
CaptureAllocationCaptureOnShipment policy on Kafka FulfillmentShipped (provider Capture)AUTHORIZED → PARTIALLY_CAPTURED, AUTHORIZED → CAPTURED, PARTIALLY_CAPTURED → PARTIALLY_CAPTURED, PARTIALLY_CAPTURED → CAPTURED; sets the allocation's trackingNumber
ReleaseAllocationReleaseOnCancellation policy on Kafka FulfillmentCancelled; ReleaseOnUnsourceable policy on Kafka FulfillmentUnsourceable (provider ReduceAuthorization(amount) for a partial release)the allocation becomes RELEASED; AUTHORIZED → AUTHORIZED, AUTHORIZED → RELEASED, PARTIALLY_CAPTURED → PARTIALLY_CAPTURED, PARTIALLY_CAPTURED → CAPTURED, PARTIALLY_CAPTURED → RELEASED
VoidAuthorizationcheckout, REST POST /payment-authorizations/{authorizationId}/voids; VoidUnboundAuthorizations policy (provider Void)AUTHORIZED → VOIDED
ExpireAuthorizationa provider decline on re-authorization (ReauthorizeBeforeExpiry)AUTHORIZED → EXPIRED, PARTIALLY_CAPTURED → EXPIRED
ReauthorizePaymentReauthorizeBeforeExpiry policy (provider Reauthorize)AUTHORIZED → AUTHORIZED (new expiresAt, new provider reference)
BindOrderBindOrderOnCheckoutCompleted policy on Kafka CheckoutCompletednone (sets orderId)

7. Created Events​

EventRaised whenFull CloudEvents type
PaymentAuthorizedAuthorizePayment is accepted by the provider and savedcom.retailnetwork.storefront.customer-payments.paymentauthorization.PaymentAuthorized
PaymentReauthorizedReauthorizePayment is saved (new expiresAt, providerAuthorizationId, plus orderId)com.retailnetwork.storefront.customer-payments.paymentauthorization.PaymentReauthorized
PaymentCapturedCaptureAllocation is savedcom.retailnetwork.storefront.customer-payments.paymentauthorization.PaymentCaptured
PaymentReleasedReleaseAllocation or VoidAuthorization is saved (reason is CANCELLED, UNSOURCEABLE, VOIDED or EXPIRED)com.retailnetwork.storefront.customer-payments.paymentauthorization.PaymentReleased
PaymentAuthorizationExpiredExpireAuthorization is savedcom.retailnetwork.storefront.customer-payments.paymentauthorization.PaymentAuthorizationExpired
PaymentDeclinedAuthorizePayment is declined by the provider and the DECLINED record is saved, or a re-authorization is declined (reason REAUTH_DECLINED); carries authorizationIdcom.retailnetwork.storefront.customer-payments.paymentauthorization.PaymentDeclined

All six are analytics-only by design (no network consumer exists yet). As built they go to retailnetwork.customer-payments.events (service ADR 0003); the design placed them on .analytics. BindOrder raises nothing. Events leave through the transactional outbox in the same transaction as the save.

8. Throughput​

Estimate. The model says "one per checkout session; captures per shipment group". One authorize per completed or abandoned session, then one capture or release per group over the following days; contention on one authorizationId is low and comes from shipment facts for several groups of the same order landing together (the version column absorbs them).

9. Size​

Estimate. The model says "one row per session + allocations". One root row with a handful of allocation rows, bounded by the number of shipment groups of the quote.

Refund​

1. Name​

Refund (refund.Refund), identified by refundId.

2. Description​

Money returned for one order or group: refundId, orderId, groupNo, authorizationId, amountMinor, reason and the outcome. Value object:

Value objectFieldsNote
RefundReasonLOST, RETURNED_TO_SENDER, MANUALThe first two come from the policies on PackageLost and PackageReturned; MANUAL is the ops route.

3. State Transitions​

Derived from: model.yaml → contexts[customer-payments].aggregates[Refund].states, .transitions, .commands, .notes.

RequestRefund creates the refund REQUESTED; the provider's answer (RecordRefundOutcome) settles it as SUCCEEDED or FAILED. SUCCEEDED is terminal. A FAILED refund is retryable: RetryRefund moves it back to REQUESTED, at most 5 attempts, after which it stays FAILED and an operator alert fires (H8).

4. Enforced Invariants​

InvariantEnforced by
Refunds of an order never exceed its captured total; REQUESTED and issued refunds count, FAILED never doesRequestRefund, RetryRefund → ErrOverRefund

Derived from: model.yaml → contexts[customer-payments].aggregates[Refund].invariants.

5. Corrective Policies​

PolicyOwnerRuleEffect on this aggregate
RefundOnLostPackagethis contextOn PackageLost refund the affected group's captured amount (reason LOST).RequestRefund; RefundIssued or RefundFailed.
RefundOnReturnToSenderthis contextOn PackageReturned refund the affected group's captured amount (reason RETURNED_TO_SENDER).RequestRefund; RefundIssued or RefundFailed.
Idempotent consumptionthis contextEvery consumed event is applied once (inbox on the CloudEvents id); the provider key is deterministic per (operation, authorizationId, groupNo).A repeated fact does not refund twice at the provider.

6. Handled Commands​

CommandTriggerTransition
RequestRefundStorefront ops, REST POST /refunds; the two refund policies on Kafka PackageLost, PackageReturned (provider Refund)[*] → REQUESTED
RecordRefundOutcomeThe provider's answer to RefundREQUESTED → SUCCEEDED, REQUESTED → FAILED
RetryRefundStorefront ops or an automatic retry, up to 5 attempts (the model's rest list has no route for it)FAILED → REQUESTED

7. Created Events​

EventRaised whenFull CloudEvents type
RefundIssuedRecordRefundOutcome settles the refund SUCCEEDEDcom.retailnetwork.storefront.customer-payments.refund.RefundIssued
RefundFailedRecordRefundOutcome settles the refund FAILED (the payload carries a code)com.retailnetwork.storefront.customer-payments.refund.RefundFailed

Both are analytics-only.

8. Throughput​

Estimate. The model says "low". A refund follows a lost or returned package, or an ops request.

9. Size​

Estimate. The model says "one row per refund".

Read models​

Read modelSource eventsServesWhy it is not an aggregate
ParkedFulfillmentFactsorder-sourcing FulfillmentShipped, FulfillmentCancelled, FulfillmentUnsourceable; shipping-and-tracking PackageLost, PackageReturned, for orders not yet bound to an authorizationBindOrderOnCheckoutCompleted, which applies the parked facts after the bindIt protects no invariant: it holds facts to apply, and the commands they cause are validated by the aggregates. Rebuildable only by replaying the topics, which is why the model parks instead of dropping (gap S4). Retained 30 days, then purged; a gauge reports the parked count (H10).

Derived from: model.yaml → contexts[customer-payments].read_models.

Model gaps and hotspots​

Every hotspot raised on EventStorming is resolved in the model-fix (H1 to H12). The resolutions that land on these aggregates:

#ResolutionWhere it shows
H1VoidAuthorization raises PaymentReleased{reason: VOIDED}; ReauthorizePayment has the transition AUTHORIZED → AUTHORIZED and raises PaymentReauthorized.Events table, state diagram
H2ReauthorizeBeforeExpiry (at expiresAt minus 24 h); a provider decline moves the authorization to EXPIRED and raises PaymentDeclined{reason: REAUTH_DECLINED}.Policies table
H3State derived from the allocations; AUTHORIZED → CAPTURED and the ReleaseAllocation transitions exist.State diagram
H4Provider port verb ReduceAuthorization(amount) for a partial release.Commands table
H5FulfillmentUnsourceable is consumed (ReleaseOnUnsourceable); PaymentReleased.reason is CANCELLED, UNSOURCEABLE, VOIDED or EXPIRED.Policies table
H6Nullable Allocation.trackingNumber, set at capture.Value objects
H7PaymentDeclined carries authorizationId.Events table
H8RetryRefund (FAILED → REQUESTED, max 5 attempts); ErrOverRefund counts REQUESTED and issued refunds, never FAILED.Refund state diagram
H9All events are analytics-only by design.Events table
H10Parked facts retained 30 days, then purged; gauge on the parked count.Read models
H11The description is complete.Section 2 of this aggregate
H12Refund and RefundIssued carry groupNo and authorizationId.Refund description