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(payment-api): integrate dynamic env branding into checkout and admin API routes
ArunGupta152629559d24340d
diff --git a/apps/payment-api/src/__tests__/whitelabel-branding.test.js b/apps/payment-api/src/__tests__/whitelabel-branding.test.jsindex 6f03aa0..bb602e8 100644--- a/apps/payment-api/src/__tests__/whitelabel-branding.test.js+++ b/apps/payment-api/src/__tests__/whitelabel-branding.test.js@@ -120,4 +120,36 @@ describe('White-Label Branding & Visual Gateway Customizer Test Suite', () => {assert.strictEqual(state.branding.hidePoweredBy, true);assert.strictEqual(state.branding.termsUrl, 'https://aurapay.io/terms');});++ it('4. sanitizePublicBranding falls back to environment variables when merchant branding is unset', async () => {+ const origEnv = { ...process.env };+ process.env.DEFAULT_APP_NAME = 'EnvCustomGateway';+ process.env.DEFAULT_PRIMARY_COLOR = '#059669';++ // Clear merchant branding directly in DB+ testDb.run("UPDATE merchants SET branding = '{}' WHERE id = ?", [merchantId]);++ // Create session without merchant branding override+ const initRes = await authFetch(`${baseUrl}/api/v1/payments`, {+ method: 'POST',+ headers: { 'Content-Type': 'application/json', 'x-api-key': merchantId },+ body: JSON.stringify({+ orderId: 'ORD-ENV-TEST-01',+ amount: '150.00',+ currency: 'INR'+ })+ });+ assert.strictEqual(initRes.status, 201);+ const initData = await initRes.json();++ const stateRes = await authFetch(`${baseUrl}/api/v1/checkout/${initData.paymentId}/state`);+ assert.strictEqual(stateRes.status, 200);+ const state = await stateRes.json();++ assert.strictEqual(state.branding.appName, 'EnvCustomGateway');+ assert.strictEqual(state.branding.primaryColor, '#059669');++ // Restore env+ process.env = { ...origEnv };+ });});diff --git a/apps/payment-api/src/routes/admin.routes.js b/apps/payment-api/src/routes/admin.routes.jsindex eab105f..d24bd15 100644--- a/apps/payment-api/src/routes/admin.routes.js+++ b/apps/payment-api/src/routes/admin.routes.js@@ -670,18 +670,18 @@ export function registerAdminRoutes(server, ctx) {privacyPolicyUrl,sellingMode,branding: {- appName: branding.appName || 'YOUPI PG',- primaryColor: branding.primaryColor || '#2563eb',+ appName: branding.appName || (process.env.DEFAULT_APP_NAME || '').trim() || 'YOUPI PG',+ primaryColor: branding.primaryColor || (process.env.DEFAULT_PRIMARY_COLOR || '').trim() || '#2563eb',headerTheme: branding.headerTheme || 'gradient',- logoUrl: branding.logoUrl || null,+ logoUrl: branding.logoUrl !== undefined ? branding.logoUrl : ((process.env.DEFAULT_LOGO_URL || '').trim() || null),sidebarNotice: branding.sidebarNotice || 'Fast & Secure 256-Bit Encrypted Payments',subbannerText: branding.subbannerText || null,- trustBadgeText: branding.trustBadgeText || null,+ trustBadgeText: branding.trustBadgeText || (process.env.DEFAULT_TRUST_BADGE_TEXT || '').trim() || null,hideTrustBadge: Boolean(branding.hideTrustBadge),hideSubbanner: Boolean(branding.hideSubbanner),- hidePoweredBy: Boolean(branding.hidePoweredBy),+ hidePoweredBy: branding.hidePoweredBy !== undefined ? Boolean(branding.hidePoweredBy) : (process.env.DEFAULT_HIDE_POWERED_BY === 'true'),hideTimeSavingBanner: Boolean(branding.hideTimeSavingBanner),- footerGuarantee: branding.footerGuarantee || 'Secured by YOUPI PG 256-Bit Encryption',+ footerGuarantee: branding.footerGuarantee || (process.env.DEFAULT_FOOTER_GUARANTEE || '').trim() || `Secured by ${branding.appName || (process.env.DEFAULT_APP_NAME || '').trim() || 'YOUPI PG'} 256-Bit Encryption`,counterInstructions: branding.counterInstructions || 'Please visit the store billing counter and present this screen to the cashier.',cardCounterInstructions: branding.cardCounterInstructions || 'Please swipe, insert, or tap your credit or debit card on the store POS terminal at the billing desk.',termsUrl: branding.termsUrl || null,@@ -735,9 +735,9 @@ export function registerAdminRoutes(server, ctx) {const inputBranding = body.branding || {};const mergedBranding = { ...currentBranding };- if (inputBranding.appName !== undefined) mergedBranding.appName = inputBranding.appName.trim() || currentBranding.appName || 'YOUPI PG';+ if (inputBranding.appName !== undefined) mergedBranding.appName = inputBranding.appName.trim() || currentBranding.appName || (process.env.DEFAULT_APP_NAME || '').trim() || 'YOUPI PG';if (inputBranding.logoUrl !== undefined) mergedBranding.logoUrl = inputBranding.logoUrl ? inputBranding.logoUrl.trim() : null;- if (inputBranding.primaryColor !== undefined) mergedBranding.primaryColor = inputBranding.primaryColor.trim() || currentBranding.primaryColor || '#2563eb';+ if (inputBranding.primaryColor !== undefined) mergedBranding.primaryColor = inputBranding.primaryColor.trim() || currentBranding.primaryColor || (process.env.DEFAULT_PRIMARY_COLOR || '').trim() || '#2563eb';if (inputBranding.sidebarNotice !== undefined) mergedBranding.sidebarNotice = inputBranding.sidebarNotice ? inputBranding.sidebarNotice.trim() : null;if (inputBranding.headerTheme !== undefined) mergedBranding.headerTheme = inputBranding.headerTheme || 'gradient';if (inputBranding.subbannerText !== undefined) mergedBranding.subbannerText = inputBranding.subbannerText ? inputBranding.subbannerText.trim() : null;@@ -952,9 +952,9 @@ export function registerAdminRoutes(server, ctx) {const currentBranding = merchant.branding || {};const mergedBranding = { ...currentBranding };- if (inputBranding.appName !== undefined) mergedBranding.appName = inputBranding.appName.trim() || currentBranding.appName || 'YOUPI PG';+ if (inputBranding.appName !== undefined) mergedBranding.appName = inputBranding.appName.trim() || currentBranding.appName || (process.env.DEFAULT_APP_NAME || '').trim() || 'YOUPI PG';if (inputBranding.logoUrl !== undefined) mergedBranding.logoUrl = inputBranding.logoUrl ? inputBranding.logoUrl.trim() : null;- if (inputBranding.primaryColor !== undefined) mergedBranding.primaryColor = inputBranding.primaryColor.trim() || currentBranding.primaryColor || '#2563eb';+ if (inputBranding.primaryColor !== undefined) mergedBranding.primaryColor = inputBranding.primaryColor.trim() || currentBranding.primaryColor || (process.env.DEFAULT_PRIMARY_COLOR || '').trim() || '#2563eb';if (inputBranding.headerTheme !== undefined) mergedBranding.headerTheme = inputBranding.headerTheme || 'gradient';if (inputBranding.sidebarNotice !== undefined) mergedBranding.sidebarNotice = inputBranding.sidebarNotice ? inputBranding.sidebarNotice.trim() : null;if (inputBranding.subbannerText !== undefined) mergedBranding.subbannerText = inputBranding.subbannerText ? inputBranding.subbannerText.trim() : null;diff --git a/apps/payment-api/src/routes/checkout.routes.js b/apps/payment-api/src/routes/checkout.routes.jsindex b167bbc..918ccbe 100644--- a/apps/payment-api/src/routes/checkout.routes.js+++ b/apps/payment-api/src/routes/checkout.routes.js@@ -110,12 +110,17 @@ function checkSessionDeviceAccess(session, req, body = null, ctx = null) {* Prevents internal credentials (gmailConfig, cloudflareTunnelCommand, tokens) from leaking to public checkouts.*/function sanitizePublicBranding(raw = {}) {+ const envAppName = (process.env.DEFAULT_APP_NAME || '').trim();+ const envPrimaryColor = (process.env.DEFAULT_PRIMARY_COLOR || '').trim();+ const defaultAppName = envAppName || 'YOUPI PG';+ const defaultPrimaryColor = envPrimaryColor || '#2563eb';+// Strict explicit allowlist to prevent arbitrary internal metadata leaks (CWE-200 / OWASP A01)return {- appName: raw.appName || 'YOUPI PG',- primaryColor: raw.primaryColor || '#2563eb',+ appName: raw.appName || defaultAppName,+ primaryColor: raw.primaryColor || defaultPrimaryColor,headerTheme: raw.headerTheme || 'gradient',- logoUrl: raw.logoUrl || null,+ logoUrl: raw.logoUrl !== undefined ? raw.logoUrl : ((process.env.DEFAULT_LOGO_URL || '').trim() || null),counterInstructions: raw.counterInstructions || 'Please visit the billing counter and present this screen to the cashier.',cardCounterInstructions: raw.cardCounterInstructions || 'Please swipe, insert, or tap your credit or debit card on the store POS terminal at the billing desk.',supportEmails: Array.isArray(raw.supportEmails) ? raw.supportEmails : [],@@ -127,12 +132,12 @@ function sanitizePublicBranding(raw = {}) {enabledPaymentMethods: Array.isArray(raw.enabledPaymentMethods) ? raw.enabledPaymentMethods : ['UPI'],sidebarNotice: raw.sidebarNotice || undefined,checkoutNotice: raw.checkoutNotice || undefined,- footerGuarantee: raw.footerGuarantee || undefined,- trustBadgeText: raw.trustBadgeText || undefined,+ footerGuarantee: raw.footerGuarantee || (process.env.DEFAULT_FOOTER_GUARANTEE || '').trim() || undefined,+ trustBadgeText: raw.trustBadgeText || (process.env.DEFAULT_TRUST_BADGE_TEXT || '').trim() || undefined,hideTrustBadge: raw.hideTrustBadge !== undefined ? Boolean(raw.hideTrustBadge) : undefined,subbannerText: raw.subbannerText || undefined,hideSubbanner: raw.hideSubbanner !== undefined ? Boolean(raw.hideSubbanner) : undefined,- hidePoweredBy: raw.hidePoweredBy !== undefined ? Boolean(raw.hidePoweredBy) : undefined,+ hidePoweredBy: raw.hidePoweredBy !== undefined ? Boolean(raw.hidePoweredBy) : (process.env.DEFAULT_HIDE_POWERED_BY === 'true' ? true : undefined),hideTimeSavingBanner: raw.hideTimeSavingBanner !== undefined ? Boolean(raw.hideTimeSavingBanner) : undefined};}diff --git a/apps/payment-api/src/services/whatsapp-berry.service.js b/apps/payment-api/src/services/whatsapp-berry.service.jsindex 4a26afd..016c6f4 100644--- a/apps/payment-api/src/services/whatsapp-berry.service.js+++ b/apps/payment-api/src/services/whatsapp-berry.service.js@@ -623,13 +623,14 @@ export class WhatsAppBerryService extends EventEmitter {* @param {string} otpCode 6-digit verification code* @param {string} [customerName]*/- async sendOtp(to, otpCode, customerName = '') {+ async sendOtp(to, otpCode, customerName = '', options = {}) {+ const senderName = options.appName || (process.env.WHATSAPP_SENDER_NAME || '').trim() || (process.env.DEFAULT_APP_NAME || '').trim() || 'YOUPI PG';const greeting = customerName ? `Hello *${customerName}*!` : 'Hello!';const message =- `🔐 *YOUPI PG Verification Code*\n\n` ++ `🔐 *${senderName} Verification Code*\n\n` +`${greeting} Your one-time login OTP is: *${otpCode}*\n\n` +`⏱ _Valid for 5 minutes. Do not share this code with anyone._\n\n` +- `— *YOUPI PG Secure Payments*`;+ `— *${senderName} Secure Payments*`;return this.sendMessage(to, message);}