ADR-00028 — Append-Only, Webhook-Anchored Ledger vs. Synchronous or Mutable-Balance Alternatives for Payment Transaction Records
- Status: Accepted
- Date: 2026-10-05
- Authors: Engineering Team
Context
BRD-016 requires a permanent, tamper-evident financial record of every payment event (BR-01) that is never written in a half-finished state (BR-02), and an automatic refund guarantee that must actually reflect what happened at the payment provider (BR-03). payment-service needed to decide both when a payment_ledger row is written relative to its outbound calls to Razorpay and its inbound webhooks, and what shape that record takes.
Decision Drivers (FRs & NFRs)
- SR-017 FR-01 — append-only ledger, one row per real event.
- SR-017 FR-02 — atomic commit with the webhook idempotency gate.
- SR-017 FR-05 — a refund’s ledger entry must reflect the provider’s own confirmed outcome.
- SR-016 FR-02, FR-04 — signature-verified, idempotently-persisted webhook events (the mechanism this decision builds directly on top of).
Options Evaluated
Option 1 — Append-only ledger, written only from the verified webhook handler, atomically with the idempotency gate (CHOSEN)
As implemented: payment_ledger rows are inserted exclusively by the three WebhookEventStrategy implementations (PaymentCapturedStrategy, PaymentFailedStrategy, RefundProcessedStrategy), inside the same DatabaseTransaction as the razorpay_events idempotency insert. Neither create_payment_order nor refund_payment writes a ledger row directly — both only make the outbound call and return its immediate result to their caller.
Pros:
- The ledger reflects the payment provider’s own confirmed outcome, not just “we made an outbound call and it returned success” — a network blip between
payment-serviceand Razorpay that drops the response can’t create a ledger entry for money that was never actually captured or refunded. - Atomic commit with the idempotency gate means the ledger and the dedup record can never disagree — a crash mid-write rolls both back together, satisfying BR-02 exactly.
- An immutable, insert-only history gives Finance an audit trail that can’t be silently edited after the fact, directly serving BR-01.
Cons & Accepted Trade-offs:
- A successful outbound call (
create_payment_orderorrefund_payment) has no ledger entry until its matching webhook arrives and is processed — there is a window, bounded by webhook delivery latency, where the ledger doesn’t yet reflect an action the business itself just took. Forrefund_paymentspecifically this also means there is no audit-trail record at the call site itself (tracked as TD-01), and no reconciliation sweep independently confirms the refund if that webhook is lost outright (tracked as TD-02) — the same risk class already accepted on the capture side by TD-04.
Option 2 — Write a ledger entry synchronously at the point of the outbound call, reconciled later if the webhook disagrees
Pros: Immediate visibility — the ledger reflects “we asked Razorpay to refund this” the moment the RPC returns, with no dependency on webhook delivery timing.
Cons (Why Rejected): Risks recording a ledger entry for an action the gateway never actually completed — e.g. a request that times out on payment-service’s side after Razorpay had already queued or silently rejected it downstream of the HTTP response. Reconciling a synchronous entry against a later, possibly-contradicting webhook needs its own correction/reversal mechanism that an append-only, insert-only ledger shape doesn’t have — and adding one would weaken the “never overwritten” guarantee BR-01 depends on. This option couples ledger correctness to the outbound call’s own reliability rather than to the provider’s verified confirmation, which is the opposite of what BR-01/BR-02 ask for.
Option 3 — Mutable running-balance/status field per order instead of an append-only ledger
Pros: Simpler to query “what’s the current payment state of this order” — one row, one read, no need to aggregate a history.
Cons (Why Rejected): Loses the event-by-event history Finance needs for a dispute or an audit — a mutated status field can’t represent that a payment was captured, then later refunded, as two distinct dated facts; it can only show the most recent one. This directly conflicts with BR-01’s “never overwritten” requirement. It also reintroduces a concurrent-write race the current one-row-per-event append avoids for free — two webhooks for the same order updating one shared mutable row needs its own locking story that an insert-only table doesn’t.
Final Decision
Option 1 is selected and already implemented. The payment_ledger’s source of truth is always the payment provider’s own verified webhook, written atomically with the webhook idempotency gate inside one database transaction — never synchronously at the outbound RPC call site, and never as a mutable per-order balance. The accepted cost is a webhook-delivery-dependent window on both the capture and refund sides, with the refund side additionally missing an audit-trail publish at its own RPC call site — both tracked in Technical Debt — Payment Transaction Ledger & Refund Audit Gaps.
Related Documents
- BRD-016 — Payment Transaction Ledger, Refund Guarantee & Data Security
- SR-017 — Payment Transaction Ledger & Security System Requirements
- Architecture — Payment Transaction Ledger, Refund Compensation & Gateway Audit Trail
- Technical Debt — Payment Transaction Ledger & Refund Audit Gaps
- ADR-00027 — Asynchronous Saga + Webhook Relay vs. Synchronous Payment Capture for Checkout Completion — the same webhook-anchored-truth posture, applied there to saga progression and here to the financial ledger
- Microservices: payment-service, order-service