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
import http from 'node:http';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { getDatabase, SupportTicketRepository } from '@youpi/db';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// The help portal is a standalone page, never framed, so it locks framing down
// to 'self'. script-src keeps 'unsafe-inline' because help.html ships inline
// JavaScript; hardening that needs nonces and is tracked separately.
const HELP_SECURITY_HEADERS = Object.freeze({
'X-Content-Type-Options': 'nosniff',
'Referrer-Policy': 'strict-origin-when-cross-origin',
'Content-Security-Policy': "default-src 'self' data: blob:; img-src 'self' data: https:; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline'; frame-ancestors 'self'; base-uri 'none'; form-action 'self'; object-src 'none'"
});
// In production, reliably ensure environment variables from .env are loaded if not already provided
if (!process.env.BASE_PAYMENT_API_URL && typeof process.loadEnvFile === 'function') {
try {
const rootEnv = path.resolve(__dirname, '../../../.env');
if (fs.existsSync(rootEnv)) {
process.loadEnvFile(rootEnv);
}
} catch {}
}
export class HelpServer {
/**
* @param {{
* db?: import('@youpi/db').Database,
* apiBaseUrl?: string
* }} options
*/
constructor(options = {}) {
this.db = options.db || getDatabase();
this.apiBaseUrl = options.apiBaseUrl || process.env.PAYMENT_API_URL || process.env.BASE_PAYMENT_API_URL || 'http://localhost:6100';
this.ticketRepo = new SupportTicketRepository(this.db);
this.htmlTemplatePath = path.join(__dirname, 'views', 'help.html');
this.template = fs.readFileSync(this.htmlTemplatePath, 'utf8');
}
renderHtml() {
return this.template;
}
createServer() {
return http.createServer(async (req, res) => {
const url = new URL(req.url, `http://${req.headers.host || 'localhost'}`);
const pathname = url.pathname;
if (req.method === 'OPTIONS') {
res.writeHead(204, {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization'
});
res.end();
return;
}
// Health
if (pathname === '/health') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ status: 'UP', service: 'payment-help' }));
return;
}
// Customer Tickets API decommissioned
if (pathname === '/api/customer/tickets') {
res.writeHead(404, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
});
res.end(JSON.stringify({ error: 'Feature not available' }));
return;
}
// Customer Help Portal Page: GET / or GET /support or GET /help
if (pathname === '/' || pathname === '/support' || pathname === '/help') {
const html = this.renderHtml();
res.writeHead(200, {
'Content-Type': 'text/html; charset=utf-8',
'Cache-Control': 'no-cache',
...HELP_SECURITY_HEADERS
});
res.end(html);
return;
}
res.writeHead(404, { 'Content-Type': 'text/plain', ...HELP_SECURITY_HEADERS });
res.end('Help Portal Route Not Found');
});
}
}