A self-hosted UPI & COD payment gateway with zero per-transaction platform fees, universal JavaScript checkout integration, dynamic QR payments, WhatsApp OTP authentication, and automated payment verification.
JavaScript
fix(core): reject same-channel replays after a cross-channel upgrade
Kanishak todwal7ac51a7fa09d
diff --git a/packages/core/src/__tests__/fail-closed-attribution.test.js b/packages/core/src/__tests__/fail-closed-attribution.test.jsindex 91c3250..3b30543 100644--- a/packages/core/src/__tests__/fail-closed-attribution.test.js+++ b/packages/core/src/__tests__/fail-closed-attribution.test.js@@ -335,6 +335,69 @@ describe('Phase 9A Fail-Closed Payment Attribution Hardening Suite', () => {assert.equal(successEvents.length, 1);});+ test('7b. Same-channel replay after a cross-channel upgrade: rejected as DUPLICATE_OBSERVATION', () => {+ const session = sessionRepo.create({+ merchantId: merchantA.id,+ receivingAccountId: accountA.id,+ orderId: 'ORD-REPLAY-AFTER-UPGRADE',+ amountPaise: 10000n+ });+ sessionRepo.acquireSlotAtomic(session.id, accountA.id, 300000);+ const sms = {+ source: 'SMS',+ rawText: 'Received Rs 100.00 on UPI. Ref: 777777000222',+ sender: 'YESBNK',+ receivingAccountId: accountA.id+ };++ // Gmail first, then SMS: the legitimate Level 2 upgrade. The UTR fingerprint now has a+ // GMAIL row and an SMS_GATEWAY row, and the GMAIL row is the one an unfiltered lookup finds.+ assert.equal(pipeline.process({+ source: 'GMAIL',+ rawText: 'Received Rs 100.00 on UPI. Ref: 777777000222',+ sender: '[email protected]',+ receivingAccountId: accountA.id+ }).status, 'VERIFIED');+ assert.equal(pipeline.process(sms).status, 'VERIFIED');+ assert.equal(sessionRepo.findById(session.id).verificationLevel, 2);+ const upgradesBefore = emittedEvents.filter(e => e.event === 'PAYMENT.VERIFICATION_UPGRADED').length;++ // The observer delivers the same SMS again (retry, or SMS + notification for one credit).+ const replay = pipeline.process(sms);++ assert.equal(replay.status, 'REJECTED');+ assert.equal(replay.failingCondition, 'DUPLICATE_OBSERVATION');+ const rows = db.query('SELECT source FROM payment_observations WHERE bank_reference_number = ?', ['777777000222']);+ assert.deepEqual(rows.map(r => r.source).sort(), [ObserverSource.GMAIL, ObserverSource.SMS_GATEWAY]);+ assert.equal(emittedEvents.filter(e => e.event === 'PAYMENT.VERIFICATION_UPGRADED').length, upgradesBefore);+ assert.equal(sessionRepo.findById(session.id).status, PaymentStatus.OBSERVED_SUCCESS);+ });++ test('7c. Attribution engine drops a same-channel replay even when another channel owns the first row', () => {+ const session = sessionRepo.create({+ merchantId: merchantA.id,+ receivingAccountId: accountA.id,+ orderId: 'ORD-ENGINE-REPLAY',+ amountPaise: 10000n+ });+ sessionRepo.acquireSlotAtomic(session.id, accountA.id, 300000);+ const observe = (source) => attributionEngine.processObservation(ObservationNormalizer.normalize({+ receivingAccountId: accountA.id,+ source,+ amountPaise: 10000n,+ bankReferenceNumber: '777777000333',+ observedAt: new Date()+ }));++ assert.equal(observe(ObserverSource.GMAIL).attributed, true);+ assert.equal(observe(ObserverSource.SMS_GATEWAY).attributed, true);+ const replay = observe(ObserverSource.SMS_GATEWAY);++ assert.equal(replay.duplicate, true);+ assert.equal(replay.outcomeStatus, ObservationAttributionStatus.DUPLICATE_IGNORED);+ assert.equal(db.queryOne('SELECT COUNT(*) AS n FROM payment_observations WHERE bank_reference_number = ?', ['777777000333']).n, 2);+ });+test('8. Concurrent checkout creation: queue and session state remain atomic and consistent', () => {// 3 concurrent checkouts on same receiving accountconst s1 = sessionRepo.create({diff --git a/packages/core/src/attribution-engine.js b/packages/core/src/attribution-engine.jsindex 799c582..2068078 100644--- a/packages/core/src/attribution-engine.js+++ b/packages/core/src/attribution-engine.js@@ -120,7 +120,10 @@ export class AttributionEngine {}// Tier 1: Check fingerprint for deduplication- const existing = this.obsRepo.findByFingerprint(normalizedObs.fingerprintHash);+ // Look up this channel first: once a second channel has upgraded the payment, the+ // fingerprint has one row per channel and the unfiltered lookup may return the other one.+ const existing = this.obsRepo.findByFingerprint(normalizedObs.fingerprintHash, normalizedObs.source)+ || this.obsRepo.findByFingerprint(normalizedObs.fingerprintHash);if (existing) {// If observation came from the SAME source, drop as redundant duplicateif (existing.source === normalizedObs.source) {diff --git a/packages/core/src/verification-pipeline.js b/packages/core/src/verification-pipeline.jsindex 391839f..6373419 100644--- a/packages/core/src/verification-pipeline.js+++ b/packages/core/src/verification-pipeline.js@@ -759,8 +759,8 @@ export class PaymentVerificationPipeline {observedAt: receivedAt});- const existingObs = this.obsRepo.findByFingerprint(fingerprint);- if (existingObs && existingObs.source === observerSourceEnum) {+ const existingObs = this.obsRepo.findByFingerprint(fingerprint, observerSourceEnum);+ if (existingObs) {stage4 = {stage: 4,name: 'VERIFICATION',diff --git a/packages/db/src/repositories/observation.repository.js b/packages/db/src/repositories/observation.repository.jsindex 2bcba78..21248b8 100644--- a/packages/db/src/repositories/observation.repository.js+++ b/packages/db/src/repositories/observation.repository.js@@ -47,8 +47,16 @@ export class ObservationRepository {return this.mapRow(row);}- findByFingerprint(fingerprintHash) {- const row = this.db.queryOne('SELECT * FROM payment_observations WHERE fingerprint_hash = ?', [fingerprintHash]);+ /**+ * @param {string} fingerprintHash+ * @param {string} [source] Restrict to one channel. A UTR fingerprint is shared by every+ * channel, so after a cross-channel upgrade the unfiltered lookup may return another+ * channel's row and hide a same-channel replay.+ */+ findByFingerprint(fingerprintHash, source) {+ const row = source+ ? this.db.queryOne('SELECT * FROM payment_observations WHERE fingerprint_hash = ? AND source = ? LIMIT 1', [fingerprintHash, source])+ : this.db.queryOne('SELECT * FROM payment_observations WHERE fingerprint_hash = ?', [fingerprintHash]);if (!row) return null;return this.mapRow(row);}