Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Capstone: Resilient E-Commerce Order Pipeline

Track

Design-only

Brief

Your five-person commerce team is replacing a checkout function that mixes order validation, gateway calls, refunds, and customer messaging. Card, wallet, and bank-transfer payments must share one checkout flow, while support staff need an auditable way to refund a captured charge. A recent gateway slowdown caused unchecked retries to duplicate messages and saturate the service. Design the order pipeline so new payment methods and notification channels can be added without rewriting its core flow.

Constraints

  • Card numbers must never enter application logs or storage; a PCI-compliant vault returns single-use tokens.
  • Checkout should acknowledge a successfully authorized order within 800 ms at p95, excluding asynchronous notifications.
  • Five developers maintain an existing modular JVM application and cannot introduce a new broker this quarter.
  • Gateway retries are capped at three attempts with exponential backoff and jitter; every attempt must reuse the payment idempotency key.
  • Auditors require the actor, command, outcome, and timestamp for every capture and refund, retained for seven years.

Required Patterns

  • Builder — WHY: assembly has ordered steps and cross-field rules; a builder can validate lines, totals, currency, and payment token before producing an immutable order.
  • Strategy — WHY: card, wallet, and bank transfer are interchangeable payment algorithms behind one contract, with selection kept outside the checkout algorithm.
  • Command — WHY: capture and refund become explicit, serializable operations with compensation, actor metadata, and a durable audit history rather than undocumented method calls.
  • Observer — WHY: receipt, analytics, and fraud-review subscribers can react to committed order events without coupling those side effects to payment execution.

Solution Sketch

Illustrative pseudocode — not runnable.

order = OrderBuilder()
  .customer(input.customerId)
  .lines(input.lines)
  .currency(input.currency)
  .paymentToken(input.vaultToken)     # never a card number
  .recalculateTotals()
  .validateOrReject()                 # no payment before this succeeds
  .buildImmutable()

payment = PaymentRegistry.forMethod(order.method)
command = CaptureCommand(order.id, payment, actor, idempotencyKey)

result = CommandJournal.executeOnce(command):
  append(CaptureRequested, actor, timestamp)
  outcome = payment.authorize(order.total, retryPolicy = {
    attempts: 3, backoff: exponential, jitter: full,
    idempotencyKey: command.idempotencyKey
  })
  append(CaptureSucceeded or CaptureFailed, outcome, timestamp)
  commitOrderAndOutboxAtomically(order, outcome)

after commit:
  EventBus.publish(OrderPaid or PaymentFailed)
  subscribers = [ReceiptObserver, AnalyticsObserver, FraudReviewObserver]
  each subscriber consumes independently with its own deduplication key

refund = RefundCommand(originalCaptureId, amount, actor, reason)
refund.compensate():
  require originalCaptureId exists and refundableBalance >= amount
  gateway.refund(originalCaptureId, refund.idempotencyKey)
  appendRefundAudit()                 # compensation never erases capture history

Rubric

  • Order validity: Passes if the builder rejects missing lines, invalid totals, or currency mismatches before any payment strategy is invoked; fails if gateways receive partially assembled orders.
  • Variant ownership: Passes if a registry or composition root selects the payment strategy and the order flow contains no card/wallet/bank-transfer branches.
  • Retry control: Passes if exactly one named payment boundary owns the three-attempt policy, backoff, jitter, and stable idempotency key; fails if observers or commands also retry gateway calls.
  • Refund semantics: Passes if refund references the original capture, checks the refundable balance, and appends a compensating audit record without deleting or rewriting the capture.
  • Notification timing: Passes if observers receive only committed outcomes and each subscriber deduplicates delivery independently; fails if a slow subscriber can repeat or roll back payment.
  • Audit and PCI scope: Passes if the journal records actor, command, outcome, and timestamp while all examples exclude raw card data and retain stable correlation IDs.