SR-017 — Payment Transaction Ledger & Security System Requirements
- Status: Approved
- Document ID: SR-2026-PAY-002
- Date: 2026-10-05
- Authors: Engineering Team
Traces back to BRD-016 — Payment Transaction Ledger, Refund Guarantee & Data Security.
Functional Requirements
Transaction Ledger & Atomicity
FR-01 — Append-Only Debit/Credit/Failed-Attempt Ledger.
Every webhook-confirmed payment event is persisted as a payment_ledger row with a transaction_type of CREDIT (capture), DEBIT (refund), or FAILED_ATTEMPT, and an event_status of CAPTURED/REFUNDED/FAILED. The table is insert-only — no update or delete path exists anywhere in payment-service for an existing row.
Satisfies BRD-016 BR-01.
FR-02 — Atomic Idempotency-Gate + Ledger Commit.
The webhook idempotency-gate record (razorpay_events, unique on Razorpay’s own event id) and its corresponding payment_ledger row are written inside one sea_orm::DatabaseTransaction, opened once per webhook delivery by RazorpayWebhookService::process and passed explicitly into both PaymentEventRepositoryTrait::insert_if_new and PaymentLedgerRepositoryTrait::append. The transaction commits only if both writes succeed, and rolls back as one unit otherwise — a redelivered event that fails the idempotency check never reaches the ledger insert.
Satisfies BRD-016 BR-02.
FR-03 — Secondary Ledger-Level Idempotency Check.
Each payment_ledger row also records the triggering event’s id as source_event_id, unique-indexed as a second, independent duplicate check that does not rely solely on the razorpay_events gate.
Supports BRD-016 BR-01, BR-02.
Refund on Fulfillment Failure
FR-04 — RefundPayment RPC for Saga Compensation.
payment-service exposes RefundPayment over gRPC (paymentorder.proto), wrapping RazorpayRepositoryTrait::refund_payment. order-service’s checkout saga calls it as a compensating action whenever payment has already been captured but a later step (shipment creation) subsequently fails, and records the outcome as a StepRefundPayment entry (StepStatusCompleted/StepStatusFailed) in its own saga history, together with a ProcessStatusPaymentRefunded/ProcessStatusPaymentRefundFailed order-status transition.
Satisfies BRD-016 BR-03.
FR-05 — A Refund’s Ledger Entry Is Webhook-Driven, Not RPC-Driven.
A successful RefundPayment call does not itself write a payment_ledger row. The DEBIT/REFUNDED entry is written only once Razorpay’s own refund.processed webhook is received and verified, via RefundProcessedStrategy, under the same atomic commit described in FR-02. The ledger’s source of truth is always the payment provider’s own confirmed outcome, not the business’s own outbound call succeeding — see ADR-00028.
(Gap — the RefundPayment RPC call site has no audit-trail publish of its own, and no reconciliation sweep independently confirms the refund if that webhook is never delivered — tracked as Technical Debt TD-01 and TD-02.)
Satisfies BRD-016 BR-03.
Payment Provider Audit Trail
FR-06 — Audit Publish at Order Creation and Webhook Receipt.
create_payment_order and the Razorpay webhook handler each fire-and-forget publish the full, unmasked request/response JSON to document-service’s audit-trail sink, satisfying BRD-016 BR-04 for those two call sites.
(Gap — refund_payment has no equivalent audit-publish call; tracked as Technical Debt TD-01.)
Non-Functional Requirements
Security & Data Minimization
NFR-01 — No Card/Payment-Credential Storage.
payment-service never receives or persists raw card numbers, CVV, or other card credentials — Razorpay’s own hosted checkout UI performs card entry and tokenization entirely outside this system’s boundary. pci-service’s encrypted card-attribute store exists as shared platform infrastructure but has no caller or integration from payment-service today — a deliberate scope-reduction posture, not a missing integration. It is the reserved target for a future saved-payment-method capability (storing a card token for faster reuse on a later order) — already named as future scope in BRD-016’s Out-of-Scope — not a currently-planned integration with a committed date.
Satisfies BRD-016 BR-05.
Decision: ADR-00031 — Razorpay-Hosted Checkout UI for Card Capture vs. In-House Card Entry + pci-service Integration Now, also stated as SR-016 NFR-05.
Reliability & Atomicity
NFR-02 — Explicit Transaction Handle on Ledger-Writing Repositories.
PaymentEventRepositoryTrait and PaymentLedgerRepositoryTrait take an explicit &sea_orm::DatabaseTransaction parameter rather than owning their own DatabaseConnection — a deliberate deviation from this service’s other repositories, needed specifically because the raw event record and its derived ledger entry must commit together or not at all.
Satisfies BRD-016 BR-02.
NFR-03 — Per-Endpoint Provider Repository (cross-reference).
The one-method-per-Razorpay-endpoint repository split (create_order, refund_payment on RazorpayRepositoryTrait) is already stated in SR-016 NFR-01 — not repeated here.
Related Documents
- BRD-016 — Payment Transaction Ledger, Refund Guarantee & Data Security
- 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
- Architecture — Payment Transaction Ledger, Refund Compensation & Gateway Audit Trail
- Technical Debt — Payment Transaction Ledger & Refund Audit Gaps
- SR-016 — Payment Capture & Confirmation System Requirements
- SR-014 — Checkout Completion: Payment Capture & Fulfillment Handoff System Requirements
- payment-service
- order-service