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(security): keep COD customer contact and metadata private to the order's merchant

kaustubhk-069a090f9fcfdf
diff --git a/apps/payment-api/src/__tests__/selling-modes-and-cod.test.js b/apps/payment-api/src/__tests__/selling-modes-and-cod.test.js
index 803670d..baf4e50 100644
--- a/apps/payment-api/src/__tests__/selling-modes-and-cod.test.js
+++ b/apps/payment-api/src/__tests__/selling-modes-and-cod.test.js
@@ -4,7 +4,7 @@ import http from 'node:http';
import { Database } from '@youpi/db';
import { startServer as startPaymentApi } from '../index.js';
import { PaymentStatus, PaymentMethod, SellingMode } from '@youpi/common';
-import { authHeadersFor } from '../../../../scripts/test-support.js';
+import { authHeaders, authHeadersFor } from '../../../../scripts/test-support.js';
describe('Architectural Update: Selling Modes (Online vs Offline), COD Flow, & Clean VPA Config', () => {
let apiInstance;
@@ -198,8 +198,17 @@ describe('Architectural Update: Selling Modes (Online vs Offline), COD Flow, & C
assert.equal(checkBRes.body.status, PaymentStatus.ACTIVE);
assert.equal(checkBRes.body.queuePosition, 0);
- // Developer API endpoint: GET /api/v1/orders/:orderId/cod
- const devOrderRes = await request('/api/v1/orders/ORDER-COD-A/cod');
+ // Order ids are guessable, so an anonymous caller must not get the delivery contact or metadata.
+ for (const path of ['/api/v1/orders/ORDER-COD-A/cod', `/api/v1/payments/${paymentIdA}/cod`, `/api/v1/checkout/${paymentIdA}/cod`]) {
+ const anonRes = await request(path);
+ assert.equal(anonRes.status, 200);
+ assert.equal(anonRes.body.method, 'COD');
+ assert.equal(anonRes.body.customer, null, `${path} leaked the customer`);
+ assert.equal(anonRes.body.metadata, undefined, `${path} leaked metadata`);
+ }
+
+ // Developer API endpoint: GET /api/v1/orders/:orderId/cod (owner credential required for the customer)
+ const devOrderRes = await request('/api/v1/orders/ORDER-COD-A/cod', { headers: authHeaders() });
assert.equal(devOrderRes.status, 200);
assert.equal(devOrderRes.body.orderId, 'ORDER-COD-A');
assert.equal(devOrderRes.body.paymentId, paymentIdA);
@@ -211,7 +220,8 @@ describe('Architectural Update: Selling Modes (Online vs Offline), COD Flow, & C
assert.equal(devOrderRes.body.customer.mobileNumber, '9876543210');
// Developer API endpoint: GET /api/v1/payments/:id/cod
- const devPayRes = await request(`/api/v1/payments/${paymentIdA}/cod`);
+ const devPayRes = await request(`/api/v1/payments/${paymentIdA}/cod`, { headers: authHeaders() });
+ assert.equal(devPayRes.body.customer.name, 'Rahul Sharma');
assert.equal(devPayRes.status, 200);
assert.equal(devPayRes.body.paymentId, paymentIdA);
assert.equal(devPayRes.body.orderId, 'ORDER-COD-A');
diff --git a/apps/payment-api/src/auth-guard.js b/apps/payment-api/src/auth-guard.js
index 0d40a6a..b5dbd62 100644
--- a/apps/payment-api/src/auth-guard.js
+++ b/apps/payment-api/src/auth-guard.js
@@ -1,4 +1,4 @@
-import { getAdminToken, getObserverToken, safeEqual } from '@youpi/common';
+import { getAdminToken, getObserverToken, safeEqual, hasBearer } from '@youpi/common';
import { HttpServer } from './server.js';
/** Admin-prefixed routes that are intentionally public (static APK download for pairing). */
@@ -72,3 +72,24 @@ export function isAdminRequest(req) {
const presented = bearerToken(req);
return Boolean(presented) && safeEqual(presented, getAdminToken());
}
+
+/**
+ * True when the caller may read the merchant-private fields of a payment session.
+ *
+ * Requires proof of ownership: either the gateway's own admin/observer credential, or the
+ * owning merchant's webhook secret presented via `X-Merchant-Key` (which every merchant
+ * integration already holds). Without it, only the public view is returned, so a leaked or
+ * guessed payment id cannot be used to read internal status, verification internals,
+ * merchant metadata or the return URL.
+ */
+export function isPaymentOwner(req, session, ctx) {
+ if (isAdminRequest(req)) return true;
+ if (hasBearer(req, getObserverToken())) return true;
+
+ const presented = req.headers['x-merchant-key'];
+ if (!presented || typeof presented !== 'string' || !presented.trim()) return false;
+
+ const merchant = ctx.merchantRepo?.findById ? ctx.merchantRepo.findById(session.merchantId) : null;
+ if (!merchant?.webhookSecret) return false;
+ return safeEqual(presented.trim(), merchant.webhookSecret);
+}
diff --git a/apps/payment-api/src/routes/checkout.routes.js b/apps/payment-api/src/routes/checkout.routes.js
index f5fc534..b2444bb 100644
--- a/apps/payment-api/src/routes/checkout.routes.js
+++ b/apps/payment-api/src/routes/checkout.routes.js
@@ -1,6 +1,7 @@
import { HttpServer } from '../server.js';
import { toRupeesString, buildUpiUri, PaymentStatus, PaymentMethod, UPI_PROVIDERS, isInternalServiceRequest } from '@youpi/common';
import { validateSessionDeviceAccess } from '@youpi/core';
+import { isPaymentOwner } from '../auth-guard.js';
/**
* Validates device access to an active payment session.
@@ -1338,7 +1339,7 @@ export function registerCheckoutRoutes(server, ctx) {
return;
}
let customer = null;
- if (session.customerId && ctx.customerRepo) {
+ if (isPaymentOwner(req, session, ctx) && session.customerId && ctx.customerRepo) {
const c = ctx.customerRepo.findById(session.customerId);
if (c) {
customer = {
diff --git a/apps/payment-api/src/routes/payments.routes.js b/apps/payment-api/src/routes/payments.routes.js
index 1a63fdf..acff353 100644
--- a/apps/payment-api/src/routes/payments.routes.js
+++ b/apps/payment-api/src/routes/payments.routes.js
@@ -1,28 +1,7 @@
import { HttpServer } from '../server.js';
-import { toPaise, toRupeesString, resolveRuntimeUrls, PaymentStatus, safeEqual, hasBearer, getObserverToken } from '@youpi/common';
+import { toPaise, toRupeesString, resolveRuntimeUrls, PaymentStatus } from '@youpi/common';
import { isTerminalStatus, toPublicOutcome } from '@youpi/core';
-import { isAdminRequest } from '../auth-guard.js';
-
-/**
- * True when the caller may read the merchant-private fields of a payment session.
- *
- * Requires proof of ownership: either the gateway's own admin/observer credential, or the
- * owning merchant's webhook secret presented via `X-Merchant-Key` (which every merchant
- * integration already holds). Without it, only the public view is returned, so a leaked or
- * guessed payment id cannot be used to read internal status, verification internals,
- * merchant metadata or the return URL.
- */
-function isPaymentOwner(req, session, ctx) {
- if (isAdminRequest(req)) return true;
- if (hasBearer(req, getObserverToken())) return true;
-
- const presented = req.headers['x-merchant-key'];
- if (!presented || typeof presented !== 'string' || !presented.trim()) return false;
-
- const merchant = ctx.merchantRepo?.findById ? ctx.merchantRepo.findById(session.merchantId) : null;
- if (!merchant?.webhookSecret) return false;
- return safeEqual(presented.trim(), merchant.webhookSecret);
-}
+import { isPaymentOwner } from '../auth-guard.js';
/**
* Registers merchant-facing payment routes.
@@ -337,8 +316,11 @@ export function registerPaymentRoutes(server, ctx) {
return;
}
+ // Order ids are merchant-chosen and often sequential, so the delivery contact and metadata
+ // are only for the owning merchant (or the gateway); anyone else gets the order status.
+ const owner = isPaymentOwner(req, session, ctx);
let customer = null;
- if (session.customerId && ctx.customerRepo) {
+ if (owner && session.customerId && ctx.customerRepo) {
const c = ctx.customerRepo.findById(session.customerId);
if (c) {
customer = {
@@ -360,9 +342,9 @@ export function registerPaymentRoutes(server, ctx) {
status: session.status,
verificationTag: session.verificationTag,
isCod: session.method === 'COD',
- customer,
+ customer: owner ? customer : null,
placedAt: session.completedAt ? (session.completedAt.toISOString ? session.completedAt.toISOString() : session.completedAt) : (session.createdAt?.toISOString ? session.createdAt.toISOString() : session.createdAt),
- metadata: session.metadata
+ ...(owner ? { metadata: session.metadata } : {})
});
});
@@ -387,8 +369,11 @@ export function registerPaymentRoutes(server, ctx) {
return;
}
+ // Order ids are merchant-chosen and often sequential, so the delivery contact and metadata
+ // are only for the owning merchant (or the gateway); anyone else gets the order status.
+ const owner = isPaymentOwner(req, session, ctx);
let customer = null;
- if (session.customerId && ctx.customerRepo) {
+ if (owner && session.customerId && ctx.customerRepo) {
const c = ctx.customerRepo.findById(session.customerId);
if (c) {
customer = {
@@ -410,9 +395,9 @@ export function registerPaymentRoutes(server, ctx) {
status: session.status,
verificationTag: session.verificationTag,
isCod: session.method === 'COD',
- customer,
+ customer: owner ? customer : null,
placedAt: session.completedAt ? (session.completedAt.toISOString ? session.completedAt.toISOString() : session.completedAt) : (session.createdAt?.toISOString ? session.createdAt.toISOString() : session.createdAt),
- metadata: session.metadata
+ ...(owner ? { metadata: session.metadata } : {})
});
});
}