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(payment-api): make OTP resend cooldown configurable and eliminate unconditional test bypass

ArunGupta1526d7a057fd3bb8
diff --git a/apps/payment-api/src/__tests__/whatsapp-berry-auth.test.js b/apps/payment-api/src/__tests__/whatsapp-berry-auth.test.js
index 751e567..fc00c06 100644
--- a/apps/payment-api/src/__tests__/whatsapp-berry-auth.test.js
+++ b/apps/payment-api/src/__tests__/whatsapp-berry-auth.test.js
@@ -6,7 +6,7 @@ import { authFetch } from '../../../../scripts/test-support.js';
test('WhatsApp Berry Protocol OTP Customer Auth & Merchant Configuration Suite', async (t) => {
const customDb = new Database({ inMemory: true });
- const app = createPaymentApp(customDb);
+ const app = createPaymentApp(customDb, { otpCooldownMs: 0 });
const testPort = 6288;
const serverInstance = await new Promise(resolve => {
diff --git a/apps/payment-api/src/index.js b/apps/payment-api/src/index.js
index e2bddac..867ff00 100644
--- a/apps/payment-api/src/index.js
+++ b/apps/payment-api/src/index.js
@@ -432,7 +432,8 @@ export function createPaymentApp(customDb = null, options = {}) {
defaultMerchant,
checkoutBaseUrl,
whatsappBerryService,
- webhookEngine
+ webhookEngine,
+ otpCooldownMs: options.otpCooldownMs
};
registerPaymentRoutes(server, context);
diff --git a/apps/payment-api/src/routes/customer.routes.js b/apps/payment-api/src/routes/customer.routes.js
index 5157a39..c5e8166 100644
--- a/apps/payment-api/src/routes/customer.routes.js
+++ b/apps/payment-api/src/routes/customer.routes.js
@@ -43,20 +43,53 @@ export function registerCustomerRoutes(server, ctx) {
// Throttles OTP dispatch per mobile number so the WhatsApp channel cannot be flooded/cost-abused.
const otpRequestLog = new Map();
+ /**
+ * Resolves the configured cooldown between consecutive OTP dispatches.
+ * Prioritizes explicit runtime options (ctx.otpCooldownMs), environment overrides
+ * (OTP_RESEND_COOLDOWN_MS), and defaults to 30 seconds.
+ */
+ function getOtpCooldownMs() {
+ if (ctx && typeof ctx.otpCooldownMs === 'number' && ctx.otpCooldownMs >= 0) {
+ return ctx.otpCooldownMs;
+ }
+ if (process.env.OTP_RESEND_COOLDOWN_MS !== undefined) {
+ const val = Number(process.env.OTP_RESEND_COOLDOWN_MS);
+ if (Number.isFinite(val) && val >= 0) {
+ return val;
+ }
+ }
+ return OTP_RESEND_COOLDOWN_MS;
+ }
+
/**
* Records an OTP request and reports whether the caller is within the resend cooldown.
* Entries older than the cooldown are pruned on each call.
*/
function isOtpThrottled(mobile) {
+ const cooldownMs = getOtpCooldownMs();
+ if (cooldownMs === 0) {
+ return false;
+ }
const now = Date.now();
for (const [key, at] of otpRequestLog) {
- if (now - at > OTP_RESEND_COOLDOWN_MS) otpRequestLog.delete(key);
+ if (now - at > cooldownMs) otpRequestLog.delete(key);
}
if (otpRequestLog.has(mobile)) return true;
otpRequestLog.set(mobile, now);
return false;
}
+ // Expose reset helper for tests to reset rate-limiting state deterministically
+ if (ctx) {
+ ctx.resetOtpCooldown = (mobile) => {
+ if (mobile) {
+ otpRequestLog.delete(normalizeMobile(mobile) || mobile);
+ } else {
+ otpRequestLog.clear();
+ }
+ };
+ }
+
// Helper to get Berry Service
function getBerry() {
return ctx.whatsappBerryService || null;