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): claim webhook retries atomically before dispatch
ArunGupta1526be72ccbb67f5
diff --git a/packages/core/src/__tests__/webhook-engine.test.js b/packages/core/src/__tests__/webhook-engine.test.jsindex d36a1fe..13ba8cc 100644--- a/packages/core/src/__tests__/webhook-engine.test.js+++ b/packages/core/src/__tests__/webhook-engine.test.js@@ -510,3 +510,101 @@ describe('Webhook Engine Integration Tests', () => {assert.ok(receivedWebhooks[0].body.utr === '' || receivedWebhooks[0].body.utr === null);});});++describe('Webhook retry claim lifetime', () => {+ function pendingDeliveries(count) {+ const db = new Database({ inMemory: true });+ runMigrations(db);+ const sessionRepo = new PaymentSessionRepository(db);+ const merchantRepo = new MerchantRepository(db);+ const merchant = merchantRepo.create({+ businessName: 'Retry claim store',+ slug: 'retry-claim-store',+ contactEmail: '[email protected]',+ contactPhone: '9876543210',+ webhookUrl: 'https://example.com/webhook',+ webhookSecret: 'synthetic-retry-claim-secret'+ });+ const session = sessionRepo.create({+ merchantId: merchant.id,+ orderId: 'RETRY-CLAIM',+ amountPaise: 10000n+ });+ const timestamp = Date.now() - 1000;+ for (let i = 0; i < count; i++) {+ const id = `retry-${String(i).padStart(2, '0')}`;+ db.execute(`+ INSERT INTO webhook_deliveries (+ id, merchant_id, payment_id, event_type, payload, endpoint_url, next_retry_at+ ) VALUES (?, ?, ?, 'SUCCESS', ?, ?, ?)+ `, [id, merchant.id, session.id, JSON.stringify({ deliveryId: id, timestamp }), merchant.webhookUrl, new Date(timestamp).toISOString()]);+ }+ return {+ db,+ engine: new WebhookEngine({ db, merchantRepo }),+ otherEngine: new WebhookEngine({ db, merchantRepo })+ };+ }++ test('overlapping sweeps send each due delivery once when a slow batch exceeds the claim lease', async (t) => {+ const { db, engine, otherEngine } = pendingDeliveries(10);+ t.after(() => db.close());+ t.mock.timers.enable({ apis: ['Date'], now: Date.now() });+ const sent = new Map();+ let requests = 0;+ let overlappingSweep;+ t.mock.method(globalThis, 'fetch', async (_url, options) => {+ const id = options.headers['X-Gateway-Delivery-Id'];+ sent.set(id, (sent.get(id) || 0) + 1);+ requests++;+ // Each response takes less than the HTTP timeout, but the sequential batch+ // exceeds the 60s lease. A second worker wakes while request eight is in flight.+ if (requests <= 8) t.mock.timers.tick(9000);+ if (requests === 8) overlappingSweep = otherEngine.retryPending();+ return { ok: true, status: 200, text: async () => 'OK' };+ });++ const results = await engine.retryPending();+ const overlappingResults = await overlappingSweep;+ assert.equal(requests, 10, 'a slow backlog must not create duplicate HTTP requests');+ assert.equal(results.length + overlappingResults.length, 10);+ assert.equal(sent.size, 10);+ for (const count of sent.values()) assert.equal(count, 1);+ for (const row of db.query('SELECT attempt_count, is_successful FROM webhook_deliveries')) {+ assert.equal(row.attempt_count, 2, 'waiting for an earlier delivery must not consume attempts');+ assert.equal(row.is_successful, 1);+ }+ });++ test('a second worker can process waiting deliveries while the first request is in flight', async (t) => {+ const { db, engine, otherEngine } = pendingDeliveries(3);+ t.after(() => db.close());+ let releaseFirst;+ const firstResponse = new Promise(resolve => { releaseFirst = resolve; });+ let requests = 0;+ t.mock.method(globalThis, 'fetch', async () => {+ if (++requests === 1) await firstResponse;+ return { ok: true, status: 200, text: async () => 'OK' };+ });++ const firstSweep = engine.retryPending();+ let secondResults;+ try {+ secondResults = await otherEngine.retryPending();+ } finally {+ releaseFirst();+ await firstSweep;+ }+ assert.equal(secondResults.length, 2);+ assert.equal(requests, 3);+ });++ test('a sweep remains bounded to twenty deliveries', async (t) => {+ const { db, engine } = pendingDeliveries(21);+ t.after(() => db.close());+ t.mock.method(globalThis, 'fetch', async () => ({ ok: true, status: 200, text: async () => 'OK' }));++ assert.equal((await engine.retryPending()).length, 20);+ assert.equal(db.queryOne('SELECT COUNT(*) AS count FROM webhook_deliveries WHERE attempt_count = 1').count, 1);+ });+});diff --git a/packages/core/src/webhook-engine.js b/packages/core/src/webhook-engine.jsindex d01dc24..eddda25 100644--- a/packages/core/src/webhook-engine.js+++ b/packages/core/src/webhook-engine.js@@ -385,36 +385,37 @@ export class WebhookEngine {const nowIso = new Date().toISOString();- // Invariant: Atomically claim overdue pending deliveries in a transaction to prevent duplicate dispatches- const claimed = this.db.transaction(() => {- const pending = this.db.query(`- SELECT id, merchant_id, payment_id, event_type, payload, endpoint_url, attempt_count- FROM webhook_deliveries- WHERE is_successful = 0 AND attempt_count < ? AND next_retry_at <= ?- LIMIT 20- `, [this.maxRetries, nowIso]);-- if (pending.length === 0) return [];-- // Lock claimed items by setting next_retry_at 60 seconds into the future- const claimLockIso = new Date(Date.now() + 60000).toISOString();- for (const item of pending) {- this.db.execute(`- UPDATE webhook_deliveries- SET next_retry_at = ?, attempt_count = attempt_count + 1- WHERE id = ?- `, [claimLockIso, item.id]);- }-- return pending;- });-- if (claimed.length === 0) return [];+ // Snapshot a bounded batch, but claim each delivery only immediately before sending.+ // Twenty sequential 10s requests can outlive a shared 60s lease, allowing another+ // sweep to reclaim waiting items and consume attempts without a network request.+ const candidates = this.db.query(`+ SELECT id FROM webhook_deliveries+ WHERE is_successful = 0 AND attempt_count < ? AND next_retry_at <= ?+ ORDER BY next_retry_at, id+ LIMIT 20+ `, [this.maxRetries, nowIso]);const results = [];const backoffs = [5000, 30000, 120000, 600000, 3600000];- for (const item of claimed) {+ for (const candidate of candidates) {+ const item = this.db.transaction(() => {+ // Another worker may have claimed or completed this item since the snapshot.+ const pending = this.db.queryOne(`+ SELECT id, merchant_id, payment_id, event_type, payload, endpoint_url, attempt_count+ FROM webhook_deliveries+ WHERE id = ? AND is_successful = 0 AND attempt_count < ? AND next_retry_at <= ?+ `, [candidate.id, this.maxRetries, new Date().toISOString()]);+ if (!pending) return null;++ this.db.execute(`+ UPDATE webhook_deliveries+ SET next_retry_at = ?, attempt_count = attempt_count + 1+ WHERE id = ?+ `, [new Date(Date.now() + 60000).toISOString(), pending.id]);+ return pending;+ });+ if (!item) continue;const merchant = this.merchantRepo.findById(item.merchant_id);if (!merchant || !merchant.webhookSecret || !item.endpoint_url) {this.db.execute(`