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)
[ Finalize Ledger Audit Trail ] [ Compensating Saga Rollback / DLQ Quarantine ]
Designed as the asynchronous reversal and risk-control bridge between checkout-flow-sdk and payout-gateway-service, this worker enforces strict financial invariants across every refund lifecycle stage:
- 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). - Full-Jitter Backoff & DLQ Quarantine: Mitigates acquirer rate-limiting with AWS-style Full-Jitter exponential backoff and isolates ambiguous timeouts in a Dead-Letter Queue (
src/workers/refund-backoff-dlq-policy.ts).
- Deterministic Idempotency Guards: Payload fingerprint attestation preventing duplicate customer credits across worker retries (
src/guards/refund-idempotency-guard.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+): Dual mathematical specifications proving strict cumulative refund caps and saga rollback consistency (
docs/formal-verification/). - High-Throughput Micro-Benchmarks: Deterministic benchmarks for
BigIntpro-rata allocation, fraud velocity scoring, and Full-Jitter backoff (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
- TLA+ Invariant 1: Cumulative Refund Cap Specification
- TLA+ Invariant 2: Vendor Reversal Saga State Machine Specification
- OpenAPI 3.1 Specification: Refund Worker Orchestration Contract
- Operational Runbook: DLQ Refund Saga Quarantine & Recovery 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.