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(whatsapp): resolve broken QR code image rendering with native SVG Base64 encoding

Gaurav Mehraaedf7447f53b
diff --git a/apps/merchant-admin/src/views/admin.html b/apps/merchant-admin/src/views/admin.html
index 7854a3e..1314202 100644
--- a/apps/merchant-admin/src/views/admin.html
+++ b/apps/merchant-admin/src/views/admin.html
@@ -2986,7 +2986,7 @@
style="width:200px; height:200px; display:flex; align-items:center; justify-content:center; color:#0f172a; font-size:0.85rem;">
Click "Connect / Refresh QR" to pair device
</div>
- <img id="waQrImg" src="" alt="WhatsApp Pairing QR" style="width:200px; height:200px; display:none;" />
+ <img id="waQrImg" src="" alt="WhatsApp Pairing QR" style="width:200px; height:200px; display:none;" onerror="this.style.display='none'; if(document.getElementById('waQrPlaceholder')){ document.getElementById('waQrPlaceholder').style.display='flex'; document.getElementById('waQrPlaceholder').innerText='QR preview unavailable. Click Refresh QR.'; }" />
</div>
<div style="display:flex; gap:0.5rem; flex-wrap:wrap;">
@@ -5290,9 +5290,22 @@
if (btnConnect) btnConnect.innerText = '🔄 Refresh QR Code';
if (data.qrCode && qrImg && qrPlaceholder) {
- qrImg.src = data.qrCode;
- qrImg.style.display = 'block';
- qrPlaceholder.style.display = 'none';
+ let qrSrc = data.qrCode;
+ if (typeof qrSrc === 'string' && !qrSrc.startsWith('data:image/') && !qrSrc.startsWith('http')) {
+ if (qrSrc.startsWith('<svg')) {
+ qrSrc = 'data:image/svg+xml;utf8,' + encodeURIComponent(qrSrc);
+ }
+ }
+
+ if (qrSrc && (qrSrc.startsWith('data:image/') || qrSrc.startsWith('http'))) {
+ qrImg.src = qrSrc;
+ qrImg.style.display = 'block';
+ qrPlaceholder.style.display = 'none';
+ } else {
+ qrImg.style.display = 'none';
+ qrPlaceholder.style.display = 'flex';
+ qrPlaceholder.innerText = 'Preparing pairing QR... Click "Refresh QR Code"';
+ }
}
} else {
if (badge) { badge.className = 'badge badge-danger'; badge.innerText = 'DISCONNECTED'; }
diff --git a/apps/payment-api/src/routes/admin.routes.js b/apps/payment-api/src/routes/admin.routes.js
index 7137fef..33dbdce 100644
--- a/apps/payment-api/src/routes/admin.routes.js
+++ b/apps/payment-api/src/routes/admin.routes.js
@@ -3,6 +3,7 @@ import { HttpServer } from '../server.js';
import { PaymentStatus, PaymentMethod, SellingMode, AuditActorType, isValidVpa, toRupeesString, deriveCloudflareRoutes, resolveRuntimeUrls } from '@youpi/common';
import { PaymentVerificationPipeline } from '@youpi/core';
import { globalTunnelManager } from '../services/tunnel-manager.js';
+import { formatQrDataUrl } from '../services/whatsapp-berry.service.js';
function resolveSellingModeAndMethods(body, currentBranding = {}) {
const inputBranding = body.branding || {};
@@ -1110,6 +1111,13 @@ export function registerAdminRoutes(server, ctx) {
const info = berry.getClientInfo();
const qr = berry.getQrCode();
+ let qrPayload = qr?.dataUrl || (typeof qr === 'string' && qr.startsWith('data:image/') ? qr : null);
+ if (!qrPayload && (qr?.raw || typeof qr === 'string')) {
+ const rawText = qr?.raw || (typeof qr === 'string' ? qr : null);
+ if (rawText) {
+ qrPayload = await formatQrDataUrl(rawText);
+ }
+ }
HttpServer.json(res, 200, {
success: true,
@@ -1118,7 +1126,7 @@ export function registerAdminRoutes(server, ctx) {
platform: info.platform,
sessionId: info.sessionId,
isConnected: info.isConnected,
- qrCode: qr?.dataUrl || qr?.raw || (typeof qr === 'string' ? qr : null),
+ qrCode: qrPayload || qr?.dataUrl || qr?.raw || (typeof qr === 'string' ? qr : null),
pairingCode: info.pairingCode || null,
pairingPhone: info.pairingPhone || null
});
@@ -1133,10 +1141,18 @@ export function registerAdminRoutes(server, ctx) {
}
const qr = berry.getQrCode();
+ let qrPayload = qr?.dataUrl || (typeof qr === 'string' && qr.startsWith('data:image/') ? qr : null);
+ if (!qrPayload && (qr?.raw || typeof qr === 'string')) {
+ const rawText = qr?.raw || (typeof qr === 'string' ? qr : null);
+ if (rawText) {
+ qrPayload = await formatQrDataUrl(rawText);
+ }
+ }
+
HttpServer.json(res, 200, {
success: true,
status: berry.getStatus(),
- qrCode: qr?.dataUrl || qr?.raw || (typeof qr === 'string' ? qr : null),
+ qrCode: qrPayload || qr?.dataUrl || qr?.raw || (typeof qr === 'string' ? qr : null),
available: Boolean(qr)
});
});
@@ -1163,11 +1179,18 @@ export function registerAdminRoutes(server, ctx) {
qr = berry.getQrCode();
}
- const qrPayload = qr?.dataUrl || qr?.raw || (typeof qr === 'string' ? qr : null);
+ let qrPayload = qr?.dataUrl || (typeof qr === 'string' && qr.startsWith('data:image/') ? qr : null);
+ if (!qrPayload && (qr?.raw || typeof qr === 'string')) {
+ const rawText = qr?.raw || (typeof qr === 'string' ? qr : null);
+ if (rawText) {
+ qrPayload = await formatQrDataUrl(rawText);
+ }
+ }
+
HttpServer.json(res, 200, {
success: true,
status: berry.getStatus(),
- qrCode: qrPayload
+ qrCode: qrPayload || qr?.dataUrl || qr?.raw || (typeof qr === 'string' ? qr : null)
});
} catch (err) {
HttpServer.json(res, 500, { error: err.message });
diff --git a/apps/payment-api/src/services/whatsapp-berry.service.js b/apps/payment-api/src/services/whatsapp-berry.service.js
index 5fbc933..fda9a32 100644
--- a/apps/payment-api/src/services/whatsapp-berry.service.js
+++ b/apps/payment-api/src/services/whatsapp-berry.service.js
@@ -2,6 +2,7 @@ import { EventEmitter } from 'node:events';
import path from 'node:path';
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';
+import { generateQrSvg } from '@youpi/core';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
@@ -12,32 +13,71 @@ let qrcodeLib = null;
async function loadDependencies() {
if (!BerryProtocolClass) {
- try {
- const mod = await import('berryprotocol');
- BerryProtocolClass = mod.BerryProtocol || mod.default;
- } catch {
+ const candidates = [
+ 'berryprotocol',
+ 'file:///D:/E-Commerce PKG/services/whatsapp-worker/node_modules/berryprotocol/dist/index.js',
+ 'file:///D:/ECOM AND YOUPI PG INTEGRATION/E-Commerce PACKAGE/services/whatsapp-worker/node_modules/berryprotocol/dist/index.js',
+ 'file:///D:/distribution/services/whatsapp-worker/node_modules/berryprotocol/dist/index.js'
+ ];
+ for (const p of candidates) {
try {
- const mod = await import('file:///D:/distribution/services/whatsapp-worker/node_modules/berryprotocol/dist/index.js');
+ const mod = await import(p);
BerryProtocolClass = mod.BerryProtocol || mod.default;
- } catch (e) {
- // Will operate in pairing QR fallback mode if berryprotocol module cannot be found
- }
+ if (BerryProtocolClass) {
+ console.log(`[WhatsAppBerryService] Loaded BerryProtocol engine from: ${p}`);
+ break;
+ }
+ } catch {}
}
}
if (!qrcodeLib) {
- try {
- const qrMod = await import('qrcode');
- qrcodeLib = qrMod.default || qrMod;
- } catch {
+ const qrCandidates = [
+ 'qrcode',
+ 'file:///D:/E-Commerce PKG/services/whatsapp-worker/node_modules/qrcode/lib/index.js',
+ 'file:///D:/ECOM AND YOUPI PG INTEGRATION/E-Commerce PACKAGE/services/whatsapp-worker/node_modules/qrcode/lib/index.js',
+ 'file:///D:/distribution/services/whatsapp-worker/node_modules/qrcode/lib/index.js'
+ ];
+ for (const p of qrCandidates) {
try {
- const qrMod = await import('file:///D:/distribution/services/whatsapp-worker/node_modules/qrcode/lib/index.js');
+ const qrMod = await import(p);
qrcodeLib = qrMod.default || qrMod;
- } catch (e) {}
+ if (qrcodeLib) break;
+ } catch {}
}
}
return { BerryProtocol: BerryProtocolClass, qrcode: qrcodeLib };
}
+/**
+ * Ensures any pairing string or QR data is converted to an authentic, renderable Data URI.
+ * Uses qrcode library if available, with pure-JS SVG Base64 fallback from @youpi/core.
+ */
+export async function formatQrDataUrl(rawText, qrcodeModule = null) {
+ if (!rawText) return null;
+ if (typeof rawText === 'string' && rawText.startsWith('data:image/')) {
+ return rawText;
+ }
+ if (qrcodeModule && typeof qrcodeModule.toDataURL === 'function') {
+ try {
+ const dataUrl = await qrcodeModule.toDataURL(rawText, {
+ width: 320,
+ margin: 2,
+ color: { dark: '#0f172a', light: '#ffffff' }
+ });
+ if (dataUrl && dataUrl.startsWith('data:image/')) {
+ return dataUrl;
+ }
+ } catch {}
+ }
+ try {
+ const svg = generateQrSvg(rawText, 320, 'M');
+ return 'data:image/svg+xml;base64,' + Buffer.from(svg).toString('base64');
+ } catch (err) {
+ console.error('[WhatsAppBerryService] Failed to generate QR SVG Data URL:', err.message);
+ return null;
+ }
+}
+
export class WhatsAppBerryService extends EventEmitter {
constructor(options = {}) {
super();
@@ -186,7 +226,7 @@ export class WhatsAppBerryService extends EventEmitter {
if (!this.qrCode) {
const { qrcode } = await loadDependencies();
const fallbackStr = `berry-pair:${Date.now()}`;
- const dataUrl = qrcode?.toDataURL ? await qrcode.toDataURL(fallbackStr, { margin: 1, scale: 6 }) : null;
+ const dataUrl = await formatQrDataUrl(fallbackStr, qrcode);
this.qrCode = { raw: fallbackStr, dataUrl };
this.status = 'QR_READY';
}
@@ -221,16 +261,7 @@ export class WhatsAppBerryService extends EventEmitter {
this.status = 'QR_READY';
this.phoneNumber = null;
const simRaw = `BERRY_PAIR_${Date.now()}`;
- let simDataUrl = simRaw;
- if (qrcode) {
- try {
- simDataUrl = await qrcode.toDataURL(simRaw, {
- width: 320,
- margin: 2,
- color: { dark: '#0f172a', light: '#ffffff' }
- });
- } catch {}
- }
+ const simDataUrl = await formatQrDataUrl(simRaw, qrcode);
this.qrCode = { raw: simRaw, dataUrl: simDataUrl };
console.log('[WhatsAppBerryService] Running in pairing QR mode (berryprotocol standalone). QR ready.');
this.emit('qr', this.qrCode);
@@ -275,17 +306,8 @@ export class WhatsAppBerryService extends EventEmitter {
this.status = 'QR_READY';
this.phoneNumber = null;
const fallbackRaw = `BERRY_PAIR_${Date.now()}`;
- let fallbackDataUrl = fallbackRaw;
const { qrcode } = await loadDependencies();
- if (qrcode) {
- try {
- fallbackDataUrl = await qrcode.toDataURL(fallbackRaw, {
- width: 320,
- margin: 2,
- color: { dark: '#0f172a', light: '#ffffff' }
- });
- } catch {}
- }
+ const fallbackDataUrl = await formatQrDataUrl(fallbackRaw, qrcode);
this.qrCode = { raw: fallbackRaw, dataUrl: fallbackDataUrl };
this.emit('qr', this.qrCode);
this.emit('status', this.status);
@@ -307,18 +329,11 @@ export class WhatsAppBerryService extends EventEmitter {
}
try {
- let dataUrl = null;
- if (qrcodeLib) {
- dataUrl = await qrcodeLib.toDataURL(value, {
- width: 320,
- margin: 2,
- color: { dark: '#0f172a', light: '#ffffff' }
- });
- }
+ const dataUrl = await formatQrDataUrl(value, qrcodeLib);
this.qrCode = {
raw: value,
- dataUrl: dataUrl || value
+ dataUrl
};
this.status = 'QR_READY';
this.phoneNumber = null;