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): coin store credits only payments the gateway confirms
Abhi Pareeka2270c4fe695
diff --git a/apps/coin-wallet-store/src/__tests__/coin-wallet-store.test.js b/apps/coin-wallet-store/src/__tests__/coin-wallet-store.test.jsindex 93d58f6..2eab4ba 100644--- a/apps/coin-wallet-store/src/__tests__/coin-wallet-store.test.js+++ b/apps/coin-wallet-store/src/__tests__/coin-wallet-store.test.js@@ -275,37 +275,45 @@ describe('Coin Top-Up Wallet Demo Store Integration Tests', () => {assert.ok(res.body.includes('Try Again'));});- test('POST /api/verify-payment credits coins directly from popup drawer and prevents duplicates', async () => {+ test('POST /api/verify-payment refuses to credit unknown orders and unpaid payments', async () => {+ const before = storeInstance.server.walletCoins;++ const unknown = await request('/api/verify-payment', {+ method: 'POST',+ body: { orderId: 'COIN-ORD-FORGED', coins: 999999, paymentId: 'x' }+ });+ assert.equal(unknown.status, 404);++ const checkout = await request('/api/checkout', { method: 'POST', body: { price: 100, coins: 100, packName: 'Starter Pack' } });+ assert.equal(checkout.status, 200);+ const unpaid = await request('/api/verify-payment', {+ method: 'POST',+ body: { orderId: checkout.body.orderId, coins: 999999 }+ });+ assert.equal(unpaid.status, 402);+ assert.equal(storeInstance.server.walletCoins, before);+ });++ test('POST /api/verify-payment credits coins only after the gateway reports SUCCESS, and is idempotent', async () => {const initialCoins = storeInstance.server.walletCoins;- const orderId = `COIN-ORD-POPUP-${Date.now()}`;+ const checkout = await request('/api/checkout', { method: 'POST', body: { price: 100, coins: 100, packName: 'Starter Pack' } });+ assert.equal(checkout.status, 200);+ const { orderId, paymentId } = checkout.body;++ // Simulate the gateway verification engine completing this UPI payment.+ testDb.execute("UPDATE payment_sessions SET status = 'SUCCESS', method = 'UPI' WHERE id = ?", [paymentId]);- // 1. Initial verification callconst verifyRes = await request('/api/verify-payment', {method: 'POST',- body: {- orderId,- coins: 100,- packName: 'Starter Pack'- }+ body: { orderId, coins: 999999, packName: 'Ignored' }});-assert.equal(verifyRes.status, 200);assert.equal(verifyRes.body.success, true);- assert.equal(verifyRes.body.coins, initialCoins + 100);+ // Client-supplied coin counts are ignored: the gateway session metadata wins.assert.equal(storeInstance.server.walletCoins, initialCoins + 100);- // 2. Duplicate call must be idempotent- const dupRes = await request('/api/verify-payment', {- method: 'POST',- body: {- orderId,- coins: 100,- packName: 'Starter Pack'- }- });-+ const dupRes = await request('/api/verify-payment', { method: 'POST', body: { orderId } });assert.equal(dupRes.status, 200);- assert.equal(dupRes.body.success, true);assert.equal(dupRes.body.message, 'Already credited');assert.equal(storeInstance.server.walletCoins, initialCoins + 100);});diff --git a/apps/coin-wallet-store/src/server.js b/apps/coin-wallet-store/src/server.jsindex b397dd0..350ba3d 100644--- a/apps/coin-wallet-store/src/server.js+++ b/apps/coin-wallet-store/src/server.js@@ -3,6 +3,7 @@ import fs from 'node:fs';import path from 'node:path';import { fileURLToPath } from 'node:url';import { YoupiPaymentClient } from '@youpi/merchant-sdk';+import { getCoinStoreWebhookSecret } from '@youpi/common';const __filename = fileURLToPath(import.meta.url);const __dirname = path.dirname(__filename);@@ -29,7 +30,10 @@ export class CoinWalletStoreServer {const internalPort = process.env.PAYMENT_API_PORT || 6100;this.gatewayApiUrl = options.gatewayApiUrl || process.env.PAYMENT_API_INTERNAL_URL || `http://127.0.0.1:${internalPort}`;this.merchantId = options.merchantId || '00000000-0000-0000-0000-000000000001';- this.webhookSecret = options.webhookSecret || 'merchant_webhook_secret_for_wallet_store';+ this.webhookSecret = options.webhookSecret || getCoinStoreWebhookSecret();++ // orderId -> gateway paymentId for orders this store created (used to verify payments server-side)+ this.orderPayments = new Map();this.paymentClient = new YoupiPaymentClient({apiUrl: this.gatewayApiUrl,@@ -357,6 +361,8 @@ export class CoinWalletStoreServer {}});+ this.orderPayments.set(orderId, payment.paymentId);+// Normalize checkoutUrl for localhost development / testing to prevent tunnel offline issueslet resolvedCheckoutUrl = payment.checkoutUrl;if (host.includes('localhost') || host.includes('127.0.0.1')) {@@ -387,41 +393,48 @@ export class CoinWalletStoreServer {req.on('end', async () => {try {const data = JSON.parse(body || '{}');- const { paymentId, orderId, coins, packName } = data;+ const { orderId: clientOrderId } = data;+ const sendJson = (status, payload) => {+ res.writeHead(status, { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' });+ res.end(JSON.stringify(payload));+ };++ // The payment id comes from the order this server created, never from the browser.+ const paymentId = clientOrderId ? this.orderPayments.get(clientOrderId) : undefined;+ if (!paymentId) {+ sendJson(404, { error: 'Unknown order' });+ return;+ }- if (!orderId && !paymentId) {- res.writeHead(400, { 'Content-Type': 'application/json' });- res.end(JSON.stringify({ error: 'Missing orderId or paymentId' }));+ // Never trust the browser: ask the gateway whether this payment really succeeded,+ // and take the order id and coin amount from the gateway session, not from the request.+ let session;+ try {+ session = await this.paymentClient.getPayment(paymentId);+ } catch (e) {+ console.warn('[Coin Wallet Store] getPayment failed:', e.message);+ sendJson(502, { error: 'Unable to verify payment with gateway' });return;}- // Check if already credited- const alreadyCredited = this.transactionHistory.some(t => t.orderId === orderId);- if (alreadyCredited) {- res.writeHead(200, {- 'Content-Type': 'application/json',- 'Access-Control-Allow-Origin': '*'- });- res.end(JSON.stringify({ success: true, coins: this.walletCoins, message: 'Already credited' }));+ if (!session || session.status !== 'SUCCESS' || session.method !== 'UPI') {+ sendJson(402, { error: 'Payment not completed', status: session?.status || 'UNKNOWN' });+ return;+ }+ if (clientOrderId && clientOrderId !== session.orderId) {+ sendJson(400, { error: 'orderId does not match payment' });return;}- // Verify with payment API if paymentId is available- let creditedCoins = Number(coins) || 100;- let finalPackName = packName || 'Coin Pack';-- if (paymentId) {- try {- const session = await this.paymentClient.getPayment(paymentId);- if (session) {- if (session.metadata?.coins) creditedCoins = Number(session.metadata.coins);- if (session.metadata?.packName) finalPackName = session.metadata.packName;- }- } catch (e) {- console.warn('[Coin Wallet Store] getPayment check note:', e.message);- }+ const orderId = session.orderId;+ if (this.transactionHistory.some(t => t.orderId === orderId)) {+ sendJson(200, { success: true, coins: this.walletCoins, message: 'Already credited' });+ return;}+ const creditedCoins = Number(session.metadata?.coins) || 100;+ const finalPackName = session.metadata?.packName || 'Coin Pack';+this.creditCoins(orderId, creditedCoins, finalPackName);console.log(`[Coin Wallet Store] Verified & credited ${creditedCoins} coins for Order ${orderId}. New balance: ${this.walletCoins}`);@@ -454,7 +467,9 @@ export class CoinWalletStoreServer {// Verify HMAC-SHA256 signature using merchant SDKconst event = this.paymentClient.verifyWebhook(rawBody, signature);- if (event.event === 'SUCCESS' || event.status === 'SUCCESS') {+ const paidMethod = event.method || event.data?.method;+ // COD / counter 'SUCCESS' means an order was placed, not that money arrived.+ if ((event.event === 'SUCCESS' || event.status === 'SUCCESS') && (!paidMethod || paidMethod === 'UPI')) {const orderId = event.orderId || event.data?.orderId;const alreadyCredited = this.transactionHistory.some(t => t.orderId === orderId);if (!alreadyCredited) {