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

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_readyand waits on the order-level SSE stream. - Order Confirmation — reached once that stream reports
shipment_created. - Track Shipment — the separate, live
shipment-serviceSSE 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
- Order creation (per Architecture — Reusable Product-to-Order Orchestration, flow D) schedules a
CheckoutSagaWorkflowand returns immediately — the request never blocks on any of the steps below — SR-014 FR-01. - A dedicated Temporal worker (a separate
order-serviceprocess polling the checkout-saga task queue, confirmed running per SR-014 NFR-01) picks up the workflow and executesReserveInventoryActivityfirst; a failure here fails the saga directly, since nothing has been reserved yet to release. - The saga calls
payment-service’sCreatePaymentOrderover gRPC (CreatePaymentOrderActivity, up to 3 attempts, 30s each).payment-servicecalls Razorpay’s order-creation endpoint viarepository/razorpay/create_order(one method per Razorpay endpoint, never an aggregate method — SR-016 NFR-01), persists apayment_gateway_ordersrow, 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. - On success, the saga itself — not a separate activity — publishes a
payment_readyevent onto the same order-level SSE stream, carrying the realrazorpay_order_id/razorpay_key_idpayment-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
- The customer completes payment on Razorpay’s own hosted checkout UI, opened by the
ui-applicationusing thepayment_readyevent’s fields. - Razorpay delivers a webhook to
payment-service’sPOST /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. - A
WebhookEventStrategydispatches by event type.PaymentCapturedStrategypersists onerazorpay_eventsrow (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 onepayment_ledgerrow atomically inside a single Postgres transaction, then callsorder-service’sPaymentSignalService.SignalPaymentCapturedover gRPC, carrying tenant/user/role metadata like any other outbound RPC — SR-014 NFR-02. - That signal wakes the saga’s
waitForPaymentCapturedstep — aworkflow.Selectorblocking 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. PaymentFailedStrategy/RefundProcessedStrategypersist their ownrazorpay_events/payment_ledgerrows 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.- 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
- Once the saga’s payment-wait step is unblocked,
CreateShipmentActivityhands off toshipment-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 aRefundPaymentActivitycall back intopayment-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. ConsumeInventoryActivityruns 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.- Throughout every step, the saga’s current step and step history are tracked only in
order_orchestrations— never on theorderdocument itself — SR-014 FR-08. - 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 onceshipment_createdfires, or an ambiguous failure state ifsaga_failedfires (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
- If
SignalPaymentCapturedis 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. order-servicecatches that specific failure and falls back to a dedicated reconciliation path, reading the persistedorder_orchestrationsrecord to confirm the order hasn’t already been resolved (either completed normally, or already reconciled by a prior late signal) before doing anything.- 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. - 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.
- 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
CreatePaymentOrdercall 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
SignalPaymentCapturedrelay 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_eventskeyed 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.
Related Documents
- BRD-015 — Payment Capture & Confirmation
- SR-016 — Payment Capture & Confirmation System Requirements
- BRD-013 — Checkout Completion: Real Payment Capture and Reliable Fulfillment Handoff
- SR-014 — Checkout Completion: Payment Capture & Fulfillment Handoff System Requirements
- ADR-00027 — Asynchronous Saga + Webhook Relay vs. Synchronous Payment Capture for Checkout Completion
- ADR-00029 — Server-Side Razorpay Order Creation (payment-service) vs. Client-Constructed Payment Order
- ADR-00030 — Webhook-Verified Payment Outcome vs. Client-Reported Checkout Success as Source of Truth
- ADR-00031 — Razorpay-Hosted Checkout UI for Card Capture vs. In-House Card Entry + pci-service Integration Now — why card/PCI data collection stays entirely inside Razorpay’s hosted UI, not described in this doc’s own flow
- Architecture — Reusable Product-to-Order Orchestration — order creation and the cached checkout session this saga is scheduled from
- Architecture — Multi-Courier Fulfillment via Shiprocket — the shipment-creation sequence this saga hands off to
- Architecture — Inventory Stock Reservation, Consumption, and Concurrency Control — what’s actually inside
ReserveInventoryActivity/ConsumeInventoryActivity, treated as a black box here - Technical Debt — Checkout Completion Payment Flow Gaps
- Microservices: order-service, payment-service, shipment-service, document-service
- Flutter walkthrough: Payment Method & Razorpay Payment Flow, Order Confirmation, Track Shipment