An event-driven, enterprise-grade TypeScript background worker engineered for idempotent full and partial refund pipelines, pre-dispatch fraud velocity auditing, and distributed vendor payout reversal sagas.
[ Refund Trigger (Full / Partial) ]
│
▼
[ Idempotency & Cumulative Cap Guard ] ──(Enforce: Σ Refunds <= Captured BigInt)
│
▼
[ Pre-Dispatch Fraud Velocity Auditor ] ─(Score < 40: Approve │ >= 40: Hold/Reject)
│
▼
[ Pro-Rata Vendor Payout Reversal ] ─────(Clawback Connected Vendor & Fee Split)
│
▼
[ Acquiring PSP Refund Worker Queue ] ───(Full-Jitter Exponential Backoff)
│
┌────────────┴─────────────────────────┐
▼ (2xx Refund Settled) ▼ (Terminal Reject / Timeout)
[ Emit Double-Entry Contra-Journal ] [ Compensating Saga Rollback / DLQ Quarantine ]
Serving as the asynchronous reversal and risk-control bridge connecting checkout-flow-sdk (Inbound Capture), payout-gateway-service (Vendor Disbursement), and double-entry-ledger-core (Immutable Accounting), this worker enforces strict financial invariants:
- Cumulative Partial Refund Cap Invariant: Synchronously locks and validates that settled plus in-flight partial refunds never exceed the original captured payment amount using 64-bit
BigIntminor units (src/guards/cumulative-refund-cap-guard.ts). - Pre-Dispatch Fraud & Velocity Auditing: Evaluates 24-hour refund velocity, high-value thresholds, and account age heuristics prior to balance reversal, routing anomalous requests to manual review holds (
src/fraud/refund-fraud-auditor.ts). - Pro-Rata Connected Vendor Clawbacks: Computes exact pro-rata vendor balance reversals and platform fee adjustments (
src/reversals/vendor-payout-reversal-calculator.ts) with zero floating-point rounding drift. - Compensating Saga Orchestration: Coordinates multi-step state transitions between vendor balance clawbacks and acquiring PSP dispatches, automatically executing compensating vendor credits if downstream card networks terminally reject a refund (
src/saga/refund-saga-orchestrator.ts). - Double-Entry Contra-Journal Emission: Emits balanced compensating contra-entry journal batches (
src/ledger/contra-journal-emitter.ts) linked viareversesJournalIdtodouble-entry-ledger-core.
- Fencing-Token Distributed Saga Locks: Prevents concurrent workers from mutating the same parent capture state (
src/concurrency/saga-distributed-lock.ts). - Zero-Trust Webhook Attestation: Constant-time signature verification and 300-second replay window guards (
src/security/webhook-signature-verifier.ts). - Recursive PAN, IBAN & PII Scrubbing: Automated telemetry sanitization masking sensitive payment and banking identifiers (
src/telemetry/refund-pii-redactor.ts). - Formal Verification (TLA+): Triple mathematical specifications proving strict cumulative refund caps, saga rollback consistency, and fraud quarantine gate safety (
docs/formal-verification/). - High-Throughput Micro-Benchmarks: Deterministic benchmarks for
BigIntpro-rata allocation, fraud velocity scoring, 64-bit fingerprinting, Full-Jitter backoff, and saga compensation (benchmarks/).
We treat refund integrity, fraud prevention, and cardholder data protection as non-negotiable priorities:
- Security Policy & Compliance Boundaries: Review our SECURITY.md for cumulative cap locks, fraud gating, and PII redaction standards.
- STRIDE Threat Model: Comprehensive threat analysis and cryptographic mitigations are documented in docs/threat-model.md.
- Card Network & ISO 20022 Compliance Matrix: Alignment with
pacs.004payment returns, Visa/Mastercard fraud controls, and SOC 1 split-settlement accounting is documented in docs/compliance/dispute-and-chargeback-controls.md. - RFC 9116 Compliance: Standardized vulnerability disclosure contact policy is published at .well-known/security.txt.
- ADR 0001: Cumulative Partial Refund Cap and BigInt Minor-Unit Invariant
- ADR 0002: Distributed Saga Orchestration for Connected Vendor Payout Reversals
- ADR 0003: Deterministic Pre-Dispatch Fraud and Velocity Scoring Gates
- ADR 0004: Idempotent Queue Workers, Full-Jitter Backoff, and DLQ Quarantine
- ADR 0005: ISO 20022 PACS.004 Payment Return Reason Code Mapping
- ADR 0006: Double-Entry Contra-Entry Journal Emission for Refund Reversals
- TLA+ Invariant 1: Cumulative Refund Cap Specification
- TLA+ Invariant 2: Vendor Reversal Saga State Machine Specification
- TLA+ Invariant 3: Pre-Dispatch Fraud Quarantine Gate Specification
- OpenAPI 3.1 Specification: Refund Worker Orchestration Contract
- Operational Runbook 1: DLQ Refund Saga Quarantine & Recovery SOP
- Operational Runbook 2: Fraud Velocity Hold & Treasury Review SOP
We welcome contributions from distributed systems engineers and security researchers. Please review our CONTRIBUTING.md and adhere strictly to Conventional Commits standards.
Distributed under the Apache-2.0 License. See LICENSE for details.