Architecture — Payment Transaction Ledger, Refund Compensation & Gateway Audit Trail
By Engineering Team•October 5, 2026•6 min read
| Metadata | Details |
|---|---|
| System | Payment Transaction Ledger, Refund Compensation & Gateway Audit Trail |
| Document ID | ARCH-2026-PAY-002 |
| Author | Engineering Team |
| Status | Approved |
Implements SR-017, per the decision in ADR-00028. This doc describes payment-service’s own ledger, refund, audit, and security detail; it does not re-describe the checkout saga’s step sequencing or the webhook capture/relay mechanics, both already covered in full by Architecture — Checkout Completion: Payment Capture and Fulfillment Handoff (“00012” below).
Diagram

Same physical diagram and topology as 00012 — this doc describes the ledger, refund, audit, and security detail behind the same payment-service/order-service/Razorpay/document-service boxes already shown there, not a new system.
Components
| Component | Language / Runtime | Role in this flow |
|---|---|---|
payment-service |
Rust, Postgres (SeaORM) | Owns payment_ledger’s append-only write path, refund_payment’s RPC handling, the two audit-publish call sites, and the no-card-data security boundary — SR-017 FR-01 through FR-06, NFR-01. |
order-service |
Go, Temporal | Calls RefundPayment as the checkout saga’s fulfillment-failure compensating action, records the outcome in order_orchestrations — SR-017 FR-04. |
| Razorpay | External payment gateway | Source of truth for both captures and refunds — payment-service only commits a ledger entry once Razorpay’s own webhook confirms the outcome, never on the strength of its own outbound call succeeding. |
document-service |
Rust, MongoDB + GCS | Masks and persists the audit event published at order creation and webhook receipt — reused as-is; see SR-017 FR-06 and 00012’s own description of this sink. |
pci-service |
Rust, encrypted attribute store (not integrated) | Exists as shared platform infrastructure for encrypted card-attribute storage, but has zero callers from payment-service — included here only to state the security boundary explicitly, not because it participates in this flow. Reserved as the future target for a saved-payment-method/card-token-for-reference capability, not yet built. |
End-to-end flow (A) — Ledger write path
create_payment_orderandrefund_paymenteach call out to Razorpay (order creation / refund request respectively) and return the gateway’s immediate response to their own caller — neither writes apayment_ledgerrow itself. See SR-017 FR-05 and ADR-00028 for why.- Razorpay’s webhook (
captured,failed, orrefund.processed) arrives atPOST /payment/webhook/razorpay— signature verification and strategy dispatch are described in full by 00012’s flow (B); this doc picks up at the point aWebhookEventStrategyhas already been selected. RazorpayWebhookService::processopens onesea_orm::DatabaseTransactionand passes it explicitly into bothPaymentEventRepositoryTrait::insert_if_new(therazorpay_eventsidempotency-gate insert) andPaymentLedgerRepositoryTrait::append(thepayment_ledgerinsert) — SR-017 FR-02. The transaction commits only if both succeed.- The ledger row records
transaction_type(CREDIT/DEBIT/FAILED_ATTEMPT),event_status(CAPTURED/REFUNDED/FAILED), the amount in paise, andsource_event_id— a second, independent idempotency check beyond therazorpay_eventsgate — SR-017 FR-01, FR-03. - Because the table is insert-only, this is the only place in
payment-serviceapayment_ledgerrow is ever written — there is no update/delete path to reconcile against.
End-to-end flow (B) — Refund-on-fulfillment-failure, payment-service side
- 00012’s flow (C), step 1 already describes when this compensation fires: a shipment-creation failure inside the checkout saga, after payment has already been captured. This doc describes what happens once the saga’s
RefundPaymentActivityactually reachespayment-service. order-service’s saga callspayment-service’sRefundPaymentgRPC RPC (paymentorder.proto).refund_payment(paymentorder_svc_impl.rs) callsRazorpayRepositoryTrait::refund_payment— one method, one Razorpay endpoint, per the repository-per-provider pattern already established forcreate_order(the platform’s layered architecture standard) — and returns the immediaterazorpay_refund_id/statusto the saga.- The saga records this immediate result as
StepRefundPayment/StepStatusCompleted(orFailed) and aProcessStatusPaymentRefunded/ProcessStatusPaymentRefundFailedorder-status transition — SR-017 FR-04. This saga-level record is not the same thing as a ledger entry — nopayment_ledgerrow exists yet at this point. - Independently, and later, Razorpay delivers its own
refund.processedwebhook.RefundProcessedStrategyruns through the exact same driver (flow A, steps 2–4 above) as a capture event — same transaction, same idempotency gate, same insert-only table — writing theDEBIT/REFUNDEDrow that is the refund’s actual permanent financial record. - Gap, by design and tracked, not accidental: step 2’s
RefundPaymentcall has no audit-trail publish of its own (unlikecreate_payment_order), and no process independently re-confirms the refund with Razorpay if its webhook is never delivered — Technical Debt TD-01, TD-02.
End-to-end flow (C) — Payment-provider audit trail and the data-security boundary
create_payment_orderand the webhook handler each fire-and-forget publish (tokio::spawn) the full, unmasked request/response JSON todocument-service’s audit topic viaDocumentAuditPublisherTrait::publish_audit_event— never a raw Pub/Sub client held inservice/— SR-017 FR-06.document-servicemasks the payload once, at persistence time, before writing it to GCS — the same subscribe-only, mask-once sink 00012 already describes; this doc does not repeat its internals.refund_paymentis the one call site inpayment-servicewith no equivalent publish call — the gap named in flow (B), step 5.- Separately from the audit trail: no card number, CVV, or other card credential is ever received or persisted by
payment-service, at any step in flows (A) or (B) above — Razorpay’s own hosted checkout UI performs card entry and tokenization entirely outside this system’s boundary.pci-service’s encrypted card-attribute store is available platform infrastructure but has no caller here — a deliberate scope-reduction posture, not a missing integration — SR-017 NFR-01. It is the service this flow would wire into if/when a saved-payment-method capability (storing a card token for faster reuse on a later order) is built — code for that EAV store already exists and is production-ready viapii-service’s shared pattern, it simply has no caller frompayment-serviceyet.
Why this is reliable
- The ledger’s truth always comes from the provider, never from the business’s own outbound call succeeding. Every ledger row — capture or refund — is written only once Razorpay’s own webhook confirms it, inside the same atomic transaction as the idempotency gate; see ADR-00028.
- A record is never half-written. The idempotency-gate insert and the ledger insert share one database transaction — either both land or neither does, satisfying BRD-016’s BR-02.
- Refund-on-failure is already a real, end-to-end guarantee, not aspirational. The saga’s compensation call, the RPC, and the webhook-driven ledger write are all live code today — the only accepted gaps are the missing audit-publish at the RPC call site and the absence of an independent reconciliation sweep, both explicitly tracked rather than silently missing.
- Card data genuinely never enters this system. The security boundary isn’t a policy statement with no enforcement behind it — there is no code path in
payment-servicethat receives, parses, or stores raw card fields at all.
Related Documents
- BRD-016 — Payment Transaction Ledger, Refund Guarantee & Data Security
- SR-017 — Payment Transaction Ledger & Security System Requirements
- ADR-00028 — Append-Only, Webhook-Anchored Ledger vs. Synchronous or Mutable-Balance Alternatives for Payment Transaction Records
- ADR-00031 — Razorpay-Hosted Checkout UI for Card Capture vs. In-House Card Entry + pci-service Integration Now — the decision behind flow (C), step 4’s no-card-data boundary
- Technical Debt — Payment Transaction Ledger & Refund Audit Gaps
- Architecture — Checkout Completion: Payment Capture and Fulfillment Handoff — saga step sequencing and webhook capture/relay mechanics this doc builds on
- Technical Debt — Checkout Completion Payment Flow Gaps — TD-04, the capture-side precedent for this doc’s TD-02
- Microservices: payment-service, order-service