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(core): honour PAYMENT_ACTIVE_WINDOW_MS for every payment slot

pravin04r-commits734002ddbbf7
diff --git a/apps/payment-api/src/index.js b/apps/payment-api/src/index.js
index 69f66d1..113bec5 100644
--- a/apps/payment-api/src/index.js
+++ b/apps/payment-api/src/index.js
@@ -78,7 +78,8 @@ export function createPaymentApp(customDb = null, options = {}) {
sessionRepo,
accountRepo,
eventEmitter,
- auditRepo
+ auditRepo,
+ activeDurationMs: Number(process.env.PAYMENT_ACTIVE_WINDOW_MS || 300000)
});
const timingEngine = new TimingEngine({
diff --git a/packages/core/src/__tests__/queue-timing.test.js b/packages/core/src/__tests__/queue-timing.test.js
index 57be0ee..91f0ff6 100644
--- a/packages/core/src/__tests__/queue-timing.test.js
+++ b/packages/core/src/__tests__/queue-timing.test.js
@@ -214,4 +214,24 @@ describe('@youpi/core Queue & Timing Engine Tests', () => {
assert.equal(checkS2.status, PaymentStatus.ACTIVE);
assert.equal(checkS2.queuePosition, 0);
});
+ test('uses the configured payment window (PAYMENT_ACTIVE_WINDOW_MS) for activation and promotion', () => {
+ const engine = new QueueEngine({ sessionRepo, accountRepo, activeDurationMs: 60000 });
+ const mk = (orderId) => sessionRepo.create({ merchantId: defaultMerchant.id, receivingAccountId: testAccount.id, orderId, amountPaise: 20000n });
+ const a = mk('WIN-ORD-1');
+ const b = mk('WIN-ORD-2');
+
+ const t0 = Date.now();
+ const first = engine.enqueueOrActivate(a.id, testAccount.id);
+ assert.equal(first.status, PaymentStatus.ACTIVE);
+ const firstWindow = sessionRepo.findById(a.id).expiresAt.getTime() - t0;
+ assert.ok(firstWindow > 55000 && firstWindow < 65000, `first slot should get 60s, got ${firstWindow}ms`);
+
+ assert.equal(engine.enqueueOrActivate(b.id, testAccount.id).status, PaymentStatus.QUEUED);
+ sessionRepo.transitionToTerminal(a.id, PaymentStatus.EXPIRED);
+ const t1 = Date.now();
+ const promoted = engine.promoteNext(testAccount.id);
+ assert.ok(promoted, 'queued session was not promoted');
+ const promotedWindow = promoted.expiresAt.getTime() - t1;
+ assert.ok(promotedWindow > 55000 && promotedWindow < 65000, `promoted slot should get 60s, got ${promotedWindow}ms`);
+ });
});
diff --git a/packages/core/src/queue-engine.js b/packages/core/src/queue-engine.js
index db22689..74daf51 100644
--- a/packages/core/src/queue-engine.js
+++ b/packages/core/src/queue-engine.js
@@ -6,10 +6,13 @@ export class QueueEngine {
* sessionRepo: import('@youpi/db').PaymentSessionRepository,
* accountRepo: import('@youpi/db').ReceivingAccountRepository,
* eventEmitter?: { emit: (event: string, payload: any) => void },
- * auditRepo?: import('@youpi/db').AuditLogRepository
+ * auditRepo?: import('@youpi/db').AuditLogRepository,
+ * activeDurationMs?: number
* }} options
*/
constructor(options) {
+ // Payment window used whenever a caller does not pass one (PAYMENT_ACTIVE_WINDOW_MS).
+ this.activeDurationMs = options.activeDurationMs || 300000;
this.sessionRepo = options.sessionRepo;
this.accountRepo = options.accountRepo;
this.eventEmitter = options.eventEmitter || { emit: () => {} };
@@ -20,10 +23,10 @@ export class QueueEngine {
* Attempts to acquire an active slot or places the session into the queue.
* @param {string} sessionId
* @param {string} receivingAccountId
- * @param {number} [activeDurationMs=300000]
+ * @param {number} [activeDurationMs] Defaults to the engine's configured payment window
* @returns {{ status: string, queuePosition: number, expiresAt: Date | null }}
*/
- enqueueOrActivate(sessionId, receivingAccountId, activeDurationMs = 300000) {
+ enqueueOrActivate(sessionId, receivingAccountId, activeDurationMs = this.activeDurationMs) {
const result = this.sessionRepo.acquireSlotAtomic(sessionId, receivingAccountId, activeDurationMs);
if (result.status === PaymentStatus.ACTIVE) {
@@ -66,10 +69,10 @@ export class QueueEngine {
/**
* Promotes the next queued session for a receiving account if the slot is free.
* @param {string} receivingAccountId
- * @param {number} [activeDurationMs=300000]
+ * @param {number} [activeDurationMs] Defaults to the engine's configured payment window
* @returns {any | null} The newly activated session or null if queue is empty or slot is busy
*/
- promoteNext(receivingAccountId, activeDurationMs = 300000) {
+ promoteNext(receivingAccountId, activeDurationMs = this.activeDurationMs) {
// Check if slot is currently occupied
const active = this.sessionRepo.findCurrentActiveOrProcessing(receivingAccountId);
if (active) {