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

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

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

  1. create_payment_order and refund_payment each call out to Razorpay (order creation / refund request respectively) and return the gateway’s immediate response to their own caller — neither writes a payment_ledger row itself. See SR-017 FR-05 and ADR-00028 for why.
  2. Razorpay’s webhook (captured, failed, or refund.processed) arrives at POST /payment/webhook/razorpay — signature verification and strategy dispatch are described in full by 00012’s flow (B); this doc picks up at the point a WebhookEventStrategy has already been selected.
  3. RazorpayWebhookService::process opens one sea_orm::DatabaseTransaction and passes it explicitly into both PaymentEventRepositoryTrait::insert_if_new (the razorpay_events idempotency-gate insert) and PaymentLedgerRepositoryTrait::append (the payment_ledger insert) — SR-017 FR-02. The transaction commits only if both succeed.
  4. The ledger row records transaction_type (CREDIT/DEBIT/FAILED_ATTEMPT), event_status (CAPTURED/REFUNDED/FAILED), the amount in paise, and source_event_id — a second, independent idempotency check beyond the razorpay_events gate — SR-017 FR-01, FR-03.
  5. Because the table is insert-only, this is the only place in payment-service a payment_ledger row is ever written — there is no update/delete path to reconcile against.

End-to-end flow (B) — Refund-on-fulfillment-failure, payment-service side

  1. 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 RefundPaymentActivity actually reaches payment-service.
  2. order-service’s saga calls payment-service’s RefundPayment gRPC RPC (paymentorder.proto). refund_payment (paymentorder_svc_impl.rs) calls RazorpayRepositoryTrait::refund_payment — one method, one Razorpay endpoint, per the repository-per-provider pattern already established for create_order (the platform’s layered architecture standard) — and returns the immediate razorpay_refund_id/status to the saga.
  3. The saga records this immediate result as StepRefundPayment/StepStatusCompleted (or Failed) and a ProcessStatusPaymentRefunded/ProcessStatusPaymentRefundFailed order-status transition — SR-017 FR-04. This saga-level record is not the same thing as a ledger entry — no payment_ledger row exists yet at this point.
  4. Independently, and later, Razorpay delivers its own refund.processed webhook. RefundProcessedStrategy runs 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 the DEBIT/REFUNDED row that is the refund’s actual permanent financial record.
  5. Gap, by design and tracked, not accidental: step 2’s RefundPayment call has no audit-trail publish of its own (unlike create_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

  1. create_payment_order and the webhook handler each fire-and-forget publish (tokio::spawn) the full, unmasked request/response JSON to document-service’s audit topic via DocumentAuditPublisherTrait::publish_audit_event — never a raw Pub/Sub client held in service/ — SR-017 FR-06.
  2. document-service masks 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.
  3. refund_payment is the one call site in payment-service with no equivalent publish call — the gap named in flow (B), step 5.
  4. 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 via pii-service’s shared pattern, it simply has no caller from payment-service yet.

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-service that receives, parses, or stores raw card fields at all.