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

Merge PR #91

rishabhead789403c65
diff --cc packages/core/src/webhook-engine.js
index f3880c5,de047b5..c21ae57
--- a/packages/core/src/webhook-engine.js
+++ b/packages/core/src/webhook-engine.js
@@@ -1,7 -1,6 +1,7 @@@
import crypto from 'node:crypto';
+import { PaymentSessionRepository } from '@youpi/db';
import { generateHmac, toRupeesString, validateOutboundWebhookUrl } from '@youpi/common';
- import { isTerminalStatus, toPublicOutcome } from './state-machine.js';
+ import { toPublicOutcome, isDispatchableOutcome, settlementStateFor } from './state-machine.js';
export class WebhookEngine {
/**
@@@ -30,32 -28,11 +30,32 @@@
return { delivered: false, deliveryId: '', error: 'No webhook URL configured for merchant.' };
}
- const publicOutcome = isTerminalStatus(event) ? toPublicOutcome(event) : event;
+ const publicOutcome = isDispatchableOutcome(event) ? toPublicOutcome(event) : event;
const nowIso = new Date().toISOString();
- // 1. Up-front secret validation: if webhookSecret is missing, persist a failed row so it is auditable
+ // One durable delivery per payment/event, including duplicate or replayed in-process
+ // events. SQLite serializes the final create-or-read in the transaction below.
+ const prior = this.db.queryOne(
+ 'SELECT id, is_successful, response_status, response_body FROM webhook_deliveries WHERE payment_id = ? AND event_type = ? ORDER BY created_at DESC LIMIT 1',
+ [session.id, publicOutcome]
+ );
+ if (prior) {
+ return { delivered: Boolean(prior.is_successful), deliveryId: prior.id, status: prior.response_status, error: prior.is_successful ? undefined : prior.response_body };
+ }
+
+ // 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 (