Skip to content

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(webhooks): retain outbox when merchant signing config is missing or unsafe

SHIVANSHU SATYAJEET2bf111a5fbbb
diff --git a/packages/core/src/__tests__/webhook-outbox-durability.test.js b/packages/core/src/__tests__/webhook-outbox-durability.test.js
index 8b4a3eb..02ac45d 100644
--- a/packages/core/src/__tests__/webhook-outbox-durability.test.js
+++ b/packages/core/src/__tests__/webhook-outbox-durability.test.js
@@ -95,3 +95,29 @@ test('interrupted first attempt remains eligible for restart retry with the same
db.close();
}
});
+
+test('missing merchant signing key preserves an outboxed event until configuration is repaired', async () => {
+ const { db, sessions, merchant, engine, merchants } = fixture('durable-key-rotation');
+ const previousFetch = globalThis.fetch;
+ try {
+ const session = sessions.create({ merchantId: merchant.id, orderId: 'DURABLE-3', amountPaise: 300n, status: 'ACTIVE' });
+ sessions.transitionToTerminal(session.id, 'EXPIRED');
+ db.execute("UPDATE merchants SET webhook_secret = '' WHERE id = ?", [merchant.id]);
+ assert.equal((await engine.recoverOutbox()).length, 0);
+ assert.equal(db.queryOne('SELECT COUNT(*) AS n FROM webhook_deliveries WHERE payment_id = ?', [session.id]).n, 0);
+ assert.equal(db.queryOne('SELECT COUNT(*) AS n FROM payment_webhook_outbox WHERE payment_id = ?', [session.id]).n, 1);
+ merchants.update(merchant.id, { webhookSecret: 'newly_rotated_synthetic_test_secret' });
+ let attempted = 0;
+ globalThis.fetch = async () => {
+ attempted++;
+ return { ok: true, status: 200, text: async () => 'ok' };
+ };
+ const replayed = await engine.recoverOutbox();
+ assert.equal(replayed.length, 1);
+ assert.equal(replayed[0].delivered, true);
+ assert.equal(attempted, 1);
+ } finally {
+ globalThis.fetch = previousFetch;
+ db.close();
+ }
+});
diff --git a/packages/core/src/webhook-engine.js b/packages/core/src/webhook-engine.js
index b2ad185..f3880c5 100644
--- a/packages/core/src/webhook-engine.js
+++ b/packages/core/src/webhook-engine.js
@@ -43,8 +43,19 @@ export class WebhookEngine {
return { delivered: Boolean(prior.is_successful), deliveryId: prior.id, status: prior.response_status, error: prior.is_successful ? undefined : prior.response_body };
}
- // 1. Up-front secret validation: if webhookSecret is missing, persist a failed row so it is auditable
+ // Terminal transitions already have a durable outbox entry. If configuration
+ // is missing or unsafe, KEEP that entry instead of recording an irrecoverable
+ // synthetic failure row; the operator can repair configuration and replay.
+ const isOutboxed = Boolean(this.db.queryOne(
+ 'SELECT payment_id FROM payment_webhook_outbox WHERE payment_id = ?',
+ [session.id]
+ ));
+
+ // Direct non-outboxed dispatches still write an auditable failure record.
if (!merchant.webhookSecret) {
+ if (isOutboxed) {
+ return { delivered: false, deliveryId: '', status: 500, error: 'Merchant webhookSecret is not configured.' };
+ }
const deliveryId = crypto.randomUUID();
this.db.execute(`
INSERT INTO webhook_deliveries (
@@ -66,6 +77,9 @@ export class WebhookEngine {
// 2. Re-validate at dispatch time (SSRF guard)
const endpointCheck = validateOutboundWebhookUrl(merchant.webhookUrl);
if (!endpointCheck.valid) {
+ if (isOutboxed) {
+ return { delivered: false, deliveryId: '', status: 400, error: `Webhook delivery blocked: ${endpointCheck.error}` };
+ }
const deliveryId = crypto.randomUUID();
this.db.execute(`
INSERT INTO webhook_deliveries (