Architecture — Checkout Completion: Payment Capture and Fulfillment Handoff

By Engineering Team•October 5, 2026•7 min read
Metadata Details
System Checkout Completion — Payment Capture & Fulfillment Handoff
Document ID ARCH-2026-OPF-001
Author Engineering Team
Status Approved

Implements SR-014 and SR-016, per the decision in ADR-00027. Picks up immediately after order creation, which Architecture — Reusable Product-to-Order Orchestration already covers in full; this doc does not re-describe checkout-session resolution or order creation itself.


Diagram

Checkout completion payment flow — order-service's checkout saga, payment-service's Razorpay integration, and the document-service audit sink


Components

Component Language / Runtime Role in this flow
order-service Go, Temporal workflow/activities (cmd/worker) + gRPC/REST API (cmd/api), MongoDB Owns the CheckoutSagaWorkflow — the step sequence, the compensation logic, the real-time progress stream, and the saga-signal RPCs a webhook relay calls into — SR-014 FR-01, FR-02, FR-08, FR-09.
Temporal (workflow engine) — (temporal frontend service, port 7233; temporal-ui on 8233) Durably persists the saga’s workflow history, drives retries per defaultActivityOptions (30s per-attempt timeout, 3 attempts), and delivers the payment_captured signal into the running workflow instance. Not application code order-service owns — reused Temporal-server infrastructure.
payment-service Rust, gRPC (CreatePaymentOrder) + REST (POST /payment/webhook/razorpay), Postgres (SeaORM) Wraps Razorpay’s order-creation API behind repository/razorpay/ (one method per endpoint), persists payment_gateway_orders/razorpay_events/payment_ledger, and relays a verified capture back into the saga — SR-016 FR-01 through FR-04.
Razorpay External payment gateway Hosts the actual checkout UI the customer completes payment on, and is the source of truth for whether a payment was captured — delivered back to payment-service only via its own webhook, never inferred from the client’s own success callback alone.
shipment-service Go, gRPC server, MongoDB Receives the saga’s shipment-creation call and runs its own existing multi-step creation + automatic courier-fallback sequence — reused, not re-implemented, by this saga — SR-014 FR-07; see Architecture — Multi-Courier Fulfillment via Shiprocket.
document-service Rust, MongoDB + Firebase Storage/GCS Subscribes to the audit event both Razorpay order creation and the webhook handler fire-and-forget publish, masks PII, and persists the masked JSON — reused as-is; this doc does not re-describe its internals, see its own 06-microservices doc and ADR-00002.
ui-application Flutter Opens Razorpay’s hosted checkout UI once the saga publishes a payment_ready event, then watches the same order-level SSE stream for shipment_created/saga_failed — see the UI walkthrough docs linked below.

What the customer sees, screen by screen

  • Payment Method & Razorpay Payment Flow — the screen that opens Razorpay’s checkout UI on payment_ready and waits on the order-level SSE stream.
  • Order Confirmation — reached once that stream reports shipment_created.
  • Track Shipment — the separate, live shipment-service SSE stream reached from Order Confirmation, My Orders, or Order Details once a shipment actually exists.

End-to-end flow (A) — Saga scheduling and real Razorpay order creation

  1. Order creation (per Architecture — Reusable Product-to-Order Orchestration, flow D) schedules a CheckoutSagaWorkflow and returns immediately — the request never blocks on any of the steps below — SR-014 FR-01.
  2. A dedicated Temporal worker (a separate order-service process polling the checkout-saga task queue, confirmed running per SR-014 NFR-01) picks up the workflow and executes ReserveInventoryActivity first; a failure here fails the saga directly, since nothing has been reserved yet to release.
  3. The saga calls payment-service’s CreatePaymentOrder over gRPC (CreatePaymentOrderActivity, up to 3 attempts, 30s each). payment-service calls Razorpay’s order-creation endpoint via repository/razorpay/create_order (one method per Razorpay endpoint, never an aggregate method — SR-016 NFR-01), persists a payment_gateway_orders row, and fire-and-forget publishes a document-audit event carrying the full, unmasked request/response — SR-016 NFR-02. A failure at this step compensates by releasing the inventory reserved in step 2 and fails the saga — SR-016 FR-01, BRD-013 BR-04.
  4. On success, the saga itself — not a separate activity — publishes a payment_ready event onto the same order-level SSE stream, carrying the real razorpay_order_id/razorpay_key_id payment-service just returned plus the amount/currency the saga already had as its own input.

End-to-end flow (B) — Razorpay webhook capture and saga relay

  1. The customer completes payment on Razorpay’s own hosted checkout UI, opened by the ui-application using the payment_ready event’s fields.
  2. Razorpay delivers a webhook to payment-service’s POST /payment/webhook/razorpay — the one REST route in an otherwise gRPC-only service. The handler HMAC-verifies the signature against Razorpay’s webhook secret before parsing or acting on anything the payload claims — SR-016 FR-02.
  3. A WebhookEventStrategy dispatches by event type. PaymentCapturedStrategy persists one razorpay_events row (keyed by Razorpay’s own event id, so a redelivered webhook neither duplicates the ledger nor re-signals the saga — SR-016 FR-04) and one payment_ledger row atomically inside a single Postgres transaction, then calls order-service’s PaymentSignalService.SignalPaymentCaptured over gRPC, carrying tenant/user/role metadata like any other outbound RPC — SR-014 NFR-02.
  4. That signal wakes the saga’s waitForPaymentCaptured step — a workflow.Selector blocking on either this signal or a 15-minute timer — and the saga proceeds to shipment creation (flow C). If the timer fires first (no capture signal within 15 minutes), the saga compensates by releasing inventory and fails outright.
  5. PaymentFailedStrategy/RefundProcessedStrategy persist their own razorpay_events/payment_ledger rows the same way, but do not call any saga-signal RPC — SR-016 FR-03 explicitly scopes saga relay to the captured event only today. A hard payment failure therefore resolves no faster than the 15-minute timeout above — tracked as TD-01.
  6. The webhook handler, like Razorpay order creation in flow A, fire-and-forget publishes a document-audit event with the full unmasked webhook payload — SR-016 NFR-02.

End-to-end flow (C) — Shipment handoff and live progress

  1. Once the saga’s payment-wait step is unblocked, CreateShipmentActivity hands off to shipment-service’s existing shipment-creation sequence (Shiprocket login → create order → assign AWB, with automatic courier fallback) — reused, not re-implemented — SR-014 FR-07; see Architecture — Multi-Courier Fulfillment via Shiprocket for that sequence’s own detail. A failure here compensates by both refunding the payment (via a RefundPaymentActivity call back into payment-service) and releasing inventory, then fails the saga — the one point in the flow where payment has already been captured before a failure is detected, so refund (not just release) is the correct compensating action.
  2. ConsumeInventoryActivity runs as a best-effort final step, converting the reservation into a permanent deduction — BRD-013 BR-03. It never fails the saga regardless of its own outcome, a gap tracked as TD-05.
  3. Throughout every step, the saga’s current step and step history are tracked only in order_orchestrations — never on the order document itself — SR-014 FR-08.
  4. The customer watches this entire sequence over order-service’s SSE stream (GET /api/v1/order/:id/stream) without polling — SR-014 FR-09 — landing on Order Confirmation once shipment_created fires, or an ambiguous failure state if saga_failed fires (or never fires before the client’s own local wait gives up).

End-to-end flow (D) — Resolving a delayed webhook after the saga has already given up

  1. If SignalPaymentCaptured is called after the original saga’s 15-minute wait has already timed out and compensated, the signal call itself fails with a workflow-not-found error rather than being silently swallowed.
  2. order-service catches that specific failure and falls back to a dedicated reconciliation path, reading the persisted order_orchestrations record to confirm the order hasn’t already been resolved (either completed normally, or already reconciled by a prior late signal) before doing anything.
  3. If genuinely unresolved, it starts a separate LatePaymentReconciliationWorkflow (a distinct workflow ID from the original saga, so a second late/duplicate signal collides with this run rather than starting a third) that re-attempts inventory reservation.
  4. Stock still available → reservation succeeds, the workflow proceeds through the same shipment-creation and inventory-consumption steps as the original saga — the order is reconfirmed and shipped. Stock no longer available → reservation fails, the payment is refunded. Either way, the delayed confirmation resolves to a definite, recorded outcome — BRD-015 BR-02.
  5. This reconciliation path only ever runs in response to an actual webhook delivery — there is no scheduled sweep that would discover a captured payment whose webhook is never (re)delivered at all. That residual risk is tracked as TD-04.

Why this is reliable

  • The order-creation request is never held hostage by a third party’s checkout UI. Scheduling the saga and returning immediately (flow A, step 1) means the backend’s own request latency has nothing to do with how long a customer takes to pay — see ADR-00027 for the full trade-off against a synchronous alternative.
  • The amount Razorpay charges can never diverge from the saga’s own trustworthy total. Flow A, step 3’s CreatePaymentOrder call is always driven by the saga’s own already-validated input, never by anything the client supplies at this step — see ADR-00029 for why this is server-side only.
  • Only Razorpay’s own signature-verified webhook is ever trusted to say a payment succeeded. Flow B’s SignalPaymentCaptured relay is reachable solely from the verified webhook handler — the client’s own checkout-success callback (flow B, step 1) is a UI cue only, never a trust signal the saga acts on — see ADR-00030.
  • Every failure branch has a compensating action, not a dead end. Inventory reservation failing, Razorpay order creation failing, the payment-wait step timing out, and shipment creation failing each run their own compensation (release, or release-plus-refund) before the saga terminates — directly satisfying BRD-013’s BR-04.
  • A late confirmation is still a resolvable event, not a lost one. Because the saga’s state persists independently of the workflow’s own lifetime, a webhook arriving after the original saga has already compensated still reaches a dedicated reconciliation workflow rather than being dropped — flow D, directly implementing BRD-015’s BR-02.
  • Idempotent persistence protects against duplicate webhook delivery. razorpay_events keyed by the provider’s own event id means a redelivered webhook is a safe no-op, not a double-applied ledger entry — BRD-015’s BR-03.