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): echo clientMessageId across all poller responses and isolate android observer tests (#128)
Harshee5b8773e345
diff --git a/apps/android-observer-app/__tests__/android-observer.test.js b/apps/android-observer-app/__tests__/android-observer.test.jsindex e9864b8..2b699ef 100644--- a/apps/android-observer-app/__tests__/android-observer.test.js+++ b/apps/android-observer-app/__tests__/android-observer.test.js@@ -26,6 +26,24 @@ describe('Android Payment Observer Integration Tests', () => {testDb.close();});+ async function ensurePaired() {+ if (!driver.deviceId || !driver.signingKey) {+ const enrollmentSecret = resolveSecret('android_enrollment_secret', 'ANDROID_ENROLLMENT_SECRET');+ await driver.pair(enrollmentSecret);+ }+ }++ function resetReceivingSlot(receivingAccountId = '00000000-0000-0000-0000-000000000002') {+ testDb.execute(+ "UPDATE payment_sessions SET status = 'EXPIRED' WHERE receiving_account_id = ? AND status IN ('ACTIVE', 'PROCESSING')",+ [receivingAccountId]+ );+ testDb.execute(+ 'UPDATE payment_receiving_accounts SET is_slot_busy = 0, current_active_session_id = NULL WHERE id = ?',+ [receivingAccountId]+ );+ }+test('rejects pairing with invalid enrollment token', async () => {await assert.rejects(() => driver.pair('invalid_token_123'),@@ -46,12 +64,14 @@ describe('Android Payment Observer Integration Tests', () => {});test('sends heartbeat acknowledging liveness', async () => {+ await ensurePaired();const hbResult = await driver.sendHeartbeat();assert.equal(hbResult.status, 'HEARTBEAT_ACK');assert.ok(hbResult.timestamp);});test('rejects observation with expired/drifted timestamp (Replay Protection)', async () => {+ await ensurePaired();// Timestamp 2 minutes in the past (> 60s drift)const oldTimestamp = Date.now() - 120000;const res = await driver.submitObservation({@@ -64,6 +84,8 @@ describe('Android Payment Observer Integration Tests', () => {});test('submits valid HMAC-signed observation and confirms payment in Payment API', async () => {+ await ensurePaired();+ resetReceivingSlot();// 1. Create and activate a payment session for ₹350const session = apiInstance.app.context.sessionRepo.create({merchantId: '00000000-0000-0000-0000-000000000001',@@ -96,6 +118,7 @@ describe('Android Payment Observer Integration Tests', () => {});test('Issue #61: rejects notification observation when rawNotification has missing or empty package', async () => {+ await ensurePaired();const resWithoutPkg = await driver.submitObservation({amount: '100.00',receivingAccountId: '00000000-0000-0000-0000-000000000002',@@ -109,6 +132,7 @@ describe('Android Payment Observer Integration Tests', () => {});test('Issue #61: rejects notification observation from untrusted/unallowed package name', async () => {+ await ensurePaired();const resUntrusted = await driver.submitObservation({amount: '100.00',receivingAccountId: '00000000-0000-0000-0000-000000000002',@@ -122,6 +146,8 @@ describe('Android Payment Observer Integration Tests', () => {});test('submits observation with clientMessageId and returns it in response', async () => {+ await ensurePaired();+ resetReceivingSlot();const session = apiInstance.app.context.sessionRepo.create({merchantId: '00000000-0000-0000-0000-000000000001',receivingAccountId: '00000000-0000-0000-0000-000000000002',@@ -146,6 +172,8 @@ describe('Android Payment Observer Integration Tests', () => {});test('submits raw bank SMS with clientMessageId and processes through pipeline', async () => {+ await ensurePaired();+ resetReceivingSlot();const session = apiInstance.app.context.sessionRepo.create({merchantId: '00000000-0000-0000-0000-000000000001',receivingAccountId: '00000000-0000-0000-0000-000000000002',@@ -165,6 +193,19 @@ describe('Android Payment Observer Integration Tests', () => {assert.equal(res.status, 200);assert.equal(res.body.accepted, true);assert.equal(res.body.clientMessageId, clientMsgId);++ // Also verify clientMessageId is echoed on rejected/unresolved poller responses (400)+ const rejectedMsgId = 'sms-uuid-400-rejected';+ const rejectedRes = await driver.submitObservation({+ amount: '999.00',+ receivingAccountId: '00000000-0000-0000-0000-000000000002',+ utr: 'UTR-NO-ACTIVE-999',+ clientMessageId: rejectedMsgId,+ notificationText: 'Payment of ₹999 received via UPI'+ });+ assert.equal(rejectedRes.status, 400);+ assert.equal(rejectedRes.body.accepted, false);+ assert.equal(rejectedRes.body.clientMessageId, rejectedMsgId);});test('on-device bank SMS filter logic validates institutional senders and rejects personal numbers', () => {diff --git a/apps/payment-api/src/routes/pollers.routes.js b/apps/payment-api/src/routes/pollers.routes.jsindex b5ffa93..140667a 100644--- a/apps/payment-api/src/routes/pollers.routes.js+++ b/apps/payment-api/src/routes/pollers.routes.js@@ -188,10 +188,13 @@ export function registerPollerRoutes(server, ctx) {const rawText = body.rawText || body.text || body.message || body.sms || body.body || (typeof body.rawPayload === 'string' ? body.rawPayload : (body.rawPayload?.text || body.rawPayload?.body));const sender = body.sender || body.from || body.rawPayload?.sender || body.rawPayload?.from;+ const clientMessageEcho = body.clientMessageId ? { clientMessageId: String(body.clientMessageId) } : {};+// Reject missing or blank observation rawTextif (!rawText || (typeof rawText === 'string' && !rawText.trim())) {HttpServer.json(res, 400, {accepted: false,+ ...clientMessageEcho,status: 'REJECTED',reason: 'MISSING_RAW_TEXT',error: 'Observation rawText is required for verification.'@@ -202,12 +205,18 @@ export function registerPollerRoutes(server, ctx) {// Bank notifications are small. Reject oversized bodies outright so no single observation// can be used to burn CPU or memory on the verification path.if (typeof rawText === 'string' && rawText.length > MAX_OBSERVATION_TEXT_CHARS) {- HttpServer.json(res, 413, { error: `Observation body too large (max ${MAX_OBSERVATION_TEXT_CHARS} characters).` });+ HttpServer.json(res, 413, {+ ...clientMessageEcho,+ error: `Observation body too large (max ${MAX_OBSERVATION_TEXT_CHARS} characters).`+ });return;}if (!ctx.pipeline) {- HttpServer.json(res, 500, { error: 'Verification pipeline is not configured on this server.' });+ HttpServer.json(res, 500, {+ ...clientMessageEcho,+ error: 'Verification pipeline is not configured on this server.'+ });return;}@@ -218,6 +227,7 @@ export function registerPollerRoutes(server, ctx) {receivingAccountId: body.receivingAccountId,paymentSessionId: body.paymentSessionId,bankReferenceNumber: body.bankReferenceNumber || body.utr,+ receivedAt: body.observedAt || body.receivedAt,// Test flags relax verification: honour them only for the admin credential, never for observers.isTest: Boolean(body.isTest) && isAdminRequest(req),testModeActive: Boolean(body.testModeActive) && isAdminRequest(req)@@ -226,6 +236,7 @@ export function registerPollerRoutes(server, ctx) {if (pipeResult.status === 'REJECTED' && pipeResult.failingCondition === 'FRAUDULENT_PAYMENT_INJECTION') {HttpServer.json(res, 403, {accepted: false,+ ...clientMessageEcho,status: 'REJECTED',reason: 'FRAUDULENT_PAYMENT_INJECTION',threatDetected: true,@@ -238,7 +249,7 @@ export function registerPollerRoutes(server, ctx) {if (pipeResult.status === 'VERIFIED') {HttpServer.json(res, 200, {accepted: true,- ...(body.clientMessageId ? { clientMessageId: body.clientMessageId } : {}),+ ...clientMessageEcho,observationId: pipeResult.messageId || pipeResult.stages?.stage6?.details?.observationId,attribution: pipeResult.stages?.stage6?.details || pipeResult,pipeline: pipeResult@@ -246,9 +257,23 @@ export function registerPollerRoutes(server, ctx) {return;}+ if (pipeResult.status === 'MANUAL_REVIEW') {+ HttpServer.json(res, 200, {+ accepted: false,+ ...clientMessageEcho,+ status: 'MANUAL_REVIEW',+ reason: pipeResult.failingCondition || pipeResult.attribution?.reason || 'AMBIGUOUS_SESSION_MATCH',+ observationId: pipeResult.stages?.stage6?.details?.observationId || pipeResult.messageId,+ attribution: pipeResult.attribution || pipeResult.stages?.stage6?.details,+ pipeline: pipeResult+ });+ return;+ }+if (pipeResult.status === 'REJECTED') {HttpServer.json(res, 400, {accepted: false,+ ...clientMessageEcho,status: 'REJECTED',reason: pipeResult.failingCondition,error: pipeResult.failingReason || pipeResult.details,@@ -268,13 +293,15 @@ export function registerPollerRoutes(server, ctx) {if (pipeResult.failingStage === 4 &&pipeResult.failingCondition === 'NO_ACTIVE_SESSION' &&+ body.receivingAccountId &&+ pipeResult.amountPaise &&ctx.sessionRepo && ctx.attributionEngine) {const liveSession = ctx.sessionRepo.findCurrentActiveOrProcessing(body.receivingAccountId);if (!liveSession) {try {const lateResult = ctx.attributionEngine.processObservation({- source: body.source,+ source: body.source || 'SMS_GATEWAY',receivingAccountId: body.receivingAccountId,amountPaise: pipeResult.amountPaise,bankReferenceNumber: pipeResult.reference || body.utr || body.bankReferenceNumber,@@ -282,29 +309,36 @@ export function registerPollerRoutes(server, ctx) {sender,rawPayload: body.rawPayload || { text: rawText }});- HttpServer.json(res, 200, {- accepted: false,- status: lateResult?.outcomeStatus || 'LATE_UNMATCHED',- reason: 'LATE_OBSERVATION_NO_ACTIVE_SESSION',- observationId: lateResult?.observationId || pipeResult.messageId,- error: 'Payment credit was received after the session ended and has been queued for manual reconciliation.',- pipeline: pipeResult- });+ if (lateResult?.lateMatch || lateResult?.ambiguous) {+ HttpServer.json(res, 200, {+ accepted: false,+ ...clientMessageEcho,+ status: lateResult.outcomeStatus || 'LATE_UNMATCHED',+ reason: lateResult.reason || 'LATE_OBSERVATION_NO_ACTIVE_SESSION',+ observationId: lateResult.observationId || pipeResult.messageId,+ attribution: lateResult,+ error: 'Payment credit was received after the session ended and has been queued for manual reconciliation.',+ pipeline: pipeResult+ });+ return;+ }} catch (err) {HttpServer.json(res, 400, {accepted: false,+ ...clientMessageEcho,status: 'UNRESOLVED',reason: pipeResult.failingCondition || 'UNRESOLVED_OBSERVATION',error: 'Payment observation could not be verified or attributed.'});+ return;}- return;}}// Invert check: if status is not VERIFIED, return 4xx and NEVER fall through to direct attributionHttpServer.json(res, 400, {accepted: false,+ ...clientMessageEcho,status: pipeResult.status || 'UNRESOLVED',reason: pipeResult.stages?.stage6?.outcomeStatus || pipeResult.failingCondition || 'UNRESOLVED_OBSERVATION',observationId: pipeResult.messageId || pipeResult.stages?.stage6?.details?.observationId,@@ -451,11 +485,13 @@ export function registerPollerRoutes(server, ctx) {// allowlist is the control. The plain SMS-broadcast route has no package to report (the// broadcast only carries the sender address) and is authenticated by the device HMAC// instead, so requiring one there would break the observer.+ const clientMessageEcho = body.clientMessageId ? { clientMessageId: String(body.clientMessageId) } : {};const rawPkg = body.rawNotification?.package;if (requirePackage) {if (!rawPkg || typeof rawPkg !== 'string' || !rawPkg.trim()) {HttpServer.json(res, 400, {accepted: false,+ ...clientMessageEcho,error: 'Android notification observation must specify a valid package name in rawNotification.package'});return;@@ -467,6 +503,7 @@ export function registerPollerRoutes(server, ctx) {if (!isAllowedNotificationPackage(notificationPackage)) {HttpServer.json(res, 400, {accepted: false,+ ...clientMessageEcho,error: `Untrusted Android notification package: ${notificationPackage}`});return;@@ -478,6 +515,7 @@ export function registerPollerRoutes(server, ctx) {if (!rawText || (typeof rawText === 'string' && !rawText.trim())) {HttpServer.json(res, 400, {accepted: false,+ ...clientMessageEcho,status: 'REJECTED',reason: 'MISSING_RAW_TEXT',error: 'Observation rawText is required for verification.'@@ -507,7 +545,10 @@ export function registerPollerRoutes(server, ctx) {// Evaluate through Security & Fraud Verification Pipeline for Android Ingestif (!ctx.pipeline) {- HttpServer.json(res, 500, { error: 'Verification pipeline is not configured on this server.' });+ HttpServer.json(res, 500, {+ ...clientMessageEcho,+ error: 'Verification pipeline is not configured on this server.'+ });return;}@@ -518,6 +559,7 @@ export function registerPollerRoutes(server, ctx) {receivingAccountId,paymentSessionId: body.paymentSessionId,bankReferenceNumber: bankRef,+ receivedAt: observedAt,isTest: false, // device-supplied flags must never relax verificationdeviceInfo: device ? `${device.device_name} (${device.device_model})` : 'Android SMS Gateway'});@@ -525,6 +567,7 @@ export function registerPollerRoutes(server, ctx) {if (pipeResult.status === 'REJECTED' && pipeResult.failingCondition === 'FRAUDULENT_PAYMENT_INJECTION') {HttpServer.json(res, 403, {accepted: false,+ ...clientMessageEcho,status: 'REJECTED',reason: 'FRAUDULENT_PAYMENT_INJECTION',threatDetected: true,@@ -538,7 +581,7 @@ export function registerPollerRoutes(server, ctx) {ctx.db.execute("UPDATE android_devices SET last_observation_at = datetime('now') WHERE id = ?", [deviceId]);HttpServer.json(res, 200, {accepted: true,- ...(body.clientMessageId ? { clientMessageId: body.clientMessageId } : {}),+ ...clientMessageEcho,observationId: pipeResult.messageId || pipeResult.stages?.stage6?.details?.observationId,attribution: pipeResult.stages?.stage6?.details || pipeResult,pipeline: pipeResult@@ -546,10 +589,25 @@ export function registerPollerRoutes(server, ctx) {return;}+ if (pipeResult.status === 'MANUAL_REVIEW') {+ ctx.db.execute("UPDATE android_devices SET last_observation_at = datetime('now') WHERE id = ?", [deviceId]);+ HttpServer.json(res, 200, {+ accepted: false,+ ...clientMessageEcho,+ status: 'MANUAL_REVIEW',+ reason: pipeResult.failingCondition || pipeResult.attribution?.reason || 'AMBIGUOUS_SESSION_MATCH',+ observationId: pipeResult.stages?.stage6?.details?.observationId || pipeResult.messageId,+ attribution: pipeResult.attribution || pipeResult.stages?.stage6?.details,+ pipeline: pipeResult+ });+ return;+ }+if (pipeResult.status === 'REJECTED') {ctx.db.execute("UPDATE android_devices SET last_observation_at = datetime('now') WHERE id = ?", [deviceId]);HttpServer.json(res, 400, {accepted: false,+ ...clientMessageEcho,status: 'REJECTED',reason: pipeResult.failingCondition,error: pipeResult.failingReason || pipeResult.details,@@ -565,6 +623,7 @@ export function registerPollerRoutes(server, ctx) {ctx.db.execute("UPDATE android_devices SET last_observation_at = datetime('now') WHERE id = ?", [deviceId]);HttpServer.json(res, 200, {accepted: true,+ ...clientMessageEcho,status: 'PROCESSED_NON_BANK',bankMatch: false,message: 'Raw SMS observation ingested and evaluated by server pipeline.'@@ -572,10 +631,48 @@ export function registerPollerRoutes(server, ctx) {return;}+ if (+ pipeResult.failingStage === 4 &&+ pipeResult.failingCondition === 'NO_ACTIVE_SESSION' &&+ receivingAccountId &&+ (pipeResult.amountPaise || amountPaise) &&+ ctx.sessionRepo && ctx.attributionEngine+ ) {+ const liveSession = ctx.sessionRepo.findCurrentActiveOrProcessing(receivingAccountId);+ if (!liveSession) {+ try {+ const lateResult = ctx.attributionEngine.processObservation({+ source: 'ANDROID_OBSERVER',+ receivingAccountId,+ amountPaise: pipeResult.amountPaise || amountPaise,+ bankReferenceNumber: pipeResult.reference || bankRef,+ observedAt,+ sender: body.sender || req.headers['x-sender'] || body.rawNotification?.package,+ rawPayload: body.rawNotification || { text: rawText }+ });+ if (lateResult?.lateMatch || lateResult?.ambiguous) {+ ctx.db.execute("UPDATE android_devices SET last_observation_at = datetime('now') WHERE id = ?", [deviceId]);+ HttpServer.json(res, 200, {+ accepted: false,+ ...clientMessageEcho,+ status: lateResult.outcomeStatus || 'LATE_UNMATCHED',+ reason: lateResult.reason || 'LATE_OBSERVATION_NO_ACTIVE_SESSION',+ observationId: lateResult.observationId || pipeResult.messageId,+ attribution: lateResult,+ error: 'Payment credit was received after the session ended and has been queued for manual reconciliation.',+ pipeline: pipeResult+ });+ return;+ }+ } catch {}+ }+ }+// Invert check: if status is not VERIFIED, return 4xx and NEVER fall through to direct attributionctx.db.execute("UPDATE android_devices SET last_observation_at = datetime('now') WHERE id = ?", [deviceId]);HttpServer.json(res, 400, {accepted: false,+ ...clientMessageEcho,status: pipeResult.status || 'UNRESOLVED',reason: pipeResult.stages?.stage6?.outcomeStatus || pipeResult.failingCondition || 'UNRESOLVED_OBSERVATION',observationId: pipeResult.messageId || pipeResult.stages?.stage6?.details?.observationId,