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

feat(canary): enable SINGLE_CHANNEL_VALIDATED_GMAIL fast confirmation and parallel Gmail poller

Vinoth Kumar5a4439e56607
diff --git a/apps/gmail-observer/src/gmail-poller.js b/apps/gmail-observer/src/gmail-poller.js
index a3c38aa..e73407a 100644
--- a/apps/gmail-observer/src/gmail-poller.js
+++ b/apps/gmail-observer/src/gmail-poller.js
@@ -56,7 +56,7 @@ export class GmailPoller {
const isLocalhost = process.env.YOUPI_MODE === 'localhost' || !process.env.YOUPI_MODE;
this.apiBaseUrl = options.apiBaseUrl || (isLocalhost ? `http://127.0.0.1:${process.env.PAYMENT_API_PORT || 6100}` : (process.env.BASE_PAYMENT_API_URL || 'http://localhost:6100'));
this.receivingAccountId = options.receivingAccountId || process.env.RECEIVING_ACCOUNT_ID || null;
- this.pollIntervalMs = options.pollIntervalMs || Number(process.env.GMAIL_POLL_INTERVAL_MS || 5000);
+ this.pollIntervalMs = options.pollIntervalMs || Number(process.env.GMAIL_POLL_INTERVAL_MS || 2000);
this.storagePath = options.storagePath || path.join(rootDir, 'data', 'gmail_processed_ids.json');
this.observerClient = new PaymentObserverClient({
apiBaseUrl: this.apiBaseUrl,
@@ -375,34 +375,35 @@ export class GmailPoller {
const listData = await listRes.json();
const messages = listData.messages || [];
- for (const m of messages) {
- if (this.processedMessageIds.has(m.id)) continue;
-
- try {
- const msgRes = await fetch(`https://gmail.googleapis.com/gmail/v1/users/me/messages/${m.id}?format=full`, {
- headers: { 'Authorization': `Bearer ${accessToken}` },
- signal: AbortSignal.timeout(8000)
- });
- if (!msgRes.ok) continue;
-
- const msgData = await msgRes.json();
- const headers = msgData.payload?.headers || [];
- const from = headers.find(h => h.name.toLowerCase() === 'from')?.value || '';
- const subject = headers.find(h => h.name.toLowerCase() === 'subject')?.value || '';
- const dateStr = headers.find(h => h.name.toLowerCase() === 'date')?.value || '';
- const body = extractBodyFromGmailPayload(msgData.payload) || msgData.snippet || '';
-
- await this.processEmail({
- id: m.id,
- messageId: m.id,
- subject,
- body,
- from,
- date: dateStr ? new Date(dateStr) : new Date(Number(msgData.internalDate || Date.now()))
- });
- } catch (msgErr) {
- console.warn(`[Gmail Observer] Error fetching message:`, redactSensitiveData(msgErr.message));
- }
+ const unseenMessages = messages.filter(m => !this.processedMessageIds.has(m.id));
+ if (unseenMessages.length > 0) {
+ await Promise.allSettled(unseenMessages.map(async (m) => {
+ try {
+ const msgRes = await fetch(`https://gmail.googleapis.com/gmail/v1/users/me/messages/${m.id}?format=full`, {
+ headers: { 'Authorization': `Bearer ${accessToken}` },
+ signal: AbortSignal.timeout(8000)
+ });
+ if (!msgRes.ok) return;
+
+ const msgData = await msgRes.json();
+ const headers = msgData.payload?.headers || [];
+ const from = headers.find(h => h.name.toLowerCase() === 'from')?.value || '';
+ const subject = headers.find(h => h.name.toLowerCase() === 'subject')?.value || '';
+ const dateStr = headers.find(h => h.name.toLowerCase() === 'date')?.value || '';
+ const body = extractBodyFromGmailPayload(msgData.payload) || msgData.snippet || '';
+
+ await this.processEmail({
+ id: m.id,
+ messageId: m.id,
+ subject,
+ body,
+ from,
+ date: dateStr ? new Date(dateStr) : new Date(Number(msgData.internalDate || Date.now()))
+ });
+ } catch (msgErr) {
+ console.warn(`[Gmail Observer] Error fetching message:`, redactSensitiveData(msgErr.message));
+ }
+ }));
}
} catch (err) {
console.warn('[Gmail Observer] pollCycle error:', redactSensitiveData(err.message));
diff --git a/apps/payment-api/src/__tests__/canary-isolation-verification.test.js b/apps/payment-api/src/__tests__/canary-isolation-verification.test.js
index 39b0ccb..1313567 100644
--- a/apps/payment-api/src/__tests__/canary-isolation-verification.test.js
+++ b/apps/payment-api/src/__tests__/canary-isolation-verification.test.js
@@ -91,9 +91,9 @@ describe('Mitto Pay — Canary Isolation & Dual-Channel Settlement Tests', () =>
apiInstance.app.context.db.execute("UPDATE payment_sessions SET status = 'SUCCESS' WHERE id = ?", [res.body.paymentId]);
});
- test('4. Canary settlement strictly fails if YOUPI_VERIFICATION_POLICY is not REQUIRE_DUAL_CHANNEL', async () => {
- // Temporarily degrade verification policy
- process.env.YOUPI_VERIFICATION_POLICY = 'STANDARD';
+ test('4. Canary settlement strictly fails if YOUPI_VERIFICATION_POLICY is invalid', async () => {
+ // Temporarily set invalid verification policy
+ process.env.YOUPI_VERIFICATION_POLICY = 'INSECURE_INVALID_POLICY';
const res = await createPayment({
orderId: 'CANARY-FAIL-SAFE-004',
@@ -110,6 +110,82 @@ describe('Mitto Pay — Canary Isolation & Dual-Channel Settlement Tests', () =>
process.env.YOUPI_VERIFICATION_POLICY = 'REQUIRE_DUAL_CHANNEL';
});
+ test('4b. Canary settlement succeeds when YOUPI_VERIFICATION_POLICY is SINGLE_CHANNEL_VALIDATED_GMAIL', async () => {
+ process.env.YOUPI_VERIFICATION_POLICY = 'SINGLE_CHANNEL_VALIDATED_GMAIL';
+
+ const res = await createPayment({
+ orderId: 'CANARY-SINGLE-POLICY-004B',
+ amount: '10.00',
+ autoActivate: true,
+ metadata: {
+ agencyVvisaUid: AUTHORIZED_UID
+ }
+ });
+
+ assert.equal(res.status, 201);
+ assert.ok(res.body.paymentId);
+
+ // Clean slot
+ apiInstance.app.context.db.execute("UPDATE payment_sessions SET status = 'SUCCESS' WHERE id = ?", [res.body.paymentId]);
+ process.env.YOUPI_VERIFICATION_POLICY = 'REQUIRE_DUAL_CHANNEL';
+ });
+
+ test('4c. End-to-end fast confirmation: Validated Gmail observation auto-finalizes to SUCCESS under SINGLE_CHANNEL_VALIDATED_GMAIL', async () => {
+ process.env.YOUPI_VERIFICATION_POLICY = 'SINGLE_CHANNEL_VALIDATED_GMAIL';
+ apiInstance.app.context.attributionEngine.verificationPolicy = 'SINGLE_CHANNEL_VALIDATED_GMAIL';
+
+ let successEmitted = false;
+ let successPayload = null;
+ const startTime = performance.now();
+
+ apiInstance.app.context.eventEmitter.once('PAYMENT.SUCCESS', (data) => {
+ successEmitted = true;
+ successPayload = data;
+ });
+
+ // 1. Create canary session for VVANFTTKRA
+ const createRes = await createPayment({
+ orderId: 'CANARY-GMAIL-FAST-001',
+ amount: '10.00',
+ autoActivate: true,
+ metadata: {
+ agencyVvisaUid: AUTHORIZED_UID,
+ vvisaAmountMinor: 1000
+ }
+ });
+ assert.equal(createRes.status, 201);
+ const paymentId = createRes.body.paymentId;
+
+ // 2. Ingest validated Gmail observation
+ const uTr = '911789526799';
+ const gmailRes = apiInstance.app.context.attributionEngine.processObservation({
+ receivingAccountId: '00000000-0000-0000-0000-000000000002',
+ source: 'GMAIL',
+ amountPaise: 1000n,
+ observedAt: new Date(),
+ bankReferenceNumber: uTr,
+ fingerprintHash: `fp_fast_gmail_${uTr}`
+ });
+
+ const elapsedMs = performance.now() - startTime;
+
+ assert.equal(gmailRes.attributed, true);
+ assert.equal(gmailRes.verificationLevel, 1);
+ assert.equal(gmailRes.outcomeStatus, PaymentStatus.SUCCESS);
+ assert.equal(successEmitted, true);
+ assert.equal(successPayload.paymentId, paymentId);
+ assert.ok(elapsedMs < 100, `Processing took ${elapsedMs.toFixed(2)}ms, expected < 100ms`);
+
+ // Verify session in DB transitioned to SUCCESS
+ const finalSession = apiInstance.app.context.sessionRepo.findById(paymentId);
+ assert.equal(finalSession.status, PaymentStatus.SUCCESS);
+ assert.equal(finalSession.verificationTag, 'Single Verification (Gmail)');
+
+ // Restore
+ process.env.YOUPI_VERIFICATION_POLICY = 'REQUIRE_DUAL_CHANNEL';
+ apiInstance.app.context.attributionEngine.verificationPolicy = 'REQUIRE_DUAL_CHANNEL';
+ });
+
test('5. End-to-end canary settlement: SMS alone stays in PROCESSING; secondary Gmail finalizes to SUCCESS', async () => {
let successEventEmitted = false;
let emittedPayload = null;
diff --git a/packages/common/src/__tests__/canary-policy.test.js b/packages/common/src/__tests__/canary-policy.test.js
index fb117c7..2eba97a 100644
--- a/packages/common/src/__tests__/canary-policy.test.js
+++ b/packages/common/src/__tests__/canary-policy.test.js
@@ -80,11 +80,11 @@ describe('Centralized Canary Merchant Isolation Policy Tests', () => {
});
// 7. Policy requirement: fails closed if verification policy is not REQUIRE_DUAL_CHANNEL
- test('7. Fails closed if verification policy is not REQUIRE_DUAL_CHANNEL', () => {
+ test('7. Fails closed if verification policy is not valid canary policy', () => {
const res = evaluateCanaryMerchantEligibility(AUTHORIZED_MERCHANT, {
env: {
CANARY_MERCHANT_ALLOWLIST: 'VVANFTTKRA',
- YOUPI_VERIFICATION_POLICY: 'STANDARD'
+ YOUPI_VERIFICATION_POLICY: 'INVALID_POLICY'
}
});
assert.equal(res.allowed, false);
@@ -92,6 +92,17 @@ describe('Centralized Canary Merchant Isolation Policy Tests', () => {
assert.equal(res.code, 'INVALID_VERIFICATION_POLICY');
});
+ test('7b. Passes when verification policy is SINGLE_CHANNEL_VALIDATED_GMAIL', () => {
+ const res = evaluateCanaryMerchantEligibility(AUTHORIZED_MERCHANT, {
+ requestedAgencyVvisaUid: 'VVANFTTKRA',
+ env: {
+ CANARY_MERCHANT_ALLOWLIST: 'VVANFTTKRA',
+ YOUPI_VERIFICATION_POLICY: 'SINGLE_CHANNEL_VALIDATED_GMAIL'
+ }
+ });
+ assert.equal(res.allowed, true);
+ });
+
// 8. Authorized merchant succeeds when properly configured
test('8. Authorized merchant is granted access when allowlist and dual-channel policy match', () => {
const res = evaluateCanaryMerchantEligibility(AUTHORIZED_MERCHANT, {
diff --git a/packages/common/src/canary-policy.js b/packages/common/src/canary-policy.js
index 8739a09..69500e7 100644
--- a/packages/common/src/canary-policy.js
+++ b/packages/common/src/canary-policy.js
@@ -86,9 +86,10 @@ export function parseCanaryAllowlist(rawAllowlist = process.env.CANARY_MERCHANT_
*/
export function assertCanaryVerificationPolicy(policy = process.env.YOUPI_VERIFICATION_POLICY) {
const currentPolicy = String(policy || 'STANDARD').trim().toUpperCase();
- if (currentPolicy !== 'REQUIRE_DUAL_CHANNEL') {
+ const allowedPolicies = ['REQUIRE_DUAL_CHANNEL', 'SINGLE_CHANNEL_VALIDATED_GMAIL'];
+ if (!allowedPolicies.includes(currentPolicy)) {
const err = new CanaryConfigurationError(
- `Fatal configuration error: Canary settlement requires YOUPI_VERIFICATION_POLICY=REQUIRE_DUAL_CHANNEL (current: "${currentPolicy}").`
+ `Fatal configuration error: Canary settlement requires valid policy [${allowedPolicies.join(', ')}] (current: "${currentPolicy}").`
);
err.code = 'INVALID_VERIFICATION_POLICY';
throw err;
diff --git a/packages/core/src/attribution-engine.js b/packages/core/src/attribution-engine.js
index 798dd87..00053ca 100644
--- a/packages/core/src/attribution-engine.js
+++ b/packages/core/src/attribution-engine.js
@@ -597,7 +597,7 @@ export class AttributionEngine {
});
// If grace period is 0 (e.g. in instant tests), finalize immediately
- if (this.gracePeriodMs <= 0) {
+ if (this.verificationPolicy === 'SINGLE_CHANNEL_VALIDATED_GMAIL' || this.gracePeriodMs <= 0) {
const finalResult = this.sessionRepo.recordVerificationSource(
currentSession.id,
normalizedObs.source,