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): restrict manual and COD success transitions

Abhi Pareek35168b497696
diff --git a/apps/payment-api/src/__tests__/payment-api.test.js b/apps/payment-api/src/__tests__/payment-api.test.js
index dbecd1d..217ad42 100644
--- a/apps/payment-api/src/__tests__/payment-api.test.js
+++ b/apps/payment-api/src/__tests__/payment-api.test.js
@@ -4,6 +4,7 @@ import http from 'node:http';
import { Database } from '@youpi/db';
import { startServer } from '../index.js';
import { PaymentStatus, ObserverSource, verifyHmac } from '@youpi/common';
+import { authHeadersFor } from '../../../../scripts/test-support.js';
describe('Payment API End-to-End HTTP Integration Tests', () => {
let serverInstance;
@@ -27,7 +28,7 @@ describe('Payment API End-to-End HTTP Integration Tests', () => {
const url = new URL(path, baseUrl);
const reqOpts = {
method: options.method || 'GET',
- headers: options.headers || {}
+ headers: authHeadersFor(path, options.headers || {})
};
const req = http.request(url, reqOpts, res => {
@@ -613,7 +614,7 @@ describe('Payment API End-to-End HTTP Integration Tests', () => {
body: {
orderId: 'WEBHOOK-TEST-ORD-1',
amount: '450.00',
- autoActivate: true
+ method: 'CASH_COUNTER'
}
});
assert.equal(createRes.status, 201);
diff --git a/apps/payment-api/src/routes/admin.routes.js b/apps/payment-api/src/routes/admin.routes.js
index e21e5b4..f8298c0 100644
--- a/apps/payment-api/src/routes/admin.routes.js
+++ b/apps/payment-api/src/routes/admin.routes.js
@@ -277,6 +277,17 @@ export function registerAdminRoutes(server, ctx) {
return;
}
+ // Manual confirmation is only valid for counter payments still awaiting settlement.
+ // A UPI session must only reach SUCCESS through the verification engine.
+ if (session.method !== PaymentMethod.CASH_COUNTER && session.method !== PaymentMethod.CARD_COUNTER) {
+ HttpServer.json(res, 400, { error: `Manual confirmation is only allowed for counter payments (session method is '${session.method}').` });
+ return;
+ }
+ if ([PaymentStatus.SUCCESS, PaymentStatus.FAILED, PaymentStatus.EXPIRED, PaymentStatus.CANCELLED].includes(session.status)) {
+ HttpServer.json(res, 409, { error: `Cannot confirm a payment in terminal status '${session.status}'.` });
+ return;
+ }
+
const updated = ctx.sessionRepo.transitionToTerminal(paymentId, PaymentStatus.SUCCESS);
ctx.auditRepo.log({
diff --git a/apps/payment-api/src/routes/checkout.routes.js b/apps/payment-api/src/routes/checkout.routes.js
index 7d82c31..c417834 100644
--- a/apps/payment-api/src/routes/checkout.routes.js
+++ b/apps/payment-api/src/routes/checkout.routes.js
@@ -1201,8 +1201,9 @@ export function registerCheckoutRoutes(server, ctx) {
return;
}
- if ([PaymentStatus.SUCCESS, PaymentStatus.FAILED, PaymentStatus.EXPIRED, PaymentStatus.CANCELLED].includes(session.status)) {
- HttpServer.json(res, 400, { error: `Cannot place COD order for terminal session in status '${session.status}'` });
+ // PROCESSING = a UPI payment was already detected and is being verified; switching it to COD would drop that payment.
+ if ([PaymentStatus.SUCCESS, PaymentStatus.FAILED, PaymentStatus.EXPIRED, PaymentStatus.CANCELLED, PaymentStatus.PROCESSING].includes(session.status)) {
+ HttpServer.json(res, 400, { error: `Cannot place COD order for session in status '${session.status}'` });
return;
}