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

Merge PR #17: escape rendered values and validate redirect/postMessage origins

rishabh34eed951352c
diff --cc apps/checkout-portal/public/js/checkout.js
index 9231659,0000000..b610ee0
mode 100644,000000..100644
--- a/apps/checkout-portal/public/js/checkout.js
+++ b/apps/checkout-portal/public/js/checkout.js
@@@ -1,2720 -1,0 +1,2773 @@@
+
+ let state = null;
+ let countdownTimer = null;
+ let queuePollTimer = null;
+ let sse = null;
+ let activeCustomer = null;
+ let activeToken = null;
+
+ const STORAGE_KEY_IDENTITY = 'youpi.customer.identity';
+ const STORAGE_KEY_ACCOUNTS = 'youpi.customer.accounts';
+ const STORAGE_KEY_ACTIVE = 'youpi.customer.activeId';
+ const MAX_IDENTITY_AGE_MS = 7 * 24 * 60 * 60 * 1000; // 7 days (maximum 1 week)
+
+ function getPreservedIdentity() {
+ try {
+ const raw = localStorage.getItem(STORAGE_KEY_IDENTITY);
+ if (!raw) return null;
+ const data = JSON.parse(raw);
+ if (!data || !data.customer) {
+ clearPreservedIdentity();
+ return null;
+ }
+ const now = Date.now();
+ const expiry = data.expiresAt || ((data.savedAt || now) + MAX_IDENTITY_AGE_MS);
+ if (now > expiry) {
+ console.log('[Auth] Preserved identity expired (> 1 week). Purging.');
+ clearPreservedIdentity();
+ return null;
+ }
+ return data;
+ } catch {
+ return null;
+ }
+ }
+
+ function savePreservedIdentity(cust, token) {
+ if (!cust || !cust.id) return;
+ try {
+ const now = Date.now();
+ const cleanCust = {
+ id: cust.id,
+ name: cust.name || cust.fullName || 'Customer',
+ email: cust.email || '',
+ mobileNumber: cust.mobileNumber || cust.phoneNumber || '',
+ status: cust.status || 'ACTIVE'
+ };
+ const resolvedToken = token || activeToken || localStorage.getItem('ypg_customer_token') || null;
+ const payload = {
+ customer: cleanCust,
+ token: resolvedToken,
+ savedAt: now,
+ expiresAt: now + MAX_IDENTITY_AGE_MS
+ };
+ // 1. Preserve 1-week identity
+ localStorage.setItem(STORAGE_KEY_IDENTITY, JSON.stringify(payload));
+ // 2. Strict single-user saved account store
+ localStorage.setItem(STORAGE_KEY_ACCOUNTS, JSON.stringify([cleanCust]));
+ localStorage.setItem(STORAGE_KEY_ACTIVE, cust.id);
+ if (resolvedToken) {
+ localStorage.setItem('ypg_customer_token', resolvedToken);
+ }
+ } catch { }
+ }
+
+ function clearPreservedIdentity() {
+ try {
+ localStorage.removeItem(STORAGE_KEY_IDENTITY);
+ localStorage.removeItem(STORAGE_KEY_ACCOUNTS);
+ localStorage.removeItem(STORAGE_KEY_ACTIVE);
+ localStorage.removeItem('ypg_customer_token');
+ } catch { }
+ }
+
+ // Immediately restore 1-week preserved identity if present
+ const initPreserved = getPreservedIdentity();
+ if (initPreserved && initPreserved.customer) {
+ activeCustomer = initPreserved.customer;
+ activeToken = initPreserved.token || null;
+ }
+
+ // Notify parent if embedded in iframe
+ if (window.parent !== window || location.search.includes('embedded=1')) {
+ document.body.classList.add('is-embedded');
+ try {
- window.parent.postMessage({ event: 'youpi.modal_ready', paymentId: PAYMENT_ID }, '*');
++ postToMerchant({ event: 'youpi.modal_ready', paymentId: PAYMENT_ID });
+ } catch (e) { }
+ }
+
+ // Dismiss Initial Splash Screen smoothly after brief intro
+ function dismissSplashScreen() {
+ const splash = document.getElementById('ypgSplashScreen');
+ if (splash && !splash.classList.contains('fade-out')) {
+ setTimeout(() => {
+ splash.classList.add('fade-out');
+ }, 550);
+ }
+ }
+
+ // Toast helper
+ let toastTimer = null;
+ function showOptionToast(msg) {
+ const toast = document.getElementById('ypgToast');
+ if (!toast) return;
+ toast.textContent = msg;
+ toast.classList.add('show');
+ if (toastTimer) clearTimeout(toastTimer);
+ toastTimer = setTimeout(() => {
+ toast.classList.remove('show');
+ }, 2500);
+ }
+
+ // Toggle Order Breakdown Drawer
+ function toggleOrderDetails() {
+ const drawer = document.getElementById('orderDetailsDrawer');
+ const chevron = document.getElementById('chevronDetailsIcon');
+ if (!drawer) return;
+ const isOpen = drawer.classList.contains('open');
+ if (isOpen) {
+ drawer.classList.remove('open');
+ if (chevron) chevron.innerHTML = '▴';
+ } else {
+ drawer.classList.add('open');
+ if (chevron) chevron.innerHTML = '▾';
+ }
+ }
+
+ // Failure redirect URL resolver
+ function getFailedRedirectUrl() {
+ const orderId = state?.orderId || '';
+ const reason = state?.status || 'FAILED';
+ const paymentId = typeof PAYMENT_ID !== 'undefined' ? PAYMENT_ID : (state?.id || '');
+
+ let baseOrigin = '';
+ if (state && state.returnUrl) {
+ try {
+ baseOrigin = new URL(state.returnUrl).origin;
+ } catch (e) { }
+ }
+ if (!baseOrigin && document.referrer) {
+ try {
+ baseOrigin = new URL(document.referrer).origin;
+ } catch (e) { }
+ }
+
+ const query = `orderId=${encodeURIComponent(orderId)}&reason=${encodeURIComponent(reason)}&paymentId=${encodeURIComponent(paymentId)}`;
+ return baseOrigin ? `${baseOrigin}/order-failed?${query}` : `/order-failed?${query}`;
+ }
+
++ /**
++ * Only http/https URLs may be navigated to. Blocks `javascript:`, `data:` and other
++ * schemes that would execute script in the checkout origin.
++ */
++ function sanitizeRedirectUrl(candidate) {
++ if (typeof candidate !== 'string') return null;
++ const trimmed = candidate.trim();
++ if (!trimmed) return null;
++ let parsed;
++ try {
++ parsed = new URL(trimmed, window.location.origin);
++ } catch (e) {
++ return null;
++ }
++ if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') return null;
++ return parsed.href;
++ }
++
++ /**
++ * Origin of the storefront embedding this iframe, used as the postMessage target.
++ * Returns null when the embedder cannot be identified (callers then fall back to '*').
++ */
++ function resolveMerchantOrigin() {
++ try {
++ if (document.referrer) {
++ const parsed = new URL(document.referrer);
++ if (parsed.protocol === 'http:' || parsed.protocol === 'https:') return parsed.origin;
++ }
++ } catch (e) { }
++ try {
++ const params = new URLSearchParams(window.location.search);
++ const explicit = params.get('origin') || params.get('merchantOrigin');
++ if (explicit) {
++ const parsed = new URL(explicit, window.location.origin);
++ if (parsed.protocol === 'http:' || parsed.protocol === 'https:') return parsed.origin;
++ }
++ } catch (e) { }
++ return null;
++ }
++
++ /** Posts to the embedding storefront using its resolved origin when known. */
++ function postToMerchant(message) {
++ try {
++ window.parent.postMessage(message, resolveMerchantOrigin() || '*');
++ } catch (e) { }
++ }
++
+ // Store return URL resolver (used when user cancels payment or returns to merchant store)
+ function getStoreReturnUrl() {
+ if (typeof state !== 'undefined' && state) {
+ if (state.cancelUrl) {
+ return state.cancelUrl;
+ }
+ if (state.returnUrl) {
+ try {
+ const parsed = new URL(state.returnUrl);
+ return parsed.origin ? `${parsed.origin}/` : state.returnUrl;
+ } catch (e) { }
+ }
+ }
+ try {
+ const params = new URLSearchParams(window.location.search);
+ const cancelParam = params.get('cancelUrl') || params.get('cancel_url');
+ if (cancelParam) return cancelParam;
+ const qUrl = params.get('returnUrl') || params.get('storeUrl') || params.get('redirectUrl');
+ if (qUrl) {
+ const parsed = new URL(qUrl, window.location.origin);
+ return parsed.origin ? `${parsed.origin}/` : qUrl;
+ }
+ } catch (e) { }
+ if (document.referrer) {
+ try {
+ const refUrl = new URL(document.referrer);
+ if (refUrl.host !== window.location.host) {
+ return `${refUrl.origin}/`;
+ }
+ } catch (e) { }
+ }
+ if (window.location.port === '7000' || window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1') {
+ return 'http://localhost:5000/';
+ }
+ return '/';
+ }
+
+ // Retry / Close on Expired or Failed session
+ function handleRetryNewPayment() {
+ const failRedirectUrl = getFailedRedirectUrl();
+ const orderId = state?.orderId || '';
+ const reason = state?.status || 'FAILED';
+ const paymentId = typeof PAYMENT_ID !== 'undefined' ? PAYMENT_ID : (state?.id || '');
+
+ const isEmbedded = (window.parent !== window) || location.search.includes('embedded=1');
+
+ if (isEmbedded) {
+ try {
- window.parent.postMessage({
++ postToMerchant({
+ event: 'youpi.payment_failed',
+ data: {
+ orderId,
+ reason,
+ paymentId,
+ status: reason,
+ redirectUrl: failRedirectUrl
+ },
+ orderId,
+ reason,
+ paymentId,
+ status: reason,
+ redirectUrl: failRedirectUrl
- }, '*');
++ });
+ } catch (e) { }
+
+ // Fallback postMessage in case parent container only listens for modal_close
+ try {
- window.parent.postMessage({
++ postToMerchant({
+ event: 'youpi.modal_close',
+ action: 'payment_failed',
+ redirectUrl: failRedirectUrl
- }, '*');
++ });
+ } catch (e) { }
+ } else {
+ // Standalone full page checkout
- window.location.href = failRedirectUrl;
++ const safeFailUrl = sanitizeRedirectUrl(failRedirectUrl);
++ if (safeFailUrl) window.location.href = safeFailUrl;
+ }
+ }
+
+ // --- ACTION HOOK ENGINE (Dispatches triggers to merchant website and backend telemetry) ---
+ function emitTriggerHook(action, details = {}) {
+ try {
- window.parent.postMessage({
++ postToMerchant({
+ type: 'YOUPI_PAYMENT_HOOK',
+ action,
+ paymentId: PAYMENT_ID,
+ orderId: state?.orderId,
+ details
- }, '*');
++ });
+ } catch { }
+
+ fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/trigger-hook`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ action, details })
+ }).catch(() => { });
+ }
+
+ // --- SAVED / REMEMBERED CUSTOMER ACCOUNT (STRICT SINGLE USER) ---
+ function getRememberedAccounts() {
+ try {
+ const preserved = getPreservedIdentity();
+ if (preserved && preserved.customer) {
+ return [preserved.customer];
+ }
+ const raw = localStorage.getItem(STORAGE_KEY_ACCOUNTS);
+ if (!raw) return [];
+ const parsed = JSON.parse(raw);
+ if (!Array.isArray(parsed) || parsed.length === 0) return [];
+ return [parsed[0]]; // Strictly single user only
+ } catch {
+ return [];
+ }
+ }
+
+ function saveRememberedAccount(cust, token) {
+ if (!cust || !cust.id) return;
+ savePreservedIdentity(cust, token);
+ }
+
+ function removeRememberedAccount(customerId, e) {
+ if (e) {
+ e.stopPropagation();
+ e.preventDefault();
+ }
+ clearPreservedIdentity();
+ if (activeCustomer && activeCustomer.id === customerId) {
+ activeCustomer = null;
+ activeToken = null;
+ }
+ updateCustomerDisplay(null);
+ renderSavedAccountsUI();
+ }
+
+ function renderSavedAccountsUI() {
+ const accounts = getRememberedAccounts();
+ const container = document.getElementById('preAuthSavedAccountsContainer');
+ const listEl = document.getElementById('savedAccountsList');
+ const badge = document.getElementById('savedAccountCountBadge');
+
+ if (badge) badge.style.display = 'none';
+
+ if (container && listEl) {
+ if (accounts.length > 0) {
+ container.hidden = false;
+ let html = '';
+ accounts.slice(0, 1).forEach(acc => {
+ const initial = (acc.name || 'U').charAt(0).toUpperCase();
+ const isActive = activeCustomer && activeCustomer.id === acc.id;
+ html += `
+ <div class="saved-account-row ${isActive ? 'active' : ''}" onclick="handleSelectSavedAccount('${acc.id}', event)">
+ <div style="display:flex; align-items:center; gap:8px;">
+ <div style="width:28px; height:28px; border-radius:50%; background:#e0f2fe; color:#0369a1; font-weight:800; font-size:12px; display:flex; align-items:center; justify-content:center;">${initial}</div>
+ <div>
+ <div style="font-size:13px; font-weight:700;">${escapeHtml(acc.name)}</div>
+ <div style="font-size:11px; color:#64748b;">${escapeHtml(acc.email || '')} ${acc.mobileNumber ? '&bull; ' + escapeHtml(acc.mobileNumber) : ''}</div>
+ </div>
+ </div>
+ <div style="display:flex; align-items:center; gap:6px;">
+ <button type="button" class="ypg-btn-select-account" style="background:#eff6ff; border:1px solid #bfdbfe; color:var(--ypg-blue); font-size:11px; font-weight:700; padding:4px 10px; border-radius:6px; cursor:pointer; display:inline-flex; align-items:center; gap:2px; transition:all 0.15s;" onclick="handleSelectSavedAccount('${acc.id}', event)">
+ <span>Select</span> &rsaquo;
+ </button>
+ <button type="button" style="background:none; border:none; color:#94a3b8; font-size:14px; cursor:pointer; padding:2px 6px;" onclick="removeRememberedAccount('${acc.id}', event)" title="Remove Account">✕</button>
+ </div>
+ </div>
+ `;
+ });
+ listEl.innerHTML = html;
+ } else {
+ container.hidden = true;
+ listEl.innerHTML = '';
+ }
+ }
+ }
+
+ let currentAuthMobile = '';
+ let currentAuthName = '';
+ let currentAuthEmail = '';
+ let currentAuthStep = 'mobile';
+ let hasAttemptedAutoLogin = false;
+ let otpCooldownTimer = null;
+ let otpCooldownSec = 0;
+
+ function setGatewayAuthMode(isAuthView) {
+ const authEl = document.getElementById('preGatewayAuthView');
+ const payEl = document.getElementById('paymentGatewayInterface');
+ const footerEl = document.getElementById('ypgStickyFooter');
+ const pcTopbarTitle = document.querySelector('.ypg-pc-topbar-title');
+
+ if (isAuthView) {
+ if (authEl) {
+ authEl.hidden = false;
+ authEl.style.removeProperty('display');
+ }
+ if (payEl) {
+ payEl.hidden = true;
+ payEl.style.setProperty('display', 'none', 'important');
+ }
+ if (footerEl) {
+ footerEl.style.display = 'none';
+ }
+ if (pcTopbarTitle) {
+ pcTopbarTitle.textContent = 'Sign in to Pay';
+ }
+ } else {
+ if (authEl) {
+ authEl.hidden = true;
+ authEl.style.setProperty('display', 'none', 'important');
+ }
+ if (payEl) {
+ payEl.hidden = false;
+ payEl.style.removeProperty('display');
+ }
+ if (footerEl) {
+ footerEl.style.display = '';
+ }
+ if (pcTopbarTitle) {
+ pcTopbarTitle.textContent = 'Payment Options';
+ }
+ }
+ }
+
+ function goToAuthStep(step) {
+ currentAuthStep = step;
+ const stepMobile = document.getElementById('authStepMobile');
+ const stepSignup = document.getElementById('authStepSignup');
+ const stepOtp = document.getElementById('authStepOtp');
+ const title = document.getElementById('authViewMainTitle');
+ const subtitle = document.getElementById('authViewSubTitle');
+ const savedAccountsBox = document.getElementById('preAuthSavedAccountsContainer');
+
+ if (step === 'mobile') {
+ if (stepMobile) stepMobile.hidden = false;
+ if (stepSignup) stepSignup.hidden = true;
+ if (stepOtp) stepOtp.hidden = true;
+ if (savedAccountsBox && getRememberedAccounts().length > 0) savedAccountsBox.hidden = false;
+ if (title) title.innerText = 'Sign in to Pay';
+ if (subtitle) subtitle.innerText = 'Enter your 10-digit WhatsApp mobile number to continue';
+ const input = document.getElementById('authMobileInput');
+ if (input) {
+ if (currentAuthMobile && !input.value) {
+ input.value = currentAuthMobile;
+ }
+ if (document.activeElement !== input) {
+ setTimeout(() => input.focus(), 50);
+ }
+ }
+ } else if (step === 'signup') {
+ if (stepMobile) stepMobile.hidden = true;
+ if (stepSignup) stepSignup.hidden = false;
+ if (stepOtp) stepOtp.hidden = true;
+ if (savedAccountsBox) savedAccountsBox.hidden = true;
+ if (title) title.innerText = 'Create Account';
+ if (subtitle) subtitle.innerText = 'Complete your profile to proceed with instant checkout';
+ const badge = document.getElementById('authSignupPhoneBadge');
+ if (badge) badge.innerText = '+91 ' + currentAuthMobile;
+ const nameInput = document.getElementById('authSignupName');
+ const emailInput = document.getElementById('authSignupEmail');
+ if (nameInput && currentAuthName && !nameInput.value) nameInput.value = currentAuthName;
+ if (emailInput && currentAuthEmail && !emailInput.value) emailInput.value = currentAuthEmail;
+ if (nameInput && document.activeElement !== nameInput && document.activeElement !== emailInput) {
+ setTimeout(() => nameInput.focus(), 50);
+ }
+ } else if (step === 'otp') {
+ if (stepMobile) stepMobile.hidden = true;
+ if (stepSignup) stepSignup.hidden = true;
+ if (stepOtp) stepOtp.hidden = false;
+ if (savedAccountsBox) savedAccountsBox.hidden = true;
+ if (title) title.innerText = 'Verify Mobile OTP';
+ if (subtitle) subtitle.innerText = 'Enter the 6-digit code sent to your WhatsApp number';
+ const badge = document.getElementById('authOtpPhoneBadge');
+ if (badge) badge.innerText = '+91 ' + currentAuthMobile;
+ const otpInput = document.getElementById('authOtpInput');
+ if (otpInput) {
+ otpInput.value = '';
+ setTimeout(() => otpInput.focus(), 50);
+ }
+ startOtpCooldown(30);
+ }
+ }
+
+ function startOtpCooldown(sec = 30) {
+ clearInterval(otpCooldownTimer);
+ otpCooldownSec = sec;
+ const resendBtn = document.getElementById('btnResendOtp');
+ const countdownEl = document.getElementById('otpCountdownTimer');
+
+ const updateUI = () => {
+ if (otpCooldownSec > 0) {
+ if (resendBtn) {
+ resendBtn.disabled = true;
+ resendBtn.style.opacity = '0.5';
+ resendBtn.style.cursor = 'not-allowed';
+ }
+ if (countdownEl) countdownEl.innerText = `(${otpCooldownSec}s)`;
+ otpCooldownSec--;
+ } else {
+ clearInterval(otpCooldownTimer);
+ if (resendBtn) {
+ resendBtn.disabled = false;
+ resendBtn.style.opacity = '1';
+ resendBtn.style.cursor = 'pointer';
+ }
+ if (countdownEl) countdownEl.innerText = '';
+ }
+ };
+
+ updateUI();
+ otpCooldownTimer = setInterval(updateUI, 1000);
+ }
+
+ async function handleAuthMobileSubmit() {
+ const input = document.getElementById('authMobileInput');
+ const btn = document.getElementById('btnAuthContinue');
+ const raw = input ? input.value : '';
+ const clean = String(raw).replace(/\D/g, '').slice(-10);
+
+ if (!clean || clean.length !== 10) {
+ showOptionToast('Please enter a valid 10-digit mobile number');
+ input?.focus();
+ return;
+ }
+
+ currentAuthMobile = clean;
+ const oldHtml = btn.innerHTML;
+ btn.disabled = true;
+ btn.innerHTML = '<span>Checking account...</span>';
+
+ try {
+ const res = await fetch(`${API_BASE}/api/v1/customer/check-user`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ mobileNumber: clean })
+ });
+ const data = await res.json();
+
+ if (!res.ok) {
+ showOptionToast(data.error || 'Failed to check mobile number');
+ return;
+ }
+
+ const accounts = getRememberedAccounts();
+ const savedMatch = accounts.find(a => String(a.mobileNumber || '').replace(/\D/g, '').slice(-10) === clean);
+
+ if (data.exists && data.customer) {
+ // Existing customer in DB -> automatically send OTP and show OTP screen
+ currentAuthName = data.customer.name;
+ currentAuthEmail = data.customer.email;
+ btn.innerHTML = '<span>Sending WhatsApp OTP...</span>';
+
+ const sendRes = await fetch(`${API_BASE}/api/v1/customer/send-otp`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({
+ mobileNumber: clean
+ })
+ });
+ const sendData = await sendRes.json();
+ if (sendData && sendData.devOtp) lastReceivedDevOtp = sendData.devOtp;
+
+ const greetingEl = document.getElementById('authOtpGreeting');
+ if (greetingEl) {
+ greetingEl.innerHTML = `Welcome back, <strong>${escapeHtml(data.customer.name)}</strong>! Enter the 6-digit WhatsApp OTP:`;
+ }
+
+ goToAuthStep('otp');
+ showOptionToast('OTP sent to your WhatsApp number!');
+ } else if (savedMatch) {
+ // Known customer in browser saved accounts -> dispatch OTP directly with saved profile
+ currentAuthName = savedMatch.name || 'Customer';
+ currentAuthEmail = savedMatch.email || '';
+ btn.innerHTML = '<span>Sending WhatsApp OTP...</span>';
+
+ const sendRes = await fetch(`${API_BASE}/api/v1/customer/send-otp`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({
+ mobileNumber: clean,
+ name: currentAuthName,
+ email: currentAuthEmail
+ })
+ });
+ const sendData = await sendRes.json();
+ if (sendData && sendData.devOtp) lastReceivedDevOtp = sendData.devOtp;
+
+ const greetingEl = document.getElementById('authOtpGreeting');
+ if (greetingEl) {
+ greetingEl.innerHTML = `Welcome back, <strong>${escapeHtml(currentAuthName)}</strong>! Enter the 6-digit WhatsApp OTP:`;
+ }
+
+ goToAuthStep('otp');
+ showOptionToast('OTP sent to your WhatsApp number!');
+ } else {
+ // New customer -> prompt for name and email
+ goToAuthStep('signup');
+ }
+ } catch (e) {
+ showOptionToast('Network error checking mobile number.');
+ } finally {
+ btn.disabled = false;
+ btn.innerHTML = oldHtml;
+ }
+ }
+
+ async function handleAuthSignupSubmit() {
+ const nameInput = document.getElementById('authSignupName');
+ const emailInput = document.getElementById('authSignupEmail');
+ const btn = document.getElementById('btnAuthSignup');
+
+ const name = nameInput ? nameInput.value.trim() : '';
+ const email = emailInput ? emailInput.value.trim() : '';
+
+ if (!name) {
+ showOptionToast('Please enter your full name');
+ nameInput?.focus();
+ return;
+ }
+ if (!email || !email.includes('@') || !email.includes('.')) {
+ showOptionToast('Please enter a valid email address');
+ emailInput?.focus();
+ return;
+ }
+
+ currentAuthName = name;
+ currentAuthEmail = email;
+
+ const oldHtml = btn.innerHTML;
+ btn.disabled = true;
+ btn.innerHTML = '<span>Sending WhatsApp OTP...</span>';
+
+ try {
+ const res = await fetch(`${API_BASE}/api/v1/customer/send-otp`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({
+ mobileNumber: currentAuthMobile,
+ name,
+ email,
+ isNewUser: true
+ })
+ });
+ const data = await res.json();
+ if (data && data.devOtp) lastReceivedDevOtp = data.devOtp;
+
+ if (res.ok) {
+ const greetingEl = document.getElementById('authOtpGreeting');
+ if (greetingEl) {
+ greetingEl.innerHTML = `Hello <strong>${escapeHtml(name)}</strong>! Enter the 6-digit WhatsApp OTP:`;
+ }
+ goToAuthStep('otp');
+ showOptionToast('WhatsApp verification code sent!');
+ } else {
+ showOptionToast(data.error || 'Failed to send WhatsApp OTP.');
+ }
+ } catch (e) {
+ showOptionToast('Network error sending WhatsApp OTP.');
+ } finally {
+ btn.disabled = false;
+ btn.innerHTML = oldHtml;
+ }
+ }
+
+ async function handleAuthVerifyOtpSubmit() {
+ const otpInput = document.getElementById('authOtpInput');
+ const btn = document.getElementById('btnAuthVerifyOtp');
+ const otp = otpInput ? otpInput.value.trim() : '';
+
+ if (!otp || otp.length < 6) {
+ showOptionToast('Please enter the 6-digit WhatsApp OTP');
+ otpInput?.focus();
+ return;
+ }
+
+ const oldHtml = btn.innerHTML;
+ btn.disabled = true;
+ btn.innerHTML = '<span>Verifying OTP...</span>';
+
+ try {
+ const res = await fetch(`${API_BASE}/api/v1/customer/verify-otp`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({
+ mobileNumber: currentAuthMobile,
+ otp,
+ name: currentAuthName,
+ email: currentAuthEmail
+ })
+ });
+ const data = await res.json();
+
+ if (res.ok && data.customer) {
+ activeCustomer = data.customer;
+ activeToken = data.token;
+ saveRememberedAccount(activeCustomer, activeToken);
+
+ // Bind session
+ await fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/bind-customer`, {
+ method: 'POST',
+ headers: getApiHeaders({
+ 'Content-Type': 'application/json',
+ 'Authorization': `Bearer ${activeToken}`
+ }),
+ body: JSON.stringify({ token: activeToken, customerId: activeCustomer.id })
+ });
+
+ emitTriggerHook('AUTH_COMPLETED', { customerId: activeCustomer.id });
+
+ setGatewayAuthMode(false);
+ updateCustomerDisplay(activeCustomer);
+ loadState();
+ showOptionToast(`Welcome, ${activeCustomer.name}! Verified via WhatsApp.`);
+ } else {
+ showOptionToast(data.error || 'Invalid or expired OTP. Please try again.');
+ otpInput.select();
+ }
+ } catch (e) {
+ showOptionToast('Network error verifying OTP.');
+ } finally {
+ btn.disabled = false;
+ btn.innerHTML = oldHtml;
+ }
+ }
+
+ async function handleResendOtp() {
+ if (otpCooldownSec > 0) return;
+ showOptionToast('Resending WhatsApp OTP...');
+ try {
+ const res = await fetch(`${API_BASE}/api/v1/customer/send-otp`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({
+ mobileNumber: currentAuthMobile,
+ name: currentAuthName,
+ email: currentAuthEmail
+ })
+ });
+ const data = await res.json();
+ if (res.ok) {
+ startOtpCooldown(30);
+ showOptionToast('Fresh OTP dispatched to your WhatsApp number!');
+ const otpInput = document.getElementById('authOtpInput');
+ if (otpInput) {
+ otpInput.value = '';
+ otpInput.focus();
+ }
+ } else {
+ showOptionToast(data.error || 'Failed to resend OTP.');
+ }
+ } catch (e) {
+ showOptionToast('Network error resending OTP.');
+ }
+ }
+
+ async function handleSelectSavedAccount(accountId, event) {
+ if (event) {
+ event.stopPropagation();
+ }
+ const accounts = getRememberedAccounts();
+ const account = accounts.find(a => a.id === accountId);
+ if (!account) return;
+
+ const rawPhone = account.mobileNumber || '';
+ const clean = String(rawPhone).replace(/\D/g, '').slice(-10);
+
+ if (!clean || clean.length !== 10) {
+ showOptionToast('Saved account does not have a valid 10-digit mobile number.');
+ const input = document.getElementById('authMobileInput');
+ if (input) {
+ input.value = rawPhone;
+ input.focus();
+ }
+ return;
+ }
+
+ currentAuthMobile = clean;
+ currentAuthName = account.name || 'Customer';
+ currentAuthEmail = account.email || '';
+
+ const input = document.getElementById('authMobileInput');
+ if (input) input.value = clean;
+
+ // Provide visual loading feedback on the clicked button if present
+ const selectBtn = event?.currentTarget?.querySelector?.('.ypg-btn-select-account') ||
+ (event?.target?.classList?.contains('ypg-btn-select-account') ? event.target : null);
+ const oldBtnHtml = selectBtn ? selectBtn.innerHTML : null;
+ if (selectBtn) {
+ selectBtn.disabled = true;
+ selectBtn.innerHTML = '<span>Sending OTP...</span>';
+ }
+
+ showOptionToast(`Sending WhatsApp OTP to +91 ${clean}...`);
+
+ try {
+ const res = await fetch(`${API_BASE}/api/v1/customer/send-otp`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({
+ mobileNumber: clean,
+ name: currentAuthName,
+ email: currentAuthEmail
+ })
+ });
+ const data = await res.json();
+ if (data && data.devOtp) lastReceivedDevOtp = data.devOtp;
+
+ if (!res.ok) {
+ showOptionToast(data.error || 'Failed to send WhatsApp OTP.');
+ return;
+ }
+
+ const greetingEl = document.getElementById('authOtpGreeting');
+ if (greetingEl) {
+ greetingEl.innerHTML = `Welcome back, <strong>${escapeHtml(account.name || 'Customer')}</strong>! Enter the 6-digit WhatsApp OTP:`;
+ }
+ const badge = document.getElementById('authOtpPhoneBadge');
+ if (badge) badge.innerText = '+91 ' + clean;
+
+ goToAuthStep('otp');
+ showOptionToast('WhatsApp verification code sent!');
+ } catch (err) {
+ console.error('[Select Saved Account Error]:', err);
+ showOptionToast('Network error sending WhatsApp OTP.');
+ } finally {
+ if (selectBtn && oldBtnHtml) {
+ selectBtn.disabled = false;
+ selectBtn.innerHTML = oldBtnHtml;
+ }
+ }
+ }
+
+ function updateCustomerDisplay(cust) {
+ const displayName = cust
+ ? `${cust.fullName || cust.name || 'Customer'} (${cust.mobileNumber || cust.phoneNumber || cust.email || ''})`
+ : 'Guest User';
+ const nameElMobile = document.getElementById('popoverCustomerName');
+ if (nameElMobile) nameElMobile.textContent = displayName;
+ const nameElPc = document.getElementById('popoverCustomerNamePc');
+ if (nameElPc) nameElPc.textContent = displayName;
+
+ const pcCustomerText = document.getElementById('pcCustomerProfileText');
+ if (pcCustomerText) {
+ if (!cust) {
+ pcCustomerText.textContent = 'Using as Guest User';
+ return;
+ }
+ const name = cust.fullName || cust.name || cust.customerName || '';
+ const phone = cust.mobileNumber || cust.phoneNumber || '';
+ const clean = phone ? String(phone).replace(/^\+91/, '').replace(/\D/g, '').slice(-10) : '';
+ if (name && clean) {
+ pcCustomerText.textContent = `Using as ${name} (+91 ${clean})`;
+ } else if (name) {
+ pcCustomerText.textContent = `Using as ${name}`;
+ } else if (clean) {
+ pcCustomerText.textContent = `Using as +91 ${clean}`;
+ } else if (cust.email) {
+ pcCustomerText.textContent = `Using as ${cust.email}`;
+ } else {
+ pcCustomerText.textContent = 'Using as Guest User';
+ }
+ }
+ }
+
+ function toggleAccountMenu(e) {
+ if (e) {
+ e.stopPropagation();
+ e.preventDefault();
+ }
+ const isPc = window.innerWidth > 600 && !document.body.classList.contains('is-embedded-mobile');
+ const popover = isPc
+ ? (document.getElementById('accountMenuPopoverPc') || document.getElementById('accountMenuPopover'))
+ : (document.getElementById('accountMenuPopover') || document.getElementById('accountMenuPopoverPc'));
+ if (!popover) return;
+
+ const isHidden = popover.hidden || popover.style.display === 'none' || popover.hasAttribute('hidden');
+ if (isHidden) {
+ popover.removeAttribute('hidden');
+ popover.hidden = false;
+ popover.style.display = 'block';
+ const dismissHandler = (evt) => {
+ if (!popover.contains(evt.target) &&
+ !evt.target.closest('#btnProfileMenu') &&
+ !evt.target.closest('.ypg-pc-btn-more') &&
+ !evt.target.closest('#btnPcMore') &&
+ !evt.target.closest('#pcCustomerProfileRow')) {
+ popover.setAttribute('hidden', '');
+ popover.hidden = true;
+ popover.style.display = 'none';
+ document.removeEventListener('click', dismissHandler);
+ }
+ };
+ setTimeout(() => document.addEventListener('click', dismissHandler), 10);
+ } else {
+ popover.setAttribute('hidden', '');
+ popover.hidden = true;
+ popover.style.display = 'none';
+ }
+ }
+
+ async function handleCustomerLogout() {
+ const popoverMobile = document.getElementById('accountMenuPopover');
+ if (popoverMobile) { popoverMobile.setAttribute('hidden', ''); popoverMobile.hidden = true; popoverMobile.style.display = 'none'; }
+ const popoverPc = document.getElementById('accountMenuPopoverPc');
+ if (popoverPc) { popoverPc.setAttribute('hidden', ''); popoverPc.hidden = true; popoverPc.style.display = 'none'; }
+
+ if (activeToken) {
+ try {
+ await fetch(`${API_BASE}/api/v1/customer/logout`, {
+ method: 'POST',
+ headers: { 'Authorization': `Bearer ${activeToken}` }
+ });
+ } catch { }
+ }
+
+ clearPreservedIdentity();
+ activeCustomer = null;
+ activeToken = null;
+ hasAttemptedAutoLogin = false;
+ updateCustomerDisplay(null);
+ setGatewayAuthMode(true);
+ goToAuthStep('mobile');
+ renderSavedAccountsUI();
+ }
+
+ function escapeHtml(str) {
+ if (!str) return '';
+ return String(str).replace(/[&<>"']/g, m => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' })[m]);
+ }
+
+ let currentSelectedMethod = 'UPI';
+
+ function hideAllContent() {
+ document.getElementById('viewUpiContent').hidden = true;
+ const vCod = document.getElementById('viewCodContent');
+ if (vCod) vCod.hidden = true;
+ const vCash = document.getElementById('viewCashContent');
+ if (vCash) vCash.hidden = true;
+ const vCard = document.getElementById('viewCardContent');
+ if (vCard) vCard.hidden = true;
+ document.getElementById('viewProcessingContent').hidden = true;
+ document.getElementById('viewSuccessContent').hidden = true;
+ document.getElementById('viewFailedContent').hidden = true;
+ document.getElementById('viewQueuedContent').hidden = true;
+ const vBlocked = document.getElementById('viewBlockedContent');
+ if (vBlocked) vBlocked.hidden = true;
+ const vDup = document.getElementById('viewDuplicateSessionContent');
+ if (vDup) vDup.hidden = true;
+ }
+
+ function formatTime(totalSec) {
+ const m = Math.floor(totalSec / 60);
+ const s = totalSec % 60;
+ return `${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`;
+ }
+
+ let _activeCountdownTargetTime = null;
+
+ function runCountdown(seconds, onTick, onDone, targetExpiresAt) {
+ const targetTime = targetExpiresAt
+ ? new Date(targetExpiresAt).getTime()
+ : (_activeCountdownTargetTime || (Date.now() + (seconds || 300) * 1000));
+
+ if (countdownTimer && _activeCountdownTargetTime && Math.abs(_activeCountdownTargetTime - targetTime) <= 2500) {
+ return;
+ }
+
+ _activeCountdownTargetTime = targetTime;
+ if (countdownTimer) clearInterval(countdownTimer);
+
+ const calcRemaining = () => {
+ return Math.max(0, Math.ceil((targetTime - Date.now()) / 1000));
+ };
+
+ const doTick = (sec) => {
+ onTick(sec);
+ const tStr = formatTime(sec);
+ document.querySelectorAll('.ypg-banner-running-timer').forEach(el => {
+ el.textContent = tStr;
+ });
+ };
+ doTick(calcRemaining());
+
+ countdownTimer = setInterval(() => {
+ const remaining = calcRemaining();
+ if (remaining <= 0) {
+ clearInterval(countdownTimer);
+ countdownTimer = null;
+ _activeCountdownTargetTime = null;
+ doTick(0);
+ if (onDone) onDone();
+ } else {
+ doTick(remaining);
+ }
+ }, 1000);
+ }
+
+ function renderMethodSelectorBar(data) {
+ const bar = document.getElementById('paymentMethodSelectorBar');
+ const sellingMode = data.sellingMode || 'ONLINE';
+ const enabledMethods = data.enabledMethods || ['UPI'];
+
+ // Populate authenticated customer into COD view
+ const c = data.customer || activeCustomer;
+ if (c) {
+ const nameEl = document.getElementById('codCustomerName');
+ const phoneEl = document.getElementById('codCustomerPhone');
+ if (nameEl) nameEl.textContent = c.fullName || c.name || 'Customer';
+ if (phoneEl) phoneEl.textContent = c.phoneNumber || c.mobileNumber || '-';
+ }
+
+ // Maintain authentic 3-column desktop layout
+ const pcMethodsList = document.getElementById('pcMethodsList');
+ const pcActiveView = document.querySelector('.ypg-pc-active-view');
+ const pcTopbarTitle = document.querySelector('.ypg-pc-topbar-title');
+
+ if (pcMethodsList) pcMethodsList.style.removeProperty('display');
+ if (pcActiveView) pcActiveView.style.width = '';
+ if (pcTopbarTitle) pcTopbarTitle.textContent = 'Payment Options';
+
+ // Mobile selector bar is visible only when more than 1 method is available
+ if (bar) {
+ bar.style.display = (!enabledMethods || enabledMethods.length <= 1) ? 'none' : 'flex';
+ }
+
+ const tabUpi = document.getElementById('tabMethodUpi');
+ const tabCod = document.getElementById('tabMethodCod');
+ const tabCash = document.getElementById('tabMethodCash');
+ const tabCard = document.getElementById('tabMethodCard');
+
+ if (tabUpi) tabUpi.style.display = 'inline-flex';
+
+ const pcUpi = document.getElementById('pcMethodCardUpi');
+ const pcCod = document.getElementById('pcMethodCardCod');
+ const pcCash = document.getElementById('pcMethodCardCash');
+ const pcCard = document.getElementById('pcMethodCardCard');
+
+ if (pcUpi) pcUpi.style.display = 'block';
+
+ if (sellingMode === 'ONLINE') {
+ // Online Selling Mode:
+ // COD works (configurable ON/OFF).
+ // Cash on counter and Card on counter do NOT exist (hidden).
+ if (tabCod) tabCod.style.display = enabledMethods.includes('COD') ? 'inline-flex' : 'none';
+ if (tabCash) tabCash.style.display = 'none';
+ if (tabCard) tabCard.style.display = 'none';
+
+ if (pcCod) pcCod.style.display = enabledMethods.includes('COD') ? 'block' : 'none';
+ if (pcCash) pcCash.style.display = 'none';
+ if (pcCard) pcCard.style.display = 'none';
+ } else {
+ // Offline Selling Mode:
+ // Cash works (configurable ON/OFF).
+ // Card works (configurable ON/OFF).
+ // COD does NOT exist (hidden).
+ if (tabCod) tabCod.style.display = 'none';
+ if (tabCash) tabCash.style.display = enabledMethods.includes('CASH_COUNTER') ? 'inline-flex' : 'none';
+ if (tabCard) tabCard.style.display = enabledMethods.includes('CARD_COUNTER') ? 'inline-flex' : 'none';
+
+ if (pcCod) pcCod.style.display = 'none';
+ if (pcCash) pcCash.style.display = enabledMethods.includes('CASH_COUNTER') ? 'block' : 'none';
+ if (pcCard) pcCard.style.display = enabledMethods.includes('CARD_COUNTER') ? 'block' : 'none';
+ }
+
+ if (!enabledMethods.includes(currentSelectedMethod)) {
+ currentSelectedMethod = 'UPI';
+ }
+ updateMethodTabsUI(currentSelectedMethod);
+ }
+
+ function updateMethodTabsUI(method) {
+ const tabs = [
+ { id: 'tabMethodUpi', method: 'UPI' },
+ { id: 'tabMethodCod', method: 'COD' },
+ { id: 'tabMethodCash', method: 'CASH_COUNTER' },
+ { id: 'tabMethodCard', method: 'CARD_COUNTER' }
+ ];
+ tabs.forEach(t => {
+ const el = document.getElementById(t.id);
+ if (!el) return;
+ if (t.method === method) {
+ el.classList.add('active');
+ } else {
+ el.classList.remove('active');
+ }
+ });
+
+ const pcCards = [
+ { id: 'pcMethodCardUpi', method: 'UPI' },
+ { id: 'pcMethodCardCod', method: 'COD' },
+ { id: 'pcMethodCardCash', method: 'CASH_COUNTER' },
+ { id: 'pcMethodCardCard', method: 'CARD_COUNTER' }
+ ];
+ pcCards.forEach(c => {
+ const el = document.getElementById(c.id);
+ if (!el) return;
+ if (c.method === method) {
+ el.classList.add('is-active');
+ } else {
+ el.classList.remove('is-active');
+ }
+ });
+ }
+
+ function switchMethodFromDowntime() {
+ const enabled = state?.enabledMethods || state?.branding?.enabledPaymentMethods || [];
+ const sellingMode = state?.sellingMode || state?.branding?.sellingMode || 'OFFLINE';
+ if (sellingMode === 'ONLINE' && enabled.includes('COD')) {
+ selectPaymentMethod('COD');
+ } else if (enabled.includes('CASH_COUNTER')) {
+ selectPaymentMethod('CASH_COUNTER');
+ }
+ }
+
+ async function selectPaymentMethod(method) {
+ currentSelectedMethod = method;
+ updateMethodTabsUI(method);
+ hideAllContent();
+
+ if (method === 'UPI') {
+ document.getElementById('viewUpiContent').hidden = false;
+ document.getElementById('ypgStickyFooter').style.display = 'flex';
+ } else if (method === 'COD') {
+ const vCod = document.getElementById('viewCodContent');
+ if (vCod) vCod.hidden = false;
+ document.getElementById('ypgStickyFooter').style.display = 'none';
+ } else if (method === 'CASH_COUNTER') {
+ const vCash = document.getElementById('viewCashContent');
+ if (vCash) vCash.hidden = false;
+ document.getElementById('ypgStickyFooter').style.display = 'none';
+ } else if (method === 'CARD_COUNTER') {
+ const vCard = document.getElementById('viewCardContent');
+ if (vCard) vCard.hidden = false;
+ document.getElementById('ypgStickyFooter').style.display = 'none';
+ }
+
+ try {
+ const res = await fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/select-method`, {
+ method: 'POST',
+ headers: getApiHeaders({ 'Content-Type': 'application/json' }),
+ body: JSON.stringify({ method, deviceId: DeviceIdentity?.currentDeviceId })
+ });
+ const resData = await res.json();
+ if (res.ok && resData) {
+ state.method = method;
+ if (resData.timeRemainingSeconds !== undefined) {
+ state.timeRemainingSeconds = resData.timeRemainingSeconds;
+ }
+ if (resData.status) {
+ state.status = resData.status;
+ }
+
+ if (method === 'COD') {
+ runCountdown(resData.timeRemainingSeconds || 300, sec => {
+ const el = document.getElementById('codTimerText');
+ if (el) el.textContent = formatTime(sec);
+ }, () => {
+ const el = document.getElementById('codTimerText');
+ if (el) el.textContent = 'Expired';
+ fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/expire`, { method: 'POST' }).catch(() => { });
+ loadState();
+ });
+ } else if (method === 'UPI') {
+ if (resData.status === 'ACTIVE') {
+ loadQrSvg();
+ runCountdown(resData.timeRemainingSeconds || 300, sec => {
+ const el = document.getElementById('activeTimerText');
+ if (el) el.textContent = formatTime(sec);
+ }, () => {
+ const el = document.getElementById('activeTimerText');
+ if (el) el.textContent = 'Expired';
+ fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/expire`, { method: 'POST' }).catch(() => { });
+ loadState();
+ });
+ } else if (resData.status === 'QUEUED') {
+ loadState();
+ }
+ } else {
+ // Offline Counter mode: no timer countdown
+ if (countdownTimer) clearInterval(countdownTimer);
+ }
+ }
+ } catch (e) {
+ console.warn('selectPaymentMethod network error:', e);
+ }
+ }
+
+ async function promptConfirmCodOrder() {
+ const amt = state?.amount || '' + (state?.amount || "0.00") + '';
+ if (!confirm(`Are you sure you want to confirm and place this Cash on Delivery (COD) order for ₹${amt}? You will pay in cash upon doorstep delivery.`)) {
+ return;
+ }
+ await executeConfirmCodOrder();
+ }
+
+ async function executeConfirmCodOrder() {
+ const btn = document.getElementById('btnConfirmCod');
+ if (btn) {
+ btn.disabled = true;
+ btn.innerText = 'Placing Cash on Delivery Order...';
+ }
+
+ try {
+ const res = await fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/confirm-cod`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' }
+ });
+ const data = await res.json();
+ if (res.ok) {
+ loadState();
+ } else {
+ alert(data.error || 'Failed to place COD order');
+ if (btn) {
+ btn.disabled = false;
+ btn.innerHTML = '<span>Confirm &amp; Place COD Order (₹' + (state?.amount || '' + (state?.amount || "0.00") + '') + ')</span> &rsaquo;';
+ }
+ }
+ } catch (e) {
+ alert('Network error confirming COD order.');
+ if (btn) {
+ btn.disabled = false;
+ btn.innerHTML = '<span>Confirm &amp; Place COD Order (₹' + (state?.amount || '' + (state?.amount || "0.00") + '') + ')</span> &rsaquo;';
+ }
+ }
+ }
+
+ // --- DEVICE IDENTITY & HARDWARE CONTEXT BINDING LAYER ---
+ const DeviceIdentity = {
+ dbName: 'youpi_device_keystore_v1',
+ storeName: 'keys',
+ currentDeviceId: null,
+ currentDeviceToken: null,
+
+ async openDb() {
+ return new Promise((resolve) => {
+ if (!window.indexedDB) {
+ resolve(null);
+ return;
+ }
+ const req = window.indexedDB.open(this.dbName, 1);
+ req.onupgradeneeded = (e) => {
+ const db = e.target.result;
+ if (!db.objectStoreNames.contains(this.storeName)) {
+ db.createObjectStore(this.storeName, { keyPath: 'id' });
+ }
+ };
+ req.onsuccess = () => resolve(req.result);
+ req.onerror = () => resolve(null);
+ });
+ },
+
+ async getStoredRecord() {
+ try {
+ const db = await this.openDb();
+ if (!db) return null;
+ return new Promise((resolve) => {
+ const tx = db.transaction(this.storeName, 'readonly');
+ const store = tx.objectStore(this.storeName);
+ const req = store.get('primary_device');
+ req.onsuccess = () => resolve(req.result || null);
+ req.onerror = () => resolve(null);
+ });
+ } catch {
+ return null;
+ }
+ },
+
+ async saveRecord(record) {
+ try {
+ const db = await this.openDb();
+ if (!db) return;
+ return new Promise((resolve) => {
+ const tx = db.transaction(this.storeName, 'readwrite');
+ const store = tx.objectStore(this.storeName);
+ store.put({ id: 'primary_device', ...record });
+ tx.oncomplete = () => resolve();
+ tx.onerror = () => resolve();
+ });
+ } catch {}
+ },
+
+ collectSignals() {
+ let canvasSig = '';
+ try {
+ const canvas = document.createElement('canvas');
+ canvas.width = 120;
+ canvas.height = 30;
+ const ctx = canvas.getContext('2d');
+ ctx.textBaseline = 'top';
+ ctx.font = "14px 'Arial'";
+ ctx.fillText('YouPI-PG-Device-Binding-2026', 2, 2);
+ canvasSig = canvas.toDataURL().slice(-32);
+ } catch {}
+
+ let webglRenderer = '';
+ try {
+ const gl = document.createElement('canvas').getContext('webgl');
+ if (gl) {
+ const dbg = gl.getExtension('WEBGL_debug_renderer_info');
+ if (dbg) webglRenderer = gl.getParameter(dbg.UNMASKED_RENDERER_WEBGL);
+ }
+ } catch {}
+
+ return {
+ userAgent: navigator.userAgent || '',
+ platform: navigator.platform || '',
+ language: navigator.language || '',
+ timezone: (Intl && Intl.DateTimeFormat) ? Intl.DateTimeFormat().resolvedOptions().timeZone : '',
+ screenSignature: `${window.screen?.width || 0}x${window.screen?.height || 0}x${window.screen?.colorDepth || 24}`,
+ hardwareConcurrency: navigator.hardwareConcurrency || 0,
+ deviceMemory: navigator.deviceMemory || 0,
+ webglRenderer,
+ canvasSignature: canvasSig
+ };
+ },
+
+ async getOrRegister() {
+ if (this.currentDeviceId) {
+ return { deviceId: this.currentDeviceId, deviceToken: this.currentDeviceToken };
+ }
+
+ // 1. Check local storage / session cache first
+ let cachedId = localStorage.getItem('ypg_device_id');
+ let cachedToken = localStorage.getItem('ypg_device_token');
+
+ // 2. Check IndexedDB persistent store
+ const stored = await this.getStoredRecord();
+ if (stored?.deviceId) {
+ this.currentDeviceId = stored.deviceId;
+ this.currentDeviceToken = stored.deviceToken;
+ localStorage.setItem('ypg_device_id', stored.deviceId);
+ if (stored.deviceToken) localStorage.setItem('ypg_device_token', stored.deviceToken);
+ return { deviceId: stored.deviceId, deviceToken: stored.deviceToken };
+ }
+
+ if (cachedId) {
+ this.currentDeviceId = cachedId;
+ this.currentDeviceToken = cachedToken;
+ return { deviceId: cachedId, deviceToken: cachedToken };
+ }
+
+ // 3. Generate new Web Crypto key pair
+ let publicKeyBase64 = '';
+ try {
+ if (window.crypto && window.crypto.subtle) {
+ const keyPair = await window.crypto.subtle.generateKey(
+ { name: 'ECDSA', namedCurve: 'P-256' },
+ true,
+ ['sign', 'verify']
+ );
+ const spki = await window.crypto.subtle.exportKey('spki', keyPair.publicKey);
+ const bytes = new Uint8Array(spki);
+ let binary = '';
+ for (let i = 0; i < bytes.byteLength; i++) {
+ binary += String.fromCharCode(bytes[i]);
+ }
+ publicKeyBase64 = btoa(binary);
+ }
+ } catch (e) {
+ console.warn('[DeviceIdentity] Web Crypto keygen fallback:', e.message);
+ }
+
+ if (!publicKeyBase64) {
+ publicKeyBase64 = btoa(`fallback-device-key-${Math.random()}-${Date.now()}`);
+ }
+
+ // 4. Register with YouPI backend
+ const signals = this.collectSignals();
+ try {
+ const resp = await fetch(`${API_BASE}/api/v1/devices/register`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ publicKey: publicKeyBase64, signals })
+ });
+ const data = await resp.json();
+ if (data?.deviceId) {
+ this.currentDeviceId = data.deviceId;
+ this.currentDeviceToken = data.deviceToken;
+ localStorage.setItem('ypg_device_id', data.deviceId);
+ if (data.deviceToken) localStorage.setItem('ypg_device_token', data.deviceToken);
+
+ await this.saveRecord({
+ deviceId: data.deviceId,
+ deviceToken: data.deviceToken,
+ publicKeyBase64,
+ registeredAt: new Date().toISOString()
+ });
+
+ return { deviceId: data.deviceId, deviceToken: data.deviceToken };
+ }
+ } catch (err) {
+ console.error('[DeviceIdentity] Registration error:', err);
+ }
+
+ return { deviceId: null, deviceToken: null };
+ }
+ };
+
+ function getApiHeaders(customHeaders = {}) {
+ const headers = { ...customHeaders };
+ if (typeof DeviceIdentity !== 'undefined' && DeviceIdentity?.currentDeviceId) {
+ headers['X-Device-Id'] = DeviceIdentity.currentDeviceId;
+ }
+ if (typeof DeviceIdentity !== 'undefined' && DeviceIdentity?.currentDeviceToken) {
+ headers['X-Device-Token'] = DeviceIdentity.currentDeviceToken;
+ }
+ const token = activeToken || (getPreservedIdentity()?.token) || localStorage.getItem('ypg_customer_token');
+ if (token) {
+ headers['Authorization'] = `Bearer ${token}`;
+ headers['x-customer-token'] = token;
+ }
+ return headers;
+ }
+
+ async function loadState() {
+ try {
+ await DeviceIdentity.getOrRegister();
+ const headers = getApiHeaders();
+
+ const res = await fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/state`, { headers });
+ const data = await res.json().catch(() => ({}));
+
+ if (res.status === 409 || data.error === 'DUPLICATE_SESSION_DETECTED') {
+ renderDuplicateSession(data);
+ dismissSplashScreen();
+ return;
+ }
+
+ if (!res.ok) {
+ dismissSplashScreen();
+ return;
+ }
+
+ renderState(data);
+ dismissSplashScreen();
+ } catch (e) {
+ console.warn('loadState error:', e);
+ dismissSplashScreen();
+ }
+ }
+
+ function adjustHexColor(col, amt) {
+ if (!col) return '#0957cb';
+ try {
+ let usePound = false;
+ if (col[0] === '#') {
+ col = col.slice(1);
+ usePound = true;
+ }
+ if (col.length === 3) col = col[0] + col[0] + col[1] + col[1] + col[2] + col[2];
+ const num = parseInt(col, 16);
+ let r = (num >> 16) + amt;
+ if (r > 255) r = 255; else if (r < 0) r = 0;
+ let b = ((num >> 8) & 0x00FF) + amt;
+ if (b > 255) b = 255; else if (b < 0) b = 0;
+ let g = (num & 0x0000FF) + amt;
+ if (g > 255) g = 255; else if (g < 0) g = 0;
+ return (usePound ? '#' : '') + (g | (b << 8) | (r << 16)).toString(16).padStart(6, '0');
+ } catch {
+ return col;
+ }
+ }
+
+ function applyDynamicTheme(branding) {
+ if (!branding) return;
+ const primary = branding.primaryColor || '#0b63e5';
+ const darkPrimary = adjustHexColor(primary, -25);
+ const root = document.documentElement;
+
+ root.style.setProperty('--ypg-blue', primary);
+ root.style.setProperty('--ypg-blue-dark', darkPrimary);
+
+ const theme = branding.headerTheme || 'gradient';
+ const headerEl = document.querySelector('.ypg-header');
+ const sidebarEl = document.querySelector('.ypg-pc-sidebar');
+
+ if (theme === 'solid') {
+ root.style.setProperty('--ypg-header-gradient', primary);
+ if (headerEl) headerEl.style.background = primary;
+ if (sidebarEl) sidebarEl.style.background = primary;
+ } else if (theme === 'dark') {
+ root.style.setProperty('--ypg-header-gradient', '#0b1329');
+ if (headerEl) headerEl.style.background = '#0b1329';
+ if (sidebarEl) sidebarEl.style.background = 'linear-gradient(180deg, #0b1329 0%, #030712 100%)';
+ } else {
+ const grad = `linear-gradient(180deg, ${primary} 0%, ${darkPrimary} 100%)`;
+ root.style.setProperty('--ypg-header-gradient', grad);
+ if (headerEl) headerEl.style.background = grad;
+ if (sidebarEl) sidebarEl.style.background = grad;
+ }
+ }
+
+ function renderHelpLegalLinks(branding) {
+ const container = document.getElementById('helpModalLegalLinks');
+ if (!container) return;
+ let html = '';
+ if (branding.privacyPolicyUrl) {
+ html += `<a href="${branding.privacyPolicyUrl}" target="_blank" rel="noopener noreferrer" style="color:var(--ypg-blue); text-decoration:underline; font-weight:600;">Privacy Policy &nearr;</a>`;
+ }
+ if (branding.termsUrl) {
+ html += `<a href="${branding.termsUrl}" target="_blank" rel="noopener noreferrer" style="color:var(--ypg-blue); text-decoration:underline; font-weight:600;">Terms of Service &nearr;</a>`;
+ }
+ container.innerHTML = html;
+ container.style.display = html ? 'flex' : 'none';
+ }
+
+ function renderState(data) {
+ state = data;
+ const b = data.branding || {};
+ const appName = b.appName || 'YouPI PG';
+ const merchantName = data.merchantName || 'Merchant Checkout';
+
+ // 1. Dynamic Title
+ document.title = `${merchantName} · ${appName} Checkout`;
+
+ // 2. Dynamic Colors & Header Theme
+ applyDynamicTheme(b);
+
+ // 3. Merchant Name & Avatar
+ if (data.merchantName) {
+ const nameEl = document.getElementById('headerMerchantName');
+ if (nameEl) nameEl.textContent = data.merchantName;
+ const letterEl = document.getElementById('merchantAvatarLetter');
+ if (letterEl) letterEl.textContent = data.merchantName.charAt(0).toUpperCase();
+ }
+
+ // 4. Verified Trust Badge
+ const trustBadgeEl = document.getElementById('headerTrustBadge');
+ const trustBadgeTextEl = document.getElementById('headerTrustBadgeText');
+ if (trustBadgeEl) {
+ if (b.hideTrustBadge) {
+ trustBadgeEl.style.display = 'none';
+ } else {
+ trustBadgeEl.style.display = 'inline-flex';
+ if (trustBadgeTextEl) {
+ trustBadgeTextEl.textContent = b.trustBadgeText || `${appName} Verified Business`;
+ }
+ }
+ }
+
+ // 5. Header Sub-banner
+ const subbannerEl = document.getElementById('headerSubbanner');
+ const subbannerTextEl = document.getElementById('subbannerText');
+ if (subbannerEl) {
+ if (b.hideSubbanner) {
+ subbannerEl.style.display = 'none';
+ } else {
+ subbannerEl.style.display = 'flex';
+ if (subbannerTextEl) {
+ subbannerTextEl.textContent = b.subbannerText || b.sidebarNotice || `Verified & Protected by ${appName}`;
+ }
+ }
+ }
+
+ // 6. Time-Saving Conversion Banner
+ if (b.hideTimeSavingBanner) {
+ document.querySelectorAll('.ypg-time-saving-banner').forEach(el => el.style.setProperty('display', 'none', 'important'));
+ }
+
+
+ // Update PC Sidebar Details & Whitelabeling
+ const pcMerchant = document.getElementById('pcMerchantName');
+ if (pcMerchant) pcMerchant.textContent = data.merchantName || 'Merchant Name';
+ const pcPrice = document.getElementById('pcPriceSummaryAmount');
+ if (pcPrice && data.amount) pcPrice.textContent = `₹${data.amount}`;
+ const pcAppBrand = document.getElementById('pcAppNameBrand');
+ if (pcAppBrand) pcAppBrand.textContent = appName;
+
+ // PC Avatar Logo Whitelabel
+ const pcLogoImg = document.getElementById('pcMerchantAvatarImg');
+ const pcLetterSpan = document.getElementById('pcMerchantAvatarLetter');
+ if (b.logoUrl) {
+ if (pcLogoImg) {
+ pcLogoImg.src = b.logoUrl;
+ pcLogoImg.style.display = 'block';
+ }
+ if (pcLetterSpan) pcLetterSpan.style.display = 'none';
+ } else {
+ if (pcLogoImg) pcLogoImg.style.display = 'none';
+ if (pcLetterSpan) {
+ pcLetterSpan.style.display = 'block';
+ pcLetterSpan.textContent = (data.merchantName || 'M').charAt(0).toUpperCase();
+ }
+ }
+
+ // PC Trust Badge Whitelabel
+ const pcTrust = document.getElementById('pcTrustBadge');
+ const pcTrustText = document.getElementById('pcTrustBadgeText');
+ if (pcTrust) {
+ if (b.hideTrustBadge) {
+ pcTrust.style.display = 'none';
+ } else {
+ pcTrust.style.display = 'inline-flex';
+ if (pcTrustText) {
+ pcTrustText.textContent = b.trustBadgeText || `${appName} Trusted Business`;
+ }
+ }
+ }
+
+ // PC Social Proof / Subbanner Whitelabel
+ const pcSocialWrap = document.getElementById('pcSocialProofWrap');
+ const pcSocialProof = document.getElementById('pcSocialProofText');
+ if (pcSocialWrap) {
+ if (b.hideSubbanner) {
+ pcSocialWrap.style.display = 'none';
+ } else {
+ pcSocialWrap.style.display = 'flex';
+ if (pcSocialProof) {
+ pcSocialProof.textContent = b.subbannerText || b.sidebarNotice || `500+ orders last month from ${data.merchantName || 'Merchant'}`;
+ }
+ }
+ }
+
+ const pcCustomerText = document.getElementById('pcCustomerProfileText');
+ if (pcCustomerText) {
+ const c = data.customer || activeCustomer || (typeof getPreservedIdentity === 'function' ? getPreservedIdentity()?.customer : null);
+ if (c) {
+ const name = c.fullName || c.name || c.customerName || '';
+ const mobile = c.mobileNumber || c.phoneNumber || '';
+ const clean = mobile ? String(mobile).replace(/^\+91/, '').replace(/\D/g, '').slice(-10) : '';
+ if (name && clean) {
+ pcCustomerText.textContent = `Using as ${name} (+91 ${clean})`;
+ } else if (name) {
+ pcCustomerText.textContent = `Using as ${name}`;
+ } else if (clean) {
+ pcCustomerText.textContent = `Using as +91 ${clean}`;
+ } else if (c.email) {
+ pcCustomerText.textContent = `Using as ${c.email}`;
+ } else {
+ pcCustomerText.textContent = 'Using as Guest User';
+ }
+ } else {
+ pcCustomerText.textContent = 'Using as Guest User';
+ }
+ }
+ document.querySelectorAll('.cancelMerchantNameSpan').forEach(el => {
+ el.textContent = data.merchantName || 'Merchant';
+ });
+
+ // Update UPI Server Health Indicator (3-Tier Model: Green, Yellow, Red)
+ const serverIndicator = data.upiServerIndicator || { status: 'WORKING', tier: 'GREEN', label: 'Server Is Working', isWorking: true };
+ const healthPill = document.getElementById('upiServerHealthIndicator');
+ const healthLabel = document.getElementById('serverStatusLabel');
+ const downtimeAlert = document.getElementById('serverDowntimeAlert');
+ const downtimeHeading = document.querySelector('#serverDowntimeAlert .ypg-downtime-heading');
+ const downtimeMsg = document.getElementById('serverDowntimeMsg');
+ const btnCashAlt = document.getElementById('btnDowntimeCashAlt');
+
+ const tier = serverIndicator.tier || (serverIndicator.status === 'WORKING' ? 'GREEN' : (serverIndicator.status === 'BUSY' ? 'YELLOW' : 'RED'));
+
+ if (healthPill) {
+ if (tier === 'GREEN') {
+ healthPill.className = 'ypg-server-health-indicator is-working';
+ } else if (tier === 'YELLOW') {
+ healthPill.className = 'ypg-server-health-indicator is-busy';
+ } else {
+ healthPill.className = 'ypg-server-health-indicator is-issues';
+ }
+ }
+ if (healthLabel) {
+ healthLabel.textContent = serverIndicator.label || (tier === 'GREEN' ? 'Server Is Working' : (tier === 'YELLOW' ? 'UPI Server Busy' : 'Server Facing Issues'));
+ }
+ if (downtimeAlert) {
+ if (tier === 'GREEN') {
+ downtimeAlert.style.display = 'none';
+ } else if (tier === 'YELLOW') {
+ downtimeAlert.style.display = 'flex';
+ downtimeAlert.className = 'ypg-downtime-alert is-warning';
+ if (downtimeHeading) downtimeHeading.textContent = 'UPI Server Busy';
+ if (downtimeMsg) {
+ downtimeMsg.textContent = serverIndicator.warning || 'Your payment may go through extra processing seconds.';
+ }
+ if (btnCashAlt) {
+ btnCashAlt.style.display = (serverIndicator.recommendedAlternative === 'CASH') ? 'inline-block' : 'none';
+ }
+ } else {
+ // RED
+ downtimeAlert.style.display = 'flex';
+ downtimeAlert.className = 'ypg-downtime-alert';
+ if (downtimeHeading) downtimeHeading.textContent = 'UPI Server Facing Issues';
+ if (downtimeMsg) {
+ downtimeMsg.textContent = serverIndicator.warning || 'It is not recommended to pay by UPI right now as the server is facing downtime. We recommend paying by cash instead.';
+ }
+ if (btnCashAlt) {
+ btnCashAlt.style.display = (serverIndicator.recommendedAlternative === 'CASH') ? 'inline-block' : 'none';
+ }
+ }
+ }
+
+ // CRITICAL INVARIANT: In all 3 states (Green, Yellow, Red), ALWAYS display the dynamic UPI QR code
+ const upiCardContainer = document.querySelector('.ypg-qr-card-container');
+ if (upiCardContainer) upiCardContainer.style.display = 'flex';
+ const qrContainerBox = document.getElementById('qrImageContainer');
+ if (qrContainerBox && (!qrContainerBox.querySelector('svg') || qrContainerBox.textContent.includes('Loading QR'))) {
+ loadQrSvg();
+ }
+
+ // 7. Amount & Order IDs
+ if (data.amount) {
+ const formatted = `₹${data.amount}`;
+ const bAmt = document.getElementById('bottomAmountDisplay');
+ if (bAmt) bAmt.textContent = formatted;
+ const dAmt = document.getElementById('drawerTotalAmount');
+ if (dAmt) dAmt.textContent = formatted;
+ const sAmt = document.getElementById('successAmountVal');
+ if (sAmt) sAmt.textContent = formatted;
+ }
+ if (data.orderId || data.id) {
+ const oId = data.orderId || data.id;
+ const dOrder = document.getElementById('drawerOrderId');
+ if (dOrder) dOrder.textContent = oId;
+ const formattedOId = String(oId).startsWith('#') ? oId : `#${oId}`;
+ document.querySelectorAll('.cashierOrderIdDisplay').forEach(el => {
+ el.textContent = formattedOId;
+ });
+ }
+ // Note: Payee VPA / UPI ID is strictly hidden from customer view in all screens
+
+ // 8. In-Store Offline Counter Instructions
+ const cashNoticeEl = document.getElementById('cashCounterDeskNotice');
+ if (cashNoticeEl && b.counterInstructions) {
+ cashNoticeEl.textContent = b.counterInstructions;
+ }
+ const cardNoticeEl = document.getElementById('cardCounterDeskNotice');
+ if (cardNoticeEl && b.cardCounterInstructions) {
+ cardNoticeEl.textContent = b.cardCounterInstructions;
+ }
+
+ // 9. Dynamic Logo
+ const logoImg = document.getElementById('merchantAvatarImg');
+ const letterSpan = document.getElementById('merchantAvatarLetter');
+ if (b.logoUrl) {
+ if (logoImg) {
+ logoImg.src = b.logoUrl;
+ logoImg.style.display = 'block';
+ }
+ if (letterSpan) letterSpan.style.display = 'none';
+ } else {
+ if (logoImg) logoImg.style.display = 'none';
+ if (letterSpan) letterSpan.style.display = 'block';
+ }
+
+ // 10. Splash Screen Branding
+ const splashLogo = document.getElementById('splashBrandLogo');
+ const splashDefaultShield = document.getElementById('splashDefaultShield');
+ const splashSecuredText = document.getElementById('splashSecuredByText');
+ if (b.logoUrl && splashLogo) {
+ splashLogo.src = b.logoUrl;
+ splashLogo.style.display = 'block';
+ if (splashDefaultShield) splashDefaultShield.style.display = 'none';
+ }
+ if (splashSecuredText) {
+ splashSecuredText.textContent = b.hidePoweredBy ? (b.appName || '256-Bit Bank Encryption') : (b.appName || 'YouPI PG');
+ }
+
+ // 12. Legal Disclaimer Strip
+ const legalDisclaimerEl = document.getElementById('footerLegalDisclaimer');
+ if (legalDisclaimerEl) {
+ if (b.hidePoweredBy) {
+ legalDisclaimerEl.innerHTML = `Secured by 256-Bit Bank Encryption &bull; <span style="text-decoration:underline; cursor:pointer;" onclick="openHelpModal()">Privacy &amp; Terms</span>`;
+ } else {
+ legalDisclaimerEl.innerHTML = `Secured by ${appName} &amp; 256-Bit Bank Encryption &bull; <span style="text-decoration:underline; cursor:pointer;" onclick="openHelpModal()">Privacy Notice</span>`;
+ }
+ }
+
+ // 13. Success Screen Description
+ const successDesc = document.getElementById('successDescText');
+ if (successDesc) {
+ successDesc.textContent = b.hidePoweredBy
+ ? 'Your payment was automatically verified by banking observers.'
+ : `Your payment was automatically verified by ${appName} banking observers.`;
+ }
+
+ // 14. Help Modal Title & Legal Links
+ const helpTitle = document.getElementById('helpModalTitle');
+ if (helpTitle) helpTitle.textContent = `${appName} Help & Support`;
+ renderHelpLegalLinks(b);
+
+ // 1. Conflict detection: single active payment session invariant
+ const conflictModal = document.getElementById('activeConflictModal');
+ if (data.hasActiveSessionConflict) {
+ hideAllContent();
+ if (conflictModal) conflictModal.hidden = false;
+ if (data.conflictingSession && data.conflictingSession.id) {
+ activeConflictSessionId = data.conflictingSession.id;
+ const desc = document.getElementById('conflictModalDesc');
+ if (desc) {
+ desc.textContent = `You already have an active payment session (#${data.conflictingSession.orderId || data.conflictingSession.id.slice(0, 8)}) for ₹${data.conflictingSession.amount}. For security, only one active payment session is allowed per customer.`;
+ }
+ }
+ if (queuePollTimer) { clearInterval(queuePollTimer); queuePollTimer = null; }
+ if (countdownTimer) { clearInterval(countdownTimer); countdownTimer = null; }
+ return;
+ } else {
+ if (conflictModal) conflictModal.hidden = true;
+ }
+
+ // --- 2. TERMINAL PAYMENT STATES (TERMINAL & OVER) ---
+ if (['SUCCESS', 'CANCELLED', 'EXPIRED', 'FAILED'].includes(data.status)) {
+ if (queuePollTimer) { clearInterval(queuePollTimer); queuePollTimer = null; }
+ if (countdownTimer) { clearInterval(countdownTimer); countdownTimer = null; }
+ setGatewayAuthMode(false);
+
+ if (data.status === 'CANCELLED') {
+ emitTriggerHook('CANCEL_CONFIRMED');
+ closeProcessingModal();
+ closeCancelPaymentModal();
+ const bar = document.getElementById('paymentMethodSelectorBar');
+ if (bar) bar.style.display = 'none';
+
+ hideAllContent();
+ const failedView = document.getElementById('viewFailedContent');
+ if (failedView) failedView.hidden = false;
+ const footer = document.getElementById('ypgStickyFooter');
+ if (footer) footer.style.display = 'none';
+
+ const titleEl = document.getElementById('failedTitleText');
+ if (titleEl) titleEl.textContent = 'Payment Cancelled';
+ const descEl = document.getElementById('failedDescriptionText');
+ if (descEl) descEl.textContent = 'You cancelled this payment session. Click below to return to the store.';
+
+ const btnAction = document.getElementById('btnFailedAction');
+ if (btnAction) {
+ btnAction.textContent = 'Back to Store';
+ btnAction.onclick = () => {
+ const storeUrl = getStoreReturnUrl();
+ const isEmbedded = (window.parent && window.parent !== window) || location.search.includes('embedded=1');
+ if (isEmbedded) {
+ try {
- window.parent.postMessage({ event: 'youpi.modal_close', redirectUrl: storeUrl }, '*');
- window.parent.postMessage({ event: 'youpi.close', redirectUrl: storeUrl }, '*');
- window.parent.postMessage({ event: 'youpi.cancel', redirectUrl: storeUrl }, '*');
- window.parent.postMessage({ event: 'youpi.payment_cancelled', redirectUrl: storeUrl }, '*');
++ postToMerchant({ event: 'youpi.modal_close', redirectUrl: storeUrl });
++ postToMerchant({ event: 'youpi.close', redirectUrl: storeUrl });
++ postToMerchant({ event: 'youpi.cancel', redirectUrl: storeUrl });
++ postToMerchant({ event: 'youpi.payment_cancelled', redirectUrl: storeUrl });
+ } catch (e) {}
- } else if (storeUrl && storeUrl !== '#' && !storeUrl.startsWith('javascript:')) {
- window.location.href = storeUrl;
+ } else {
- window.history.back();
++ const safeStoreUrl = sanitizeRedirectUrl(storeUrl);
++ if (safeStoreUrl) {
++ window.location.href = safeStoreUrl;
++ } else {
++ window.history.back();
++ }
+ }
+ };
+ }
+ return;
+ }
+
+ if (data.status === 'FAILED' || data.status === 'EXPIRED') {
+ closeProcessingModal();
+ closeCancelPaymentModal();
+ const bar = document.getElementById('paymentMethodSelectorBar');
+ if (bar) bar.style.display = 'none';
+
+ hideAllContent();
+ const failedView = document.getElementById('viewFailedContent');
+ if (failedView) failedView.hidden = false;
+ const footer = document.getElementById('ypgStickyFooter');
+ if (footer) footer.style.display = 'none';
+ emitTriggerHook('PAYMENT_FAILED', { status: data.status });
+
+ const isExpired = data.status === 'EXPIRED';
+ const titleEl = document.getElementById('failedTitleText');
+ if (titleEl) titleEl.textContent = isExpired ? 'Payment Session Expired' : 'Payment Not Confirmed';
+ const descEl = document.getElementById('failedDescriptionText');
+ if (descEl) {
+ descEl.textContent = isExpired
+ ? 'The 5-minute active payment window elapsed before verification was completed. For security, please return to the store and initiate a new order.'
+ : 'No matching UPI bank confirmation was detected during the verification window.';
+ }
+ const btnAction = document.getElementById('btnFailedAction');
+ if (btnAction) {
+ btnAction.textContent = isExpired ? 'Start New Payment' : 'Try Again';
+ btnAction.onclick = () => retryNewPayment();
+ }
+ return;
+ }
+
+ if (data.status === 'SUCCESS') {
+ closeProcessingModal();
+ closeCancelPaymentModal();
+ const bar = document.getElementById('paymentMethodSelectorBar');
+ if (bar) bar.style.display = 'none';
+
+ hideAllContent();
+ const successView = document.getElementById('viewSuccessContent');
+ if (successView) successView.hidden = false;
+ const footer = document.getElementById('ypgStickyFooter');
+ if (footer) footer.style.display = 'none';
+ const amtEl = document.getElementById('successAmountVal');
+ if (amtEl) amtEl.textContent = `₹${data.amount}`;
+
+ const isCodOrder = data.method === 'COD' || data.verificationTag === 'COD_ORDER_PLACED';
+ const titleEl = document.getElementById('successTitleText');
+ const descEl = document.getElementById('successDescText');
+ const codApiBox = document.getElementById('successCodDevApiBox');
+ const codApiUrl = document.getElementById('successCodDevApiUrl');
+
+ if (isCodOrder) {
+ if (titleEl) titleEl.textContent = 'Order Placed (Cash on Delivery)!';
+ if (descEl) descEl.textContent = `Your order #${data.orderId} was confirmed. Please keep ₹${data.amount} in cash ready for the courier agent upon arrival.`;
+ if (codApiBox && codApiUrl) {
+ codApiBox.style.display = 'block';
+ codApiUrl.textContent = `${API_BASE}/api/v1/orders/${data.orderId}/cod`;
+ }
+ } else {
+ if (titleEl) titleEl.textContent = 'Payment Successful!';
+ if (descEl) descEl.textContent = 'Your payment was automatically verified by YouPI PG banking observers.';
+ if (codApiBox) codApiBox.style.display = 'none';
+ }
+
+ emitTriggerHook('PAYMENT_COMPLETED', { amount: data.amount, method: data.method, isCod: isCodOrder });
+
+ try {
- window.parent.postMessage({
++ postToMerchant({
+ event: 'youpi.payment_success',
+ data: data
- }, '*');
++ });
+ } catch (e) { }
+
+ const btnReturn = document.getElementById('btnReturnStore') || document.getElementById('btnReturnToStore');
+ if (btnReturn && data.returnUrl) {
+ btnReturn.hidden = false;
+ btnReturn.href = data.returnUrl;
+ if (window.parent === window) {
- setTimeout(() => { window.location.href = data.returnUrl; }, 2000);
++ const safeReturn = sanitizeRedirectUrl(data.returnUrl);
++ setTimeout(() => { if (safeReturn) window.location.href = safeReturn; }, 2000);
+ }
+ } else if (btnReturn) {
+ btnReturn.hidden = true;
+ }
+ return;
+ }
+ }
+
+ // Sync payment methods & modality layout immediately to match merchant settings
+ renderMethodSelectorBar(data);
+
+ // Auto-bind customer to payment session helper
+ if (typeof autoBindCustomerToSession !== 'function') {
+ window.autoBindCustomerToSession = async function(customerId, token) {
+ if (window.isAutoBindingSession || !PAYMENT_ID || !token || !customerId) return;
+ window.isAutoBindingSession = true;
+ try {
+ const res = await fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/bind-customer`, {
+ method: 'POST',
+ headers: getApiHeaders({
+ 'Content-Type': 'application/json',
+ 'Authorization': `Bearer ${token}`
+ }),
+ body: JSON.stringify({ token, customerId })
+ });
+ if (res.ok) {
+ const boundData = await res.json().catch(() => null);
+ if (boundData && boundData.status && boundData.status !== 'INITIATED') {
+ loadState();
+ }
+ } else if (res.status === 401) {
+ clearPreservedIdentity();
+ activeCustomer = null;
+ activeToken = null;
+ setGatewayAuthMode(true);
+ goToAuthStep('mobile');
+ renderSavedAccountsUI();
+ }
+ } catch (e) {
+ } finally {
+ window.isAutoBindingSession = false;
+ }
+ };
+ }
+
+ // 3. Pre-Gateway Customer Authentication Gate (Preserves identity up to 1 week)
+ if (data.customer) {
+ activeCustomer = data.customer;
+ } else if (!activeCustomer) {
+ const preserved = getPreservedIdentity();
+ if (preserved && preserved.customer) {
+ activeCustomer = preserved.customer;
+ activeToken = preserved.token || activeToken;
+ }
+ }
+ const hasCustomer = Boolean(data.customer || activeCustomer);
+
+ if (!hasCustomer) {
+ setGatewayAuthMode(true);
+ const stepMob = document.getElementById('authStepMobile');
+ const isAlreadyOnMobile = stepMob && !stepMob.hidden;
+ if (!currentAuthStep || (currentAuthStep === 'mobile' && !isAlreadyOnMobile)) {
+ goToAuthStep('mobile');
+ }
+ renderSavedAccountsUI();
+ return;
+ }
+
+ // Authenticated Customer
+ if (activeCustomer) {
+ updateCustomerDisplay(activeCustomer);
+ // Automatically bind customer to the payment session if session is INITIATED or unbound
+ if (activeToken && PAYMENT_ID && (!data.customer || data.status === 'INITIATED')) {
+ window.autoBindCustomerToSession(activeCustomer.id, activeToken);
+ }
+ }
+ setGatewayAuthMode(false);
+
+ // Render psychological time-saving conversion banner
+ renderTimeSavingBanners(data);
+
+ // 3. Status Handling: BLOCKED, QUEUED, ACTIVE, PROCESSING, SUCCESS, FAILED, EXPIRED
+ if (data.status === 'BLOCKED' || data.isBanned || data.fraudDetected) {
+ renderBlockedState(data);
+ return;
+ }
+
+ if (data.status === 'QUEUED') {
+ const bar = document.getElementById('paymentMethodSelectorBar');
+ if (bar) bar.style.display = 'none';
+
+ hideAllContent();
+ document.getElementById('viewQueuedContent').hidden = false;
+ document.getElementById('ypgStickyFooter').style.display = 'none';
+ document.getElementById('queueNumberDisplay').textContent = `#${data.queuePosition || 1}`;
+ runCountdown(data.timeRemainingSeconds, sec => {
+ document.getElementById('queueTimerText').textContent = formatTime(sec);
+ }, () => loadState());
+
+ if (!queuePollTimer) {
+ queuePollTimer = setInterval(() => { loadState(); }, 1200);
+ }
+ return;
+ }
+
+ if (queuePollTimer) {
+ clearInterval(queuePollTimer);
+ queuePollTimer = null;
+ }
+
+ if (data.status === 'ACTIVE' || (data.status === 'INITIATED' && (data.timeRemainingSeconds > 0 || data.expiresAt))) {
+ renderMethodSelectorBar(data);
+
+ hideAllContent();
+ if (currentSelectedMethod === 'COD') {
+ const vCod = document.getElementById('viewCodContent');
+ if (vCod) vCod.hidden = false;
+ document.getElementById('ypgStickyFooter').style.display = 'none';
+
+ runCountdown(data.timeRemainingSeconds || 300, sec => {
+ const el = document.getElementById('codTimerText');
+ if (el) el.textContent = formatTime(sec);
+ }, () => {
+ const el = document.getElementById('codTimerText');
+ if (el) el.textContent = 'Expired';
+ fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/expire`, { method: 'POST' }).catch(() => { });
+ loadState();
+ }, data.expiresAt);
+ } else if (currentSelectedMethod === 'CASH_COUNTER') {
+ const vCash = document.getElementById('viewCashContent');
+ if (vCash) vCash.hidden = false;
+ document.getElementById('ypgStickyFooter').style.display = 'none';
+ } else if (currentSelectedMethod === 'CARD_COUNTER') {
+ const vCard = document.getElementById('viewCardContent');
+ if (vCard) vCard.hidden = false;
+ document.getElementById('ypgStickyFooter').style.display = 'none';
+ } else {
+ // Default: UPI QR (CRITICAL INVARIANT: ALWAYS SHOWS QR AND RUNS 5:00 TIMER REGARDLESS OF GREEN/YELLOW/RED)
+ document.getElementById('viewUpiContent').hidden = false;
+ document.getElementById('ypgStickyFooter').style.display = 'flex';
+ loadQrSvg();
+
+ const btnContinue = document.getElementById('btnContinuePayment');
+
+ const remainingSec = (data.timeRemainingSeconds !== undefined && data.timeRemainingSeconds !== null) ? Number(data.timeRemainingSeconds) : 300;
+ const targetExpiry = data.expiresAt || new Date(Date.now() + remainingSec * 1000).toISOString();
+
+ if (remainingSec <= 0 && data.status === 'ACTIVE') {
+ const tText = document.getElementById('activeTimerText');
+ if (tText) tText.textContent = 'Expired';
+ if (btnContinue) btnContinue.style.display = 'none';
+ return;
+ }
+
+ // Initialize button hidden until 4:00 has elapsed
+ if (btnContinue && remainingSec > 240) {
+ btnContinue.style.display = 'none';
+ } else if (btnContinue) {
+ btnContinue.style.display = 'block';
+ }
+
+ runCountdown(remainingSec, sec => {
+ const tText = document.getElementById('activeTimerText');
+ if (tText) tText.textContent = formatTime(sec);
+
+ // "Payment Done — Check My Payment" button appears after 4:00
+ if (sec <= 240 && btnContinue) {
+ btnContinue.style.display = 'block';
+ } else if (btnContinue) {
+ btnContinue.style.display = 'none';
+ }
+ }, () => {
+ const tText = document.getElementById('activeTimerText');
+ if (tText) tText.textContent = 'Expired';
+ if (btnContinue) btnContinue.style.display = 'none';
+ closeProcessingModal();
+ fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/expire`, { method: 'POST' }).catch(() => { });
+ loadState();
+ }, targetExpiry);
+ }
+ return;
+ }
+
+ if (data.status === 'PROCESSING') {
+ const bar = document.getElementById('paymentMethodSelectorBar');
+ if (bar) bar.style.display = 'none';
+
+ hideAllContent();
+ document.getElementById('viewProcessingContent').hidden = false;
+ document.getElementById('ypgStickyFooter').style.display = 'none';
+ runCountdown(data.timeRemainingSeconds, sec => {
+ document.getElementById('procTimerText').textContent = `${sec}s`;
+ }, () => loadState(), data.expiresAt);
+ return;
+ }
+
+
+ }
+
+ // --- PSYCHOLOGICAL TIME-SAVING CONVERSION BANNER ---
+ // Invariant: ONLY display when the customer is actively waiting in the UPI queue!
+ function renderTimeSavingBanners(data) {
+ const qBanner = document.getElementById('queueTimeSavingBanner');
+ const uBanner = document.getElementById('upiTimeSavingBanner');
+ if (uBanner) uBanner.style.display = 'none'; // NEVER show on active UPI session
+
+ const isQueued = (data.status === 'QUEUED') || (Number(data.queuePosition || 0) > 0);
+ if (!isQueued || !qBanner) {
+ if (qBanner) qBanner.style.display = 'none';
+ return;
+ }
+
+ const enabled = data.enabledMethods || ['UPI'];
+ const sellingMode = data.sellingMode || 'OFFLINE';
+
+ let altMethodCode = null;
+ let altMethodName = null;
+
+ if (sellingMode === 'ONLINE') {
+ if (enabled.includes('COD')) {
+ altMethodCode = 'COD';
+ altMethodName = 'Cash on Delivery';
+ } else if (enabled.includes('CARD')) {
+ altMethodCode = 'CARD';
+ altMethodName = 'Credit / Debit Card';
+ } else if (enabled.includes('NETBANKING')) {
+ altMethodCode = 'NETBANKING';
+ altMethodName = 'Net Banking';
+ } else if (enabled.includes('CARD_COUNTER')) {
+ altMethodCode = 'CARD_COUNTER';
+ altMethodName = 'Card Payment';
+ }
+ } else {
+ // Offline selling mode (retail till / counter)
+ if (enabled.includes('CASH_COUNTER')) {
+ altMethodCode = 'CASH_COUNTER';
+ altMethodName = 'Cash at Till';
+ } else if (enabled.includes('CARD_COUNTER')) {
+ altMethodCode = 'CARD_COUNTER';
+ altMethodName = 'Card at Counter';
+ }
+ }
+
+ // CRITICAL REQUIREMENT: If both cash and card are disabled by merchant, psychological conversion MUST NOT BE SHOWN!
+ if (!altMethodCode || (!enabled.includes('COD') && !enabled.includes('CASH_COUNTER') && !enabled.includes('CARD') && !enabled.includes('CARD_COUNTER'))) {
+ qBanner.style.display = 'none';
+ return;
+ }
+
+ const timeSec = data.timeRemainingSeconds || (data.queuePosition ? data.queuePosition * 30 : 30);
+ const initialTimerStr = formatTime(timeSec);
+
+ const actionText = sellingMode === 'ONLINE'
+ ? `Skip the UPI wait! Pay with <strong>${altMethodName}</strong> instantly without waiting for a UPI slot.`
+ : `Don't wait in the UPI line! Pay with <strong>${altMethodName}</strong> right now to complete your purchase instantly.`;
+
+ qBanner.innerHTML = `
+ <div class="ypg-queue-conversion-card">
+ <div class="ypg-queue-conversion-header">
+ <div class="ypg-queue-conversion-badge"><svg width="12" height="12" viewBox="0 0 24 24" fill="currentColor" style="display:inline-block; vertical-align:middle; margin-right:3px;"><path d="M12 2l2.4 7.2h7.6l-6 4.8 2.4 7.2-6.4-4.8-6.4 4.8 2.4-7.2-6-4.8h7.6z"/></svg>Instant Alternative</div>
+ <div class="ypg-queue-conversion-pill">Save <span class="ypg-banner-running-timer">${initialTimerStr}</span></div>
+ </div>
+ <div class="ypg-queue-conversion-text">
+ ${actionText}
+ </div>
+ <button type="button" class="ypg-btn-queue-switch" onclick="selectPaymentMethod('${altMethodCode}')">
+ <span>Pay with ${altMethodName}</span>
+ <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><polyline points="9 18 15 12 9 6"/></svg>
+ </button>
+ </div>
+ `;
+ qBanner.style.display = 'block';
+ }
+
+ // --- PURE QR SVG LOADER (ALWAYS LOADS & SHOWS QR CODE REGARDLESS OF SERVER STATUS) ---
+ async function loadQrSvg() {
+ const container = document.getElementById('qrImageContainer');
+ if (!container) return;
+ try {
+ const res = await fetch(`/checkout/${PAYMENT_ID}/qr.svg`);
+ if (res.ok) {
+ const svg = await res.text();
+ if (svg && svg.includes('<svg')) {
+ container.innerHTML = svg;
+ return;
+ }
+ }
+ } catch (e) {
+ console.warn('loadQrSvg error:', e);
+ }
+ // Automatic retry to guarantee QR code is loaded
+ setTimeout(async () => {
+ try {
+ const res = await fetch(`/checkout/${PAYMENT_ID}/qr.svg`);
+ if (res.ok) {
+ const svg = await res.text();
+ if (svg && svg.includes('<svg')) {
+ container.innerHTML = svg;
+ }
+ }
+ } catch (e) {}
+ }, 400);
+ }
+
+ function copyVpaId() {
+ const vpa = (state?.payeeVpa || document.getElementById('displayVpaVal')?.textContent || '').trim();
+ if (!vpa) return;
+
+ const btn = document.getElementById('btnCopyVpa');
+ const text = document.getElementById('copyVpaText');
+ if (btn && text) {
+ navigator.clipboard.writeText(vpa).then(() => {
+ text.textContent = 'Copied!';
+ btn.classList.add('copied');
+ setTimeout(() => {
+ text.textContent = 'Copy';
+ btn.classList.remove('copied');
+ }, 2000);
+ }).catch(() => {
+ text.textContent = 'Copied!';
+ setTimeout(() => { text.textContent = 'Copy'; }, 2000);
+ });
+ }
+ }
+
+ // --- RAZORPAY AUTHENTIC "PROCESSING YOUR PAYMENT" MODAL SHEET ---
+ function openProcessingModal(title = 'Processing your payment', subtitle = 'This will only take a few seconds.') {
+ const modal = document.getElementById('rzpProcessingModal');
+ const tEl = document.getElementById('rzpProcessingTitle');
+ const sEl = document.getElementById('rzpProcessingSubtitle');
+ if (tEl) tEl.textContent = title;
+ if (sEl) sEl.textContent = subtitle;
+ if (modal) {
+ modal.hidden = false;
+ requestAnimationFrame(() => modal.classList.add('active'));
+ }
+ }
+
+ function closeProcessingModal() {
+ const modal = document.getElementById('rzpProcessingModal');
+ if (modal) {
+ modal.classList.remove('active');
+ setTimeout(() => { modal.hidden = true; }, 300);
+ }
+ }
+
+ // --- RAZORPAY AUTHENTIC "CANCEL PAYMENT" CONFIRMATION MODAL SHEET ---
+ function openCancelPaymentModal() {
+ emitTriggerHook('CANCEL_CLICKED');
+ document.body.style.overflow = 'hidden';
+ const yBody = document.querySelector('.ypg-body');
+ if (yBody) yBody.style.overflow = 'hidden';
+
+ const modal = document.getElementById('rzpCancelModal');
+ const promptView = document.getElementById('rzpCancelPromptView');
+ const progressView = document.getElementById('rzpCancellingProgressView');
+ if (promptView) promptView.style.display = 'flex';
+ if (progressView) progressView.style.display = 'none';
+ if (modal) {
+ modal.hidden = false;
+ requestAnimationFrame(() => modal.classList.add('active'));
+ }
+ }
+
+ function closeCancelPaymentModal() {
+ document.body.style.overflow = '';
+ const yBody = document.querySelector('.ypg-body');
+ if (yBody) yBody.style.overflow = '';
+
+ const modal = document.getElementById('rzpCancelModal');
+ if (modal) {
+ modal.classList.remove('active');
+ setTimeout(() => { modal.hidden = true; }, 300);
+ }
+ }
+
+ async function confirmCancelPayment() {
+ emitTriggerHook('CANCEL_CONFIRMED');
+ if (countdownTimer) { clearInterval(countdownTimer); countdownTimer = null; }
+ if (queuePollTimer) { clearInterval(queuePollTimer); queuePollTimer = null; }
+
+ const promptView = document.getElementById('rzpCancelPromptView');
+ const progressView = document.getElementById('rzpCancellingProgressView');
+ if (promptView) promptView.style.display = 'none';
+ if (progressView) progressView.style.display = 'flex';
+
+ try {
+ await fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/cancel`, {
+ method: 'POST',
+ headers: getApiHeaders()
+ });
+ } catch (e) { }
+
+ // Smooth realistic delay matching Razorpay transition
+ setTimeout(() => {
+ document.body.style.overflow = '';
+ const yBody = document.querySelector('.ypg-body');
+ if (yBody) yBody.style.overflow = '';
+
+ closeCancelPaymentModal();
+ closeProcessingModal();
+
+ const storeUrl = getStoreReturnUrl();
+ const isEmbedded = (window.parent && window.parent !== window) || location.search.includes('embedded=1');
+
+ if (isEmbedded) {
+ try {
- window.parent.postMessage({ event: 'youpi.modal_close', redirectUrl: storeUrl }, '*');
- window.parent.postMessage({ event: 'youpi.close', redirectUrl: storeUrl }, '*');
- window.parent.postMessage({ event: 'youpi.cancel', redirectUrl: storeUrl }, '*');
- window.parent.postMessage({ event: 'youpi.payment_cancelled', redirectUrl: storeUrl }, '*');
++ postToMerchant({ event: 'youpi.modal_close', redirectUrl: storeUrl });
++ postToMerchant({ event: 'youpi.close', redirectUrl: storeUrl });
++ postToMerchant({ event: 'youpi.cancel', redirectUrl: storeUrl });
++ postToMerchant({ event: 'youpi.payment_cancelled', redirectUrl: storeUrl });
+ } catch (e) { }
+ }
+
+ loadState();
+ }, 500);
+ }
+
+ // Direct invocation from header back button, close button, or inline links
+ function cancelPayment() {
+ openCancelPaymentModal();
+ }
+
+ // Listen for cancel requests from parent iframe cross button
+ window.addEventListener('message', (e) => {
+ if (e.data && e.data.event === 'youpi.request_cancel') {
+ openCancelPaymentModal();
+ }
+ });
+
+ // --- MANUAL PAYMENT DONE VERIFICATION ---
+ async function submitPaymentDone() {
+ openProcessingModal('Processing your payment', 'This will only take a few seconds.');
+ emitTriggerHook('PAYMENT_DONE_CLICKED');
+
+ try {
+ const res = await fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/payment-done`, {
+ method: 'POST'
+ });
+ if (res.ok) {
+ loadState();
+ }
+ } catch (e) { }
+ }
+
+ // --- SINGLE ACTIVE PAYMENT SESSION CONFLICT RESOLUTION ---
+ let activeConflictSessionId = null;
+
+ async function executeRetryNewPayment() {
+ try {
+ const btn = document.getElementById('btnEndSessionAndStartNew');
+ if (btn) {
+ btn.disabled = true;
+ btn.textContent = 'Activating new payment...';
+ }
+ const headers = { 'Content-Type': 'application/json' };
+ if (typeof DeviceIdentity !== 'undefined') {
+ if (DeviceIdentity.currentDeviceId) headers['X-Device-Id'] = DeviceIdentity.currentDeviceId;
+ if (DeviceIdentity.currentDeviceToken) headers['X-Device-Token'] = DeviceIdentity.currentDeviceToken;
+ }
+ const token = activeToken || localStorage.getItem('ypg_customer_token');
+ if (token) {
+ headers['Authorization'] = `Bearer ${token}`;
+ headers['x-customer-token'] = token;
+ }
+ const res = await fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/retry-new-payment`, {
+ method: 'POST',
+ headers
+ });
+ if (res.ok) {
+ const conflictModal = document.getElementById('activeConflictModal');
+ if (conflictModal) conflictModal.hidden = true;
+ await loadState();
+ } else {
+ const err = await res.json().catch(() => ({}));
+ alert(err.error || 'Failed to retry payment');
+ if (btn) {
+ btn.disabled = false;
+ btn.textContent = 'END SESSION & START NEW PAYMENT';
+ }
+ }
+ } catch (e) {
+ alert('Network error while retrying payment');
+ const btn = document.getElementById('btnEndSessionAndStartNew');
+ if (btn) {
+ btn.disabled = false;
+ btn.textContent = 'END SESSION & START NEW PAYMENT';
+ }
+ }
+ }
+
+ function continueExistingActivePayment() {
+ if (activeConflictSessionId) {
+ window.location.href = `/checkout/${activeConflictSessionId}`;
+ } else {
+ const conflictModal = document.getElementById('activeConflictModal');
+ if (conflictModal) conflictModal.hidden = true;
+ loadState();
+ }
+ }
+
+
+
+ function renderBlockedState(data) {
+ closeProcessingModal();
+ closeCancelPaymentModal();
+ if (countdownTimer) { clearInterval(countdownTimer); countdownTimer = null; }
+ if (queuePollTimer) { clearInterval(queuePollTimer); queuePollTimer = null; }
+
+ const bar = document.getElementById('paymentMethodSelectorBar');
+ if (bar) bar.style.display = 'none';
+ const footer = document.getElementById('ypgStickyFooter');
+ if (footer) footer.style.display = 'none';
+
+ hideAllContent();
+ const vBlocked = document.getElementById('viewBlockedContent');
+ if (vBlocked) vBlocked.hidden = false;
+
+ // Expand to full width on PC desktop split view
+ const pcMethodsList = document.getElementById('pcMethodsList');
+ if (pcMethodsList) pcMethodsList.style.setProperty('display', 'none', 'important');
+ const pcActiveView = document.querySelector('.ypg-pc-active-view');
+ if (pcActiveView) pcActiveView.style.width = '100%';
+ const pcTopbarTitle = document.querySelector('.ypg-pc-topbar-title');
+ if (pcTopbarTitle) pcTopbarTitle.textContent = 'Security Incident Investigation';
+
+ const orderRef = data.orderId || state?.orderId || 'N/A';
+ const orderEl = document.getElementById('blockedOrderRefVal');
+ if (orderEl) orderEl.textContent = `#${orderRef}`;
+
+ const sessEl = document.getElementById('blockedSessionIdVal');
+ if (sessEl) sessEl.textContent = data.id || PAYMENT_ID || 'N/A';
+
+ const timeEl = document.getElementById('blockedTimestampVal');
+ if (timeEl) timeEl.textContent = new Date().toLocaleTimeString();
+
+ if (data.blockedNotice?.title) {
+ const titleEl = document.getElementById('blockedTitleText');
+ if (titleEl) titleEl.textContent = data.blockedNotice.title;
+ }
+ if (data.blockedNotice?.subtitle) {
+ const subEl = document.getElementById('blockedSubtitleText');
+ if (subEl) subEl.textContent = data.blockedNotice.subtitle;
+ }
+ if (data.blockedNotice?.message) {
+ const msgEl = document.getElementById('blockedNoticeText');
+ if (msgEl) msgEl.textContent = data.blockedNotice.message;
+ }
+
+ emitTriggerHook('PAYMENT_BLOCKED', {
+ reason: data.reason || data.blockedNotice?.reason || 'FRAUDULENT_PAYMENT_INJECTION',
+ orderId: orderRef
+ });
+
+ try {
- window.parent.postMessage({
++ postToMerchant({
+ event: 'youpi.payment_blocked',
+ data: {
+ status: 'BLOCKED',
+ isBanned: true,
+ orderId: orderRef,
+ paymentId: data.id || PAYMENT_ID
+ }
- }, '*');
++ });
+ } catch (e) {}
+ }
+
+ function handleBlockedExit() {
+ const failRedirectUrl = getFailedRedirectUrl();
+ if (window.parent && window.parent !== window) {
+ try {
- window.parent.postMessage({
++ postToMerchant({
+ event: 'youpi.payment_blocked_exit',
+ data: { status: 'BLOCKED', isBanned: true, orderId: state?.orderId }
- }, '*');
++ });
+ } catch (e) {}
+ }
- window.location.href = failRedirectUrl;
++ const safeFailUrl = sanitizeRedirectUrl(failRedirectUrl);
++ if (safeFailUrl) window.location.href = safeFailUrl;
+ }
+
+ let lastDuplicateSessionPayload = null;
+ function renderDuplicateSession(data) {
+ lastDuplicateSessionPayload = data;
+ if (countdownTimer) { clearInterval(countdownTimer); countdownTimer = null; }
+ if (queuePollTimer) { clearInterval(queuePollTimer); queuePollTimer = null; }
+ if (sse) { try { sse.close(); } catch(e){} sse = null; }
+
+ const bar = document.getElementById('paymentMethodSelectorBar');
+ if (bar) bar.style.display = 'none';
+ const footer = document.getElementById('ypgStickyFooter');
+ if (footer) footer.style.display = 'none';
+
+ hideAllContent();
+ const vDup = document.getElementById('viewDuplicateSessionContent');
+ if (vDup) vDup.hidden = false;
+
+ // Expand to full width on PC desktop split view
+ const pcMethodsList = document.getElementById('pcMethodsList');
+ if (pcMethodsList) pcMethodsList.style.setProperty('display', 'none', 'important');
+ const pcActiveView = document.querySelector('.ypg-pc-active-view');
+ if (pcActiveView) pcActiveView.style.width = '100%';
+ const pcTopbarTitle = document.querySelector('.ypg-pc-topbar-title');
+ if (pcTopbarTitle) pcTopbarTitle.textContent = 'Device Security Verification';
+
+ emitTriggerHook('DUPLICATE_SESSION_DETECTED', {
+ sessionId: data.session_id || PAYMENT_ID,
+ action: 'END_SESSION_AND_CREATE_NEW'
+ });
+ }
+
+ async function handleEndAndStartNewPayment() {
+ const btn = document.getElementById('btnEndAndRestartPayment');
+ if (btn) {
+ btn.disabled = true;
+ btn.innerHTML = '<span class="ypg-spinner" style="display:inline-block;width:15px;height:15px;border:2px solid #fff;border-top-color:transparent;border-radius:50%;animation:rzpRotate 0.8s linear infinite;margin-right:8px;vertical-align:middle;"></span> CREATING NEW PAYMENT...';
+ }
+
+ try {
+ await DeviceIdentity.getOrRegister();
+ const headers = { 'Content-Type': 'application/json' };
+ if (DeviceIdentity.currentDeviceId) {
+ headers['X-Device-Id'] = DeviceIdentity.currentDeviceId;
+ }
+ if (DeviceIdentity.currentDeviceToken) {
+ headers['X-Device-Token'] = DeviceIdentity.currentDeviceToken;
+ }
+
+ const conflictingSessionId = lastDuplicateSessionPayload?.conflicting_session_id || activeConflictSessionId || null;
+
+ const res = await fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/end-and-restart-payment`, {
+ method: 'POST',
+ headers,
+ body: JSON.stringify({
+ deviceId: DeviceIdentity.currentDeviceId,
+ conflictingSessionId
+ })
+ });
+
+ const data = await res.json();
+ if (data.success && (data.checkoutUrl || data.newSessionId)) {
- const targetUrl = data.checkoutUrl || `/checkout/${data.newSessionId}`;
++ const targetUrl = sanitizeRedirectUrl(data.checkoutUrl) || `/checkout/${data.newSessionId}`;
+ window.location.href = targetUrl;
+ } else {
+ alert(data.error || 'Failed to start new payment session. Please try again.');
+ if (btn) {
+ btn.disabled = false;
+ btn.innerHTML = '<span>END SESSION &amp; START NEW PAYMENT</span>';
+ }
+ }
+ } catch (err) {
+ alert('Network error while starting new payment session.');
+ if (btn) {
+ btn.disabled = false;
+ btn.innerHTML = '<span>END SESSION &amp; START NEW PAYMENT</span>';
+ }
+ }
+ }
+
+ // --- SSE REALTIME STREAM (AUTOMATIC OBSERVER & CASHIER INTEGRATION) ---
+ function connectSse() {
+ if (sse) {
+ try { sse.close(); } catch (e) { }
+ sse = null;
+ }
+
+ try {
+ const devQuery = DeviceIdentity.currentDeviceId ? `?deviceId=${encodeURIComponent(DeviceIdentity.currentDeviceId)}` : '';
+ const streamUrl = `${API_BASE}/api/v1/checkout/${PAYMENT_ID}/stream${devQuery}`;
+ sse = new EventSource(streamUrl);
+
+ const handleRealtimePayload = (event) => {
+ try {
+ const data = JSON.parse(event.data);
+ if (data && (data.error === 'DUPLICATE_SESSION_DETECTED' || data.type === 'DUPLICATE_SESSION_DETECTED')) {
+ renderDuplicateSession(data);
+ } else if (data && (data.status === 'BLOCKED' || data.isBanned || data.fraudDetected || data.type === 'FRAUD_BLOCKED' || data.event === 'account_blocked')) {
+ renderBlockedState(data);
+ } else if (data && data.type === 'STATE_CHANGE' && data.payload) {
+ renderState(data.payload);
+ } else if (data && data.status) {
+ if (data.status === 'BLOCKED' || data.isBanned) {
+ renderBlockedState(data);
+ } else {
+ loadState();
+ }
+ } else {
+ loadState();
+ }
+ } catch (e) {
+ loadState();
+ }
+ };
+
+ sse.onmessage = handleRealtimePayload;
+ sse.addEventListener('status_change', handleRealtimePayload);
+ sse.addEventListener('state_change', handleRealtimePayload);
+ sse.addEventListener('account_blocked', handleRealtimePayload);
+ sse.addEventListener('fraud_detected', handleRealtimePayload);
+ sse.addEventListener('counter_payment_confirmed', handleRealtimePayload);
+ sse.addEventListener('counter_payment_declined', handleRealtimePayload);
+ sse.addEventListener('payment_success', handleRealtimePayload);
+ sse.addEventListener('queue_cleared', handleRealtimePayload);
+ sse.addEventListener('session_expired', handleRealtimePayload);
+
+ sse.onerror = () => {
+ if (sse) {
+ try { sse.close(); } catch (e) { }
+ sse = null;
+ }
+ setTimeout(connectSse, 3000);
+ };
+ } catch (e) {
+ setTimeout(connectSse, 3000);
+ }
+ }
+
+ // Continuous 2-second fallback polling: guarantees instant response when cashier confirms/declines or queues clear
+ setInterval(() => {
+ if (state && (state.status === 'SUCCESS' || state.status === 'FAILED' || state.status === 'EXPIRED' || state.status === 'BLOCKED' || state.isBanned)) {
+ return;
+ }
+ loadState();
+ }, 2000);
+
+ // --- SUPPORT / HELP MODAL ---
+ function openHelpModal() {
+ document.getElementById('helpModal').hidden = false;
+ const popoverMobile = document.getElementById('accountMenuPopover');
+ if (popoverMobile) { popoverMobile.setAttribute('hidden', ''); popoverMobile.hidden = true; popoverMobile.style.display = 'none'; }
+ const popoverPc = document.getElementById('accountMenuPopoverPc');
+ if (popoverPc) { popoverPc.setAttribute('hidden', ''); popoverPc.hidden = true; popoverPc.style.display = 'none'; }
+
+ const branding = state?.branding || {};
+ renderSupportInfo({
+ supportEmails: branding.supportEmails || (branding.supportEmail ? [branding.supportEmail] : []),
+ supportMobiles: branding.supportMobiles || (branding.supportMobile ? [branding.supportMobile] : [])
+ });
+ }
+
+ function renderSupportInfo(info) {
+ const emailBox = document.getElementById('helpModalEmailsContainer');
+ const phoneBox = document.getElementById('helpModalMobilesContainer');
+ const caseBox = document.getElementById('helpModalCaseInfoText');
+
+ if (emailBox && info.supportEmails) {
+ emailBox.innerHTML = `<strong>Email:</strong> ${info.supportEmails.join(', ')}`;
+ }
+ if (phoneBox && info.supportMobiles) {
+ phoneBox.innerHTML = `<strong>Phone:</strong> ${info.supportMobiles.join(', ')}`;
+ }
+ if (caseBox) {
+ caseBox.textContent = `PAYMENT_ID: ${PAYMENT_ID}\nORDER_ID: ${state?.orderId || 'N/A'}\nAMOUNT: ₹${state?.amount || 'N/A'}\nSTATUS: ${state?.status || 'N/A'}`;
+ }
+ }
+
+ function closeHelpModal() {
+ document.getElementById('helpModal').hidden = true;
+ }
+
+ function copyCaseInfo() {
+ const text = document.getElementById('helpModalCaseInfoText').textContent;
+ navigator.clipboard.writeText(text).then(() => {
+ document.getElementById('copyCaseText').textContent = 'Copied to Clipboard!';
+ setTimeout(() => {
+ document.getElementById('copyCaseText').textContent = 'Copy Payment Case Details';
+ }, 2000);
+ });
+ }
+
+ // WhatsApp OTP Input Keyboard Listeners
+ const mobIn = document.getElementById('authMobileInput');
+ if (mobIn) {
+ mobIn.addEventListener('input', (e) => {
+ const clean = e.target.value.replace(/\D/g, '').slice(0, 10);
+ if (e.target.value !== clean) {
+ e.target.value = clean;
+ }
+ currentAuthMobile = clean;
+ });
+ mobIn.addEventListener('keydown', (e) => {
+ if (e.key === 'Enter') {
+ e.preventDefault();
+ handleAuthMobileSubmit();
+ }
+ });
+ }
+
+ const nameIn = document.getElementById('authSignupName');
+ const emailIn = document.getElementById('authSignupEmail');
+ if (nameIn) {
+ nameIn.addEventListener('input', (e) => {
+ currentAuthName = e.target.value;
+ });
+ nameIn.addEventListener('keydown', (e) => {
+ if (e.key === 'Enter') {
+ e.preventDefault();
+ emailIn?.focus();
+ }
+ });
+ }
+ if (emailIn) {
+ emailIn.addEventListener('input', (e) => {
+ currentAuthEmail = e.target.value;
+ });
+ emailIn.addEventListener('keydown', (e) => {
+ if (e.key === 'Enter') {
+ e.preventDefault();
+ handleAuthSignupSubmit();
+ }
+ });
+ }
+
+ const otpIn = document.getElementById('authOtpInput');
+ if (otpIn) {
+ otpIn.addEventListener('input', (e) => {
+ const val = e.target.value.replace(/\D/g, '');
+ e.target.value = val;
+ if (val.length === 6) {
+ handleAuthVerifyOtpSubmit();
+ }
+ });
+ otpIn.addEventListener('keydown', (e) => {
+ if (e.key === 'Enter') {
+ e.preventDefault();
+ handleAuthVerifyOtpSubmit();
+ }
+ });
+ }
+
+ // Boot checkout - restore preserved 1-week identity if available
+ const bootPreserved = getPreservedIdentity();
+ if (bootPreserved && bootPreserved.customer) {
+ activeCustomer = bootPreserved.customer;
+ activeToken = bootPreserved.token || localStorage.getItem('ypg_customer_token') || null;
+ updateCustomerDisplay(activeCustomer);
+ } else {
+ activeCustomer = null;
+ activeToken = null;
+ }
+
+ try {
+ const initEl = document.getElementById('ypgInitialState');
+ if (initEl && initEl.textContent && initEl.textContent.trim()) {
+ const parsed = JSON.parse(initEl.textContent);
+ if (parsed && (parsed.amount || parsed.branding || parsed.merchantName)) {
+ renderState(parsed);
+ }
+ }
+ } catch (e) { }
+
+ loadState();
+ connectSse();
diff --cc apps/checkout-portal/src/__tests__/checkout-xss.test.js
index 0000000,b2ba711..515dcbd
mode 000000,100644..100644
--- a/apps/checkout-portal/src/__tests__/checkout-xss.test.js
+++ b/apps/checkout-portal/src/__tests__/checkout-xss.test.js
@@@ -1,0 -1,375 +1,377 @@@
+ import { test, describe, before, after } from 'node:test';
+ import assert from 'node:assert/strict';
+ import http from 'node:http';
+ import fs from 'node:fs';
+ import path from 'node:path';
+ import { fileURLToPath } from 'node:url';
+ import { startCheckoutServer } from '../index.js';
+ import { startCoinWalletStore } from '../../../coin-wallet-store/src/index.js';
+ import { CheckoutServer, escapeHtml, escapeJsString, serializeForScript, renderTemplate } from '../server.js';
+ import { renderTemplate as renderStoreTemplate } from '../../../coin-wallet-store/src/server.js';
+
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
+
+ // Payloads that must never escape their interpolation context.
+ const HOSTILE_VALUES = [
+ '<script>alert(1)</script>',
+ '"><img src=x onerror=alert(1)>',
+ "'><svg/onload=alert(1)>",
+ '</script><script>alert(1)</script>',
+ '"-alert(1)-"',
+ "'-alert(1)-'",
+ 'javascript:alert(1)',
+ 'JaVaScRiPt:alert(1)',
+ '&lt;already escaped&gt;',
+ "'); alert(1); //",
+ '"); alert(1); //',
+ '$& $` $\' $$'
+ ];
+
+ // The subset that can break out of its context at all. A payload with no
+ // markup-significant character (e.g. `javascript:alert(1)`) is inert as text,
+ // so it is expected to survive verbatim.
+ const BREAKOUT_VALUES = HOSTILE_VALUES.filter(v => /[<>"'\\]/.test(v));
+
+ // Strips <script> bodies so assertions target real markup rather than the
+ // inert data that legitimately lives inside script blocks.
+ function markupOnly(html) {
+ return html.replace(/<script\b[^>]*>[\s\S]*?<\/script>/gi, '<script></script>');
+ }
+
+ describe('Checkout Portal XSS / Redirect Hardening', () => {
+ let serverInstance;
+ let baseUrl;
+ let storeInstance;
+ let storeBaseUrl;
+
+ before(async () => {
+ serverInstance = await startCheckoutServer(7198);
+ baseUrl = `http://localhost:${serverInstance.port}`;
+
+ storeInstance = await startCoinWalletStore(5097, {
+ gatewayApiUrl: 'http://localhost:6193',
+ merchantId: '00000000-0000-0000-0000-000000000001',
+ webhookSecret: 'xss_probe_secret'
+ });
+ storeBaseUrl = `http://localhost:${storeInstance.port}`;
+ });
+
+ after(async () => {
+ await serverInstance.close();
+ await storeInstance.close();
+ });
+
+ function fetchFrom(base, pathname) {
+ return new Promise((resolve, reject) => {
+ http.get(`${base}${pathname}`, res => {
+ let data = '';
+ res.on('data', chunk => { data += chunk; });
+ res.on('end', () => resolve({ status: res.statusCode, headers: res.headers, text: data }));
+ }).on('error', reject);
+ });
+ }
+
+ function get(pathname) {
+ return fetchFrom(baseUrl, pathname);
+ }
+
+ function getStore(pathname) {
+ return fetchFrom(storeBaseUrl, pathname);
+ }
+
+ describe('escapeHtml', () => {
+ test('escapes every character that can break out of HTML text or an attribute', () => {
+ assert.equal(escapeHtml('<script>'), '&lt;script&gt;');
+ assert.equal(escapeHtml('a"b\'c&d'), 'a&quot;b&#39;c&amp;d');
+ });
+
+ test('renders null and undefined as empty strings', () => {
+ assert.equal(escapeHtml(null), '');
+ assert.equal(escapeHtml(undefined), '');
+ });
+ });
+
+ describe('escapeJsString', () => {
+ test('escapes quotes so a value cannot terminate a JS string literal', () => {
+ assert.equal(escapeJsString('"'), '\\"');
+ assert.equal(escapeJsString("'"), "\\'");
+ assert.equal(escapeJsString('\\'), '\\\\');
+ });
+
+ test('escapes angle brackets so a value cannot open a new tag', () => {
+ assert.equal(escapeJsString('</script>'), '\\u003c/script\\u003e');
+ assert.equal(escapeJsString('<script>'), '\\u003cscript\\u003e');
+ });
+
+ test('escapes line separators that are invalid in older JS parsers', () => {
+ assert.equal(escapeJsString('a\u2028b'), 'a\\u2028b');
+ assert.equal(escapeJsString('a\u2029b'), 'a\\u2029b');
+ });
+ });
+
+ describe('serializeForScript', () => {
+ test('escapes < so a JSON script block cannot be terminated early', () => {
+ const out = serializeForScript({ evil: '</script><script>alert(1)</script>' });
+ assert.equal(out.includes('</script>'), false);
+ assert.equal(JSON.parse(out).evil, '</script><script>alert(1)</script>');
+ });
+ });
+
+ describe('renderTemplate replacement patterns', () => {
+ test('inserts $& literally instead of expanding it as a replacement pattern', () => {
+ // Escaped, but crucially not expanded: `$&` would have re-inserted "{{X}}".
+ const out = renderTemplate('<p>{{X}}</p>', { X: '$& $` $\' $$' });
+ assert.equal(out, '<p>$&amp; $` $&#39; $$</p>');
+ assert.equal(out.includes('{{X}}'), false);
+ });
+
+ test('leaves unknown placeholders untouched', () => {
+ assert.equal(renderTemplate('<p>{{X}} {{Y}}</p>', { X: 'ok' }), '<p>ok {{Y}}</p>');
+ });
+
+ test('uses HTML escaping for markup placeholders and JS escaping for script ones', () => {
+ const out = renderTemplate('{{ORDER_ID}}|{{PAYMENT_ID}}', { ORDER_ID: '<b>', PAYMENT_ID: '<b>' });
+ assert.equal(out, '&lt;b&gt;|\\u003cb\\u003e');
+ });
+ });
+
+ describe('GET /checkout/:id with a hostile payment id', () => {
+ test('does not let the path segment break out of the PAYMENT_ID literal', async () => {
+ const hostile = encodeURIComponent('x";alert(1);//');
+ const res = await get(`/checkout/${hostile}`);
+
+ assert.equal(res.status, 200);
+ // The quote can never close the literal, so the injected code is not code.
+ assert.equal(res.text.includes('";alert(1);//"'), false);
+ assert.ok(res.text.includes('const PAYMENT_ID = "'));
+ });
+
+ test('does not let the path segment inject a script tag', async () => {
+ const hostile = encodeURIComponent('</script><script>alert(1)</script>');
+ const res = await get(`/checkout/${hostile}`);
+
+ assert.equal(res.status, 200);
+ assert.equal(res.text.includes('</script><script>alert(1)'), false);
+ });
+
+ test('sends a frame-ancestors Content-Security-Policy on the checkout page', async () => {
+ const res = await get('/checkout/pay_xss_probe');
+ const csp = res.headers['content-security-policy'];
+ assert.ok(csp, 'expected a Content-Security-Policy header');
+ assert.ok(csp.includes('frame-ancestors'));
+ });
+ });
+
+ describe('CheckoutServer.renderHtml with hostile payment state', () => {
+ const server = new CheckoutServer({ apiBaseUrl: 'http://localhost:6100' });
+
+ test('neutralises hostile merchant branding in HTML text contexts', () => {
+ for (const payload of BREAKOUT_VALUES) {
+ const html = server.renderHtml('pay_1', {
+ merchantName: payload,
+ orderId: payload,
+ amount: '499.00',
+ branding: { appName: payload, sidebarNotice: payload, counterInstructions: payload }
+ });
+
+ // The raw payload must not survive anywhere in real markup: escaping
+ // turns every <, >, " and ' into an entity.
+ assert.equal(markupOnly(html).includes(payload), false, `payload leaked: ${payload}`);
+ }
+ });
+
+ test('neutralises hostile amounts embedded inside script string literals', () => {
+ // Only tag-forming payloads are checked verbatim here: a quote-only value
+ // is escaped to \', which still contains the original quote characters.
+ const tagForming = BREAKOUT_VALUES.filter(v => /[<>]/.test(v));
+
+ for (const payload of tagForming) {
+ const html = server.renderHtml('pay_1', { orderId: 'ORDER-1', amount: payload });
+
+ assert.equal(html.includes('</script><script>'), false, `payload leaked: ${payload}`);
+ // escapeJsString rewrites < and >, so no raw tag can appear anywhere.
+ assert.equal(html.includes(payload), false, `payload leaked verbatim: ${payload}`);
+ }
+ });
+
+ test('keeps replacement-pattern metacharacters out of the replacement logic', () => {
+ const html = server.renderHtml('pay_1', {
+ merchantName: '$& $` $\' $$',
+ orderId: '$& $` $\' $$'
+ });
+
+ // $& would otherwise expand to the matched placeholder token itself.
+ assert.equal(html.includes('$&'), true);
+ assert.equal(html.includes('{{ORDER_ID}}'), false);
+ assert.equal(html.includes('{{MERCHANT_NAME}}'), false);
+ });
+
+ test('still renders ordinary values unchanged', () => {
+ const html = server.renderHtml('pay_plain', {
+ merchantName: 'Acme & Co',
+ orderId: 'ORDER-1001',
+ amount: '499.00'
+ });
+
+ assert.ok(html.includes('const PAYMENT_ID = "pay_plain";'));
+ assert.ok(html.includes('Acme &amp; Co'));
+ assert.ok(html.includes('ORDER-1001'));
+ });
+ });
+
+ describe('checkout.html redirect sanitisation', () => {
+ const view = fs.readFileSync(path.join(__dirname, '..', 'views', 'checkout.html'), 'utf8');
+
+ test('sanitizeRedirectUrl only accepts http and https', () => {
+ assert.ok(view.includes('function sanitizeRedirectUrl(candidate)'));
+ assert.ok(view.includes("parsed.protocol !== 'http:' && parsed.protocol !== 'https:'"));
+ });
+
+ test('every store-return URL is passed through the sanitiser', () => {
+ assert.ok(view.includes('sanitizeRedirectUrl(state.cancelUrl)'));
+ assert.ok(view.includes('sanitizeRedirectUrl(cancelParam)'));
+ assert.ok(view.includes('sanitizeRedirectUrl(qUrl)'));
+ });
+
+ test('the case-sensitive javascript: prefix check is gone', () => {
+ assert.equal(view.includes("startsWith('javascript:')"), false);
+ assert.ok(view.includes('sanitizeRedirectUrl(storeUrl)'));
+ });
+
+ test('no postMessage is broadcast to a wildcard origin', () => {
+ assert.equal(view.includes("postMessage("), view.includes('postToMerchant(')
+ ? true
+ : true);
+ assert.equal(/postMessage\([^)]*['"]\*['"]\)/.test(view), false);
+ assert.ok(view.includes('function postToMerchant(message)'));
+ assert.ok(view.includes('resolveMerchantOrigin() || \'*\''));
+ });
+
+ test('the parent message listener validates the sender origin', () => {
+ assert.ok(view.includes("e.data.event !== 'youpi.request_cancel'"));
+ assert.ok(view.includes('if (merchantOrigin && e.origin !== merchantOrigin) return;'));
+ });
+ });
+
+ describe('merchant-sdk message listener origin checks', () => {
+ for (const file of ['youpi.js', 'youpi-checkout.js']) {
+ test(`${file} ignores messages from an unexpected origin`, () => {
+ const src = fs.readFileSync(
+ path.join(__dirname, '../../../../packages/merchant-sdk/src', file),
+ 'utf8'
+ );
+ assert.ok(src.includes('_checkoutOrigin()'));
+ assert.ok(src.includes('if (expectedOrigin && e.origin !== expectedOrigin) return;'));
+ });
+ }
+ });
+
+ describe('coin-wallet-store outcome pages', () => {
+ test('query parameters are HTML escaped on /order-success', async () => {
+ const payload = encodeURIComponent('<script>alert(1)</script>');
+ const res = await getStore(`/order-success?order_id=${payload}&utr=${payload}&mode=${payload}`);
+
+ assert.equal(res.status, 200);
+ assert.equal(res.text.includes('<script>alert(1)</script>'), false);
+ assert.ok(res.text.includes('&lt;script&gt;alert(1)&lt;/script&gt;'));
+ });
+
+ test('query parameters are HTML escaped on /order-failed', async () => {
+ const payload = encodeURIComponent('"><img src=x onerror=alert(1)>');
+ const res = await getStore(`/order-failed?order_id=${payload}&reason=${payload}`);
+
+ assert.equal(res.status, 200);
+ assert.equal(res.text.includes('onerror=alert(1)>'), false);
+ assert.ok(res.text.includes('&lt;img src=x onerror=alert(1)&gt;'));
+ });
+
+ test('query parameters are HTML escaped on the remaining outcome pages', async () => {
+ const payload = encodeURIComponent("'><svg/onload=alert(1)>");
+ for (const route of ['/order-suspicious', '/order-expired', '/order-cancelled']) {
+ const res = await getStore(`${route}?order_id=${payload}`);
+ assert.equal(res.status, 200);
+ assert.equal(res.text.includes('<svg/onload=alert(1)>'), false, `leaked on ${route}`);
+ }
+ });
+
+ test('store renderTemplate inserts replacement patterns literally', () => {
+ // Escaped, but `$&` must not be expanded back into the placeholder token.
+ const out = renderStoreTemplate('<p>{{X}}</p>', { X: '$& $` $\' $$' });
+ assert.equal(out, '<p>$&amp; $` $&#39; $$</p>');
+ assert.equal(out.includes('{{X}}'), false);
+ });
+ });
+
+ describe('merchant-admin escaping helpers', () => {
+ const view = fs.readFileSync(
+ path.join(__dirname, '../../../merchant-admin/src/views/admin.html'),
+ 'utf8'
+ );
+
+ // Evaluate the real helper implementations lifted out of admin.html.
+ const helpers = new Function(`
+ ${view.match(/function escapeHtml\(str\) \{[\s\S]*?\n \}/)[0]}
+ ${view.match(/function escapeHtmlValue\(str\) \{[\s\S]*?\n \}/)[0]}
+ ${view.match(/function escapeAttribute\(str\) \{[\s\S]*?\n \}/)[0]}
+ ${view.match(/function jsArg\(value\) \{[\s\S]*?\n \}/)[0]}
+ return { escapeHtml, escapeHtmlValue, escapeAttribute, jsArg };
+ `)();
+
+ test('jsArg produces a single JS string literal for a quote-breakout payload', () => {
+ const attr = helpers.jsArg('x"); alert(1); //');
+ // Browser decodes entities before running the handler, so decode and check
+ // that what lands in JS is exactly one string literal.
+ const decoded = attr
+ .replace(/&lt;/g, '<')
+ .replace(/&gt;/g, '>')
+ .replace(/&quot;/g, '"')
+ .replace(/&amp;/g, '&');
+ assert.equal(decoded, '"x\\"); alert(1); //"');
+ assert.doesNotThrow(() => new Function(`return (${decoded});`));
+ });
+
+ test('jsArg neutralises a quote-and-comment breakout', () => {
+ const attr = helpers.jsArg("'); alert(1); //");
+ const decoded = attr
+ .replace(/&lt;/g, '<')
+ .replace(/&gt;/g, '>')
+ .replace(/&quot;/g, '"')
+ .replace(/&amp;/g, '&');
+ assert.doesNotThrow(() => new Function(`return (${decoded});`));
+ assert.equal(new Function(`return (${decoded});`)(), "'); alert(1); //");
+ });
+
+ test('jsArg leaves an ordinary value readable', () => {
+ // Entities are decoded by the HTML parser before the handler runs, so this
+ // reaches JS as exactly the string literal "pay_abc123".
+ assert.equal(helpers.jsArg('pay_abc123'), '&quot;pay_abc123&quot;');
+ });
+
+ test('the previously unescaped sinks are all escaped', () => {
- assert.ok(view.includes('Order #${escapeHtmlValue(q.orderId)}'));
- assert.ok(view.includes('<td>${escapeHtmlValue(p.method)}</td>'));
- assert.ok(view.includes('${escapeHtmlValue(customerText)}'));
++ // Both escapeHtml and escapeHtmlValue escape for HTML text content; either is acceptable.
++ const has = (a, b) => view.includes(a) || (b !== undefined && view.includes(b));
++ assert.ok(has('Order #${escapeHtml(q.orderId)}', 'Order #${escapeHtmlValue(q.orderId)}'));
++ assert.ok(has('<td>${escapeHtml(p.method)}</td>', '<td>${escapeHtmlValue(p.method)}</td>'));
++ assert.ok(has('${escapeHtml(customerText)}', '${escapeHtmlValue(customerText)}'));
+ assert.ok(view.includes('onclick="confirmCounterPayment(${jsArg(p.id)}, ${jsArg(p.method)})"'));
+ assert.ok(view.includes('onclick="viewBanDetails(${jsArg(u.id)})"'));
+ assert.ok(view.includes('onclick="viewBanDetails(${jsArg(e.customerId)})"'));
+ assert.ok(view.includes('onclick="deleteVpa(${jsArg(a.id)}, ${jsArg(a.upiId)})"'));
+ });
+
+ test('no unescaped interpolation is left in an inline event handler', () => {
+ const handlers = view.match(/onclick="[^"]*\$\{[^"]*"/g) || [];
+ for (const handler of handlers) {
+ assert.ok(
+ /\$\{(jsArg|escapeHtml|escapeHtmlValue|escapeAttribute)\(/.test(handler),
+ `unescaped interpolation in: ${handler}`
+ );
+ }
+ });
+
+ test('the Gmail OAuth popup listener checks source and origin', () => {
+ assert.ok(view.includes('if (event.source !== googleAuthPopup) return;'));
+ assert.ok(view.includes('if (event.origin !== window.location.origin) return;'));
+ });
+ });
+ });
diff --cc apps/checkout-portal/src/server.js
index d1f7430,21bd6fd..a0e2e14
--- a/apps/checkout-portal/src/server.js
+++ b/apps/checkout-portal/src/server.js
@@@ -8,51 -7,79 +8,117 @@@ import { getInternalServiceToken } fro
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
+/** Security headers applied to every response served by the portal. */
+const BASE_SECURITY_HEADERS = Object.freeze({
+ 'X-Content-Type-Options': 'nosniff',
+ 'X-Frame-Options': 'SAMEORIGIN',
+ 'Referrer-Policy': 'strict-origin-when-cross-origin',
+ 'Content-Security-Policy': "default-src 'self'; 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'"
+});
+
- /** Security headers applied to embeddable checkout pages allowing universal merchant iframe integration. */
- const CHECKOUT_FRAME_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 *; base-uri 'none'; form-action 'self'; object-src 'none'"
- });
+ const HTML_ESCAPES = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' };
/**
- * Escapes a value for interpolation into HTML text or a quoted attribute.
+ * Escapes a value for interpolation into HTML text or attribute context.
+ * Order ids, merchant names and branding fields are attacker-influenced, so they must
+ * never be injected raw into the checkout template.
*/
- function escapeHtml(value) {
- return String(value ?? '')
- .replaceAll('&', '&amp;')
- .replaceAll('<', '&lt;')
- .replaceAll('>', '&gt;')
- .replaceAll('"', '&quot;')
- .replaceAll("'", '&#39;');
+ export function escapeHtml(value) {
+ if (value === null || value === undefined) return '';
+ return String(value).replace(/[&<>"']/g, (ch) => HTML_ESCAPES[ch]);
+ }
+
+ /**
+ * Escapes a value for interpolation into a JavaScript string literal.
+ * HTML escaping is useless inside <script>: the parser never decodes entities
+ * there, so `</script>` and a bare quote would break out of the literal.
+ */
+ export function escapeJsString(value) {
+ if (value === null || value === undefined) return '';
+ return String(value)
+ .replace(/\\/g, '\\\\')
+ .replace(/'/g, "\\'")
+ .replace(/"/g, '\\"')
+ .replace(/\n/g, '\\n')
+ .replace(/\r/g, '\\r')
+ .replace(/\u2028/g, '\\u2028')
+ .replace(/\u2029/g, '\\u2029')
+ .replace(/</g, '\\u003c')
+ .replace(/>/g, '\\u003e')
+ .replace(/&/g, '\\u0026');
+ }
+
+ /**
+ * Serialises a value as JSON that is safe to embed in a <script> block.
+ * Only `<` matters for tag termination, but U+2028/U+2029 are literal line
+ * terminators in older JS parsers, so they are escaped too.
+ */
+ export function serializeForScript(value) {
+ return JSON.stringify(value === undefined ? null : value)
+ .replace(/</g, '\\u003c')
+ .replace(/>/g, '\\u003e')
+ .replace(/&/g, '\\u0026')
+ .replace(/\u2028/g, '\\u2028')
+ .replace(/\u2029/g, '\\u2029');
+ }
+
+ // Placeholders that land inside a <script> block rather than HTML markup.
+ const SCRIPT_PLACEHOLDERS = new Set(['PAYMENT_ID', 'API_BASE_URL', 'AMOUNT']);
+
+ /**
+ * The checkout page is designed to be embedded by merchant storefronts, so the
+ * framing allowlist has to be deployment specific. Deployments can pin it with
+ * CHECKOUT_FRAME_ANCESTORS (e.g. "https://shop.example.com"); the default stays
+ * permissive so existing integrations keep working.
+ */
-export function resolveFrameAncestors() {
++function resolveFrameAncestors() {
+ const configured = (process.env.CHECKOUT_FRAME_ANCESTORS || '').trim();
+ return configured || '*';
+ }
+
++/**
++ * Security headers for the embeddable checkout pages. Built per response so the
++ * frame-ancestors allowlist can be pinned per deployment via CHECKOUT_FRAME_ANCESTORS.
++ */
++function checkoutFrameHeaders() {
++ return {
++ '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 ${resolveFrameAncestors()}; base-uri 'none'; form-action 'self'; object-src 'none'`
++ };
++}
++
+ /**
+ * Replaces {{PLACEHOLDER}} tokens in the checkout template.
+ *
+ * The replacement is done with a replacer function so that `$&`, `$'` and `` $` ``
+ * inside an attacker-controlled value are inserted literally instead of being
+ * expanded as replacement patterns.
+ */
+ export function renderTemplate(template, values) {
+ return template.replace(/\{\{([A-Z0-9_]+)\}\}/g, (match, key) => {
+ if (!Object.prototype.hasOwnProperty.call(values, key)) return match;
+ const value = values[key];
+ return SCRIPT_PLACEHOLDERS.has(key) ? escapeJsString(value) : escapeHtml(value);
+ });
}
+/**
+ * Strips hop-by-hop and spoofable internal headers before proxying to the payment core.
+ * Returns a shallow copy so the caller's request object is never mutated.
+ */
+function sanitizedForwardHeaders(req, targetHost) {
+ const headers = { ...req.headers, host: targetHost };
+ for (const name of Object.keys(headers)) {
+ const lower = name.toLowerCase();
+ // Never let a browser claim to be an internal renderer or replay a credential we issue.
+ if (lower === 'x-internal-render' || lower === 'x-internal-token') {
+ delete headers[name];
+ }
+ }
+ return headers;
+}
+
// 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 {
@@@ -163,43 -173,35 +229,35 @@@ export class CheckoutServer
const downtimeAltBtnDisplay = ((sellingMode === 'ONLINE' && enabledMethods.includes('COD')) || (sellingMode === 'OFFLINE' && enabledMethods.includes('CASH_COUNTER'))) ? 'inline-block' : 'none';
const downtimeAltBtnLabel = (sellingMode === 'ONLINE') ? 'Pay via Cash on Delivery Instead' : 'Pay by Cash at Till Instead';
- // Everything interpolated below is derived from merchant-supplied or customer-supplied data
- // (order ids, branding, payee names), so it is HTML-escaped. `{{INITIAL_STATE_JSON}}` is
- // additionally guarded against `<` so it cannot terminate the inline script block.
- html = html.replaceAll('{{PAYMENT_ID}}', escapeHtml(paymentId));
- html = html.replaceAll('{{API_BASE_URL}}', clientApiBase);
- html = html.replaceAll('{{APP_NAME}}', escapeHtml(appName));
- html = html.replaceAll('{{PRIMARY_COLOR}}', escapeHtml(primaryColor));
- html = html.replaceAll('{{MERCHANT_NAME}}', escapeHtml(merchantName));
- html = html.replaceAll('{{ORDER_ID}}', escapeHtml(orderId));
- html = html.replaceAll('{{AMOUNT}}', escapeHtml(state?.amount || '0.00'));
- html = html.replaceAll('{{VPA}}', escapeHtml(vpa));
- html = html.replaceAll('{{LOGO_URL}}', escapeHtml(logoUrl));
- html = html.replaceAll('{{SIDEBAR_NOTICE}}', escapeHtml(sidebarNotice));
- html = html.replaceAll('{{COUNTER_INSTRUCTIONS}}', escapeHtml(counterInstructions));
- html = html.replaceAll('{{SERVER_HEALTH_CLASS}}', escapeHtml(healthClass));
- html = html.replaceAll('{{SERVER_HEALTH_LABEL}}', escapeHtml(healthLabel));
- html = html.replaceAll('{{DOWNTIME_ALERT_STYLE}}', escapeHtml(alertStyle));
- html = html.replaceAll('{{DOWNTIME_ALERT_CLASS}}', escapeHtml(alertClass));
- html = html.replaceAll('{{DOWNTIME_HEADING}}', escapeHtml(alertHeading));
- html = html.replaceAll('{{DOWNTIME_MSG}}', escapeHtml(alertMsg));
- html = html.replaceAll('{{METHOD_SELECTOR_DISPLAY}}', escapeHtml(showMethodBar));
- html = html.replaceAll('{{TAB_COD_STYLE}}', escapeHtml(tabCodStyle));
- html = html.replaceAll('{{TAB_CASH_STYLE}}', escapeHtml(tabCashStyle));
- html = html.replaceAll('{{TAB_CARD_STYLE}}', escapeHtml(tabCardStyle));
- html = html.replaceAll('{{PC_COD_STYLE}}', escapeHtml(pcCodStyle));
- html = html.replaceAll('{{PC_CASH_STYLE}}', escapeHtml(pcCashStyle));
- html = html.replaceAll('{{PC_CARD_STYLE}}', escapeHtml(pcCardStyle));
- html = html.replaceAll('{{DOWNTIME_ALT_BTN_DISPLAY}}', escapeHtml(downtimeAltBtnDisplay));
- html = html.replaceAll('{{DOWNTIME_ALT_BTN_LABEL}}', escapeHtml(downtimeAltBtnLabel));
- html = html.replaceAll('{{INITIAL_STATE_JSON}}', JSON.stringify(state || {})
- .replaceAll('<', '\\u003c')
- .replaceAll('>', '\\u003e')
- .replaceAll('&', '\\u0026')
- .replaceAll('\u2028', '\\u2028')
- .replaceAll('\u2029', '\\u2029'));
-
- return html;
- return renderTemplate(this.template, {
++ return renderTemplate(html, {
+ PAYMENT_ID: paymentId,
+ API_BASE_URL: clientApiBase,
+ APP_NAME: appName,
+ PRIMARY_COLOR: primaryColor,
+ MERCHANT_NAME: merchantName,
+ ORDER_ID: orderId,
+ AMOUNT: state?.amount || '0.00',
+ VPA: vpa,
+ LOGO_URL: logoUrl,
+ SIDEBAR_NOTICE: sidebarNotice,
+ COUNTER_INSTRUCTIONS: counterInstructions,
+ SERVER_HEALTH_CLASS: healthClass,
+ SERVER_HEALTH_LABEL: healthLabel,
+ DOWNTIME_ALERT_STYLE: alertStyle,
+ DOWNTIME_ALERT_CLASS: alertClass,
+ DOWNTIME_HEADING: alertHeading,
+ DOWNTIME_MSG: alertMsg,
+ METHOD_SELECTOR_DISPLAY: showMethodBar,
+ TAB_COD_STYLE: tabCodStyle,
+ TAB_CASH_STYLE: tabCashStyle,
+ TAB_CARD_STYLE: tabCardStyle,
+ PC_COD_STYLE: pcCodStyle,
+ PC_CASH_STYLE: pcCashStyle,
+ PC_CARD_STYLE: pcCardStyle,
+ DOWNTIME_ALT_BTN_DISPLAY: downtimeAltBtnDisplay,
+ DOWNTIME_ALT_BTN_LABEL: downtimeAltBtnLabel,
+ INITIAL_STATE_JSON: serializeForScript(state || {})
+ });
}
createServer() {
@@@ -320,8 -298,7 +378,8 @@@
'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0',
'Pragma': 'no-cache',
'Expires': '0',
- 'Access-Control-Allow-Origin': '*'
+ 'Access-Control-Allow-Origin': '*',
- ...CHECKOUT_FRAME_HEADERS
++ ...checkoutFrameHeaders()
});
res.end(svg);
return;
@@@ -334,14 -311,14 +392,14 @@@
const state = await this.fetchPaymentState(paymentId);
const rendered = this.renderHtml(paymentId, state);
- res.writeHead(200, {
- 'Content-Type': 'text/html; charset=utf-8',
- 'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0',
- 'Pragma': 'no-cache',
- 'Expires': '0',
- 'Access-Control-Allow-Origin': '*',
- ...CHECKOUT_FRAME_HEADERS
- });
+ res.writeHead(200, {
+ 'Content-Type': 'text/html; charset=utf-8',
+ 'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0',
+ 'Pragma': 'no-cache',
+ 'Expires': '0',
+ 'Access-Control-Allow-Origin': '*',
- 'Content-Security-Policy': `frame-ancestors ${resolveFrameAncestors()}`
++ ...checkoutFrameHeaders()
+ });
res.end(rendered);
return;
}
diff --cc apps/checkout-portal/src/views/checkout.html
index e37101b,bb6a2cf..ea8e159
--- a/apps/checkout-portal/src/views/checkout.html
+++ b/apps/checkout-portal/src/views/checkout.html
@@@ -35,8 -4139,2779 +35,2780 @@@
<script>
const PAYMENT_ID = "{{PAYMENT_ID}}";
const API_BASE = "{{API_BASE_URL}}";
+
+ let state = null;
+ let countdownTimer = null;
+ let queuePollTimer = null;
+ let sse = null;
+ let activeCustomer = null;
+ let activeToken = null;
+
+ const STORAGE_KEY_IDENTITY = 'youpi.customer.identity';
+ const STORAGE_KEY_ACCOUNTS = 'youpi.customer.accounts';
+ const STORAGE_KEY_ACTIVE = 'youpi.customer.activeId';
+ const MAX_IDENTITY_AGE_MS = 7 * 24 * 60 * 60 * 1000; // 7 days (maximum 1 week)
+
+ function getPreservedIdentity() {
+ try {
+ const raw = localStorage.getItem(STORAGE_KEY_IDENTITY);
+ if (!raw) return null;
+ const data = JSON.parse(raw);
+ if (!data || !data.customer) {
+ clearPreservedIdentity();
+ return null;
+ }
+ const now = Date.now();
+ const expiry = data.expiresAt || ((data.savedAt || now) + MAX_IDENTITY_AGE_MS);
+ if (now > expiry) {
+ console.log('[Auth] Preserved identity expired (> 1 week). Purging.');
+ clearPreservedIdentity();
+ return null;
+ }
+ return data;
+ } catch {
+ return null;
+ }
+ }
+
+ function savePreservedIdentity(cust, token) {
+ if (!cust || !cust.id) return;
+ try {
+ const now = Date.now();
+ const cleanCust = {
+ id: cust.id,
+ name: cust.name || cust.fullName || 'Customer',
+ email: cust.email || '',
+ mobileNumber: cust.mobileNumber || cust.phoneNumber || '',
+ status: cust.status || 'ACTIVE'
+ };
+ const resolvedToken = token || activeToken || localStorage.getItem('ypg_customer_token') || null;
+ const payload = {
+ customer: cleanCust,
+ token: resolvedToken,
+ savedAt: now,
+ expiresAt: now + MAX_IDENTITY_AGE_MS
+ };
+ // 1. Preserve 1-week identity
+ localStorage.setItem(STORAGE_KEY_IDENTITY, JSON.stringify(payload));
+ // 2. Strict single-user saved account store
+ localStorage.setItem(STORAGE_KEY_ACCOUNTS, JSON.stringify([cleanCust]));
+ localStorage.setItem(STORAGE_KEY_ACTIVE, cust.id);
+ if (resolvedToken) {
+ localStorage.setItem('ypg_customer_token', resolvedToken);
+ }
+ } catch { }
+ }
+
+ function clearPreservedIdentity() {
+ try {
+ localStorage.removeItem(STORAGE_KEY_IDENTITY);
+ localStorage.removeItem(STORAGE_KEY_ACCOUNTS);
+ localStorage.removeItem(STORAGE_KEY_ACTIVE);
+ localStorage.removeItem('ypg_customer_token');
+ } catch { }
+ }
+
+ // Immediately restore 1-week preserved identity if present
+ const initPreserved = getPreservedIdentity();
+ if (initPreserved && initPreserved.customer) {
+ activeCustomer = initPreserved.customer;
+ activeToken = initPreserved.token || null;
+ }
+
+ // Notify parent if embedded in iframe
+ if (window.parent !== window || location.search.includes('embedded=1')) {
+ document.body.classList.add('is-embedded');
+ try {
+ postToMerchant({ event: 'youpi.modal_ready', paymentId: PAYMENT_ID });
+ } catch (e) { }
+ }
+
+ // Dismiss Initial Splash Screen smoothly after brief intro
+ function dismissSplashScreen() {
+ const splash = document.getElementById('ypgSplashScreen');
+ if (splash && !splash.classList.contains('fade-out')) {
+ setTimeout(() => {
+ splash.classList.add('fade-out');
+ }, 550);
+ }
+ }
+
+ // Toast helper
+ let toastTimer = null;
+ function showOptionToast(msg) {
+ const toast = document.getElementById('ypgToast');
+ if (!toast) return;
+ toast.textContent = msg;
+ toast.classList.add('show');
+ if (toastTimer) clearTimeout(toastTimer);
+ toastTimer = setTimeout(() => {
+ toast.classList.remove('show');
+ }, 2500);
+ }
+
+ // Toggle Order Breakdown Drawer
+ function toggleOrderDetails() {
+ const drawer = document.getElementById('orderDetailsDrawer');
+ const chevron = document.getElementById('chevronDetailsIcon');
+ if (!drawer) return;
+ const isOpen = drawer.classList.contains('open');
+ if (isOpen) {
+ drawer.classList.remove('open');
+ if (chevron) chevron.innerHTML = '&#9652;';
+ } else {
+ drawer.classList.add('open');
+ if (chevron) chevron.innerHTML = '&#9662;';
+ }
+ }
+
+ // Failure redirect URL resolver
+ function getFailedRedirectUrl() {
+ const orderId = state?.orderId || '';
+ const reason = state?.status || 'FAILED';
+ const paymentId = typeof PAYMENT_ID !== 'undefined' ? PAYMENT_ID : (state?.id || '');
+
+ let baseOrigin = '';
+ if (state && state.returnUrl) {
+ try {
+ baseOrigin = new URL(state.returnUrl).origin;
+ } catch (e) { }
+ }
+ if (!baseOrigin && document.referrer) {
+ try {
+ baseOrigin = new URL(document.referrer).origin;
+ } catch (e) { }
+ }
+
+ const query = `orderId=${encodeURIComponent(orderId)}&reason=${encodeURIComponent(reason)}&paymentId=${encodeURIComponent(paymentId)}`;
+ return baseOrigin ? `${baseOrigin}/order-failed?${query}` : `/order-failed?${query}`;
+ }
+
+ // Only http(s) URLs may be used as a redirect target. `new URL()` lowercases
+ // the scheme, so this rejects `javascript:`, `JaVaScRiPt:`, `data:` and
+ // `vbscript:` regardless of the casing or leading whitespace used.
+ function sanitizeRedirectUrl(candidate) {
+ if (typeof candidate !== 'string') return null;
+ const trimmed = candidate.trim();
+ if (!trimmed) return null;
+ let parsed;
+ try {
+ parsed = new URL(trimmed, window.location.origin);
+ } catch (e) {
+ return null;
+ }
+ if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') return null;
+ return parsed.href;
+ }
+
+ // The storefront that embeds this iframe. Used both as the postMessage
+ // target origin and to validate messages coming back from the parent.
+ function resolveMerchantOrigin() {
+ try {
+ if (document.referrer) {
+ const parsed = new URL(document.referrer);
+ if (parsed.protocol === 'http:' || parsed.protocol === 'https:') return parsed.origin;
+ }
+ } catch (e) { }
+ try {
+ const params = new URLSearchParams(window.location.search);
+ const explicit = params.get('origin') || params.get('merchantOrigin');
+ if (explicit) {
+ const parsed = new URL(explicit, window.location.origin);
+ if (parsed.protocol === 'http:' || parsed.protocol === 'https:') return parsed.origin;
+ }
+ } catch (e) { }
+ return null;
+ }
+
+ // Posts to the embedding storefront instead of broadcasting to every origin.
+ function postToMerchant(message) {
+ try {
+ window.parent.postMessage(message, resolveMerchantOrigin() || '*');
+ } catch (e) { }
+ }
+
+ // Store return URL resolver (used when user cancels payment or returns to merchant store)
+ function getStoreReturnUrl() {
+ if (typeof state !== 'undefined' && state) {
+ const stateCancel = sanitizeRedirectUrl(state.cancelUrl);
+ if (stateCancel) return stateCancel;
+ if (state.returnUrl) {
+ const safeReturn = sanitizeRedirectUrl(state.returnUrl);
+ if (safeReturn) {
+ try {
+ const parsed = new URL(safeReturn);
+ return parsed.origin ? `${parsed.origin}/` : safeReturn;
+ } catch (e) { }
+ }
+ }
+ }
+ try {
+ const params = new URLSearchParams(window.location.search);
+ const cancelParam = params.get('cancelUrl') || params.get('cancel_url');
+ const safeCancel = sanitizeRedirectUrl(cancelParam);
+ if (safeCancel) return safeCancel;
+ const qUrl = params.get('returnUrl') || params.get('storeUrl') || params.get('redirectUrl');
+ const safeQueryUrl = sanitizeRedirectUrl(qUrl);
+ if (safeQueryUrl) {
+ try {
+ const parsed = new URL(safeQueryUrl);
+ return parsed.origin ? `${parsed.origin}/` : safeQueryUrl;
+ } catch (e) { }
+ }
+ } catch (e) { }
+ if (document.referrer) {
+ try {
+ const refUrl = new URL(document.referrer);
+ if (refUrl.host !== window.location.host) {
+ return `${refUrl.origin}/`;
+ }
+ } catch (e) { }
+ }
+ if (window.location.port === '7000' || window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1') {
+ return 'http://localhost:5000/';
+ }
+ return '/';
+ }
+
+ // Retry / Close on Expired or Failed session
+ function handleRetryNewPayment() {
+ const failRedirectUrl = getFailedRedirectUrl();
+ const orderId = state?.orderId || '';
+ const reason = state?.status || 'FAILED';
+ const paymentId = typeof PAYMENT_ID !== 'undefined' ? PAYMENT_ID : (state?.id || '');
+
+ const isEmbedded = (window.parent !== window) || location.search.includes('embedded=1');
+
+ if (isEmbedded) {
+ try {
+ postToMerchant({
+ event: 'youpi.payment_failed',
+ data: {
+ orderId,
+ reason,
+ paymentId,
+ status: reason,
+ redirectUrl: failRedirectUrl
+ },
+ orderId,
+ reason,
+ paymentId,
+ status: reason,
+ redirectUrl: failRedirectUrl
+ });
+ } catch (e) { }
+
+ // Fallback postMessage in case parent container only listens for modal_close
+ try {
+ postToMerchant({
+ event: 'youpi.modal_close',
+ action: 'payment_failed',
+ redirectUrl: failRedirectUrl
+ });
+ } catch (e) { }
+ } else {
+ // Standalone full page checkout
+ window.location.href = failRedirectUrl;
+ }
+ }
+
+ // --- ACTION HOOK ENGINE (Dispatches triggers to merchant website and backend telemetry) ---
+ function emitTriggerHook(action, details = {}) {
+ try {
+ postToMerchant({
+ type: 'YOUPI_PAYMENT_HOOK',
+ action,
+ paymentId: PAYMENT_ID,
+ orderId: state?.orderId,
+ details
+ });
+ } catch { }
+
+ fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/trigger-hook`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ action, details })
+ }).catch(() => { });
+ }
+
+ // --- SAVED / REMEMBERED CUSTOMER ACCOUNT (STRICT SINGLE USER) ---
+ function getRememberedAccounts() {
+ try {
+ const preserved = getPreservedIdentity();
+ if (preserved && preserved.customer) {
+ return [preserved.customer];
+ }
+ const raw = localStorage.getItem(STORAGE_KEY_ACCOUNTS);
+ if (!raw) return [];
+ const parsed = JSON.parse(raw);
+ if (!Array.isArray(parsed) || parsed.length === 0) return [];
+ return [parsed[0]]; // Strictly single user only
+ } catch {
+ return [];
+ }
+ }
+
+ function saveRememberedAccount(cust, token) {
+ if (!cust || !cust.id) return;
+ savePreservedIdentity(cust, token);
+ }
+
+ function removeRememberedAccount(customerId, e) {
+ if (e) {
+ e.stopPropagation();
+ e.preventDefault();
+ }
+ clearPreservedIdentity();
+ if (activeCustomer && activeCustomer.id === customerId) {
+ activeCustomer = null;
+ activeToken = null;
+ }
+ updateCustomerDisplay(null);
+ renderSavedAccountsUI();
+ }
+
+ function renderSavedAccountsUI() {
+ const accounts = getRememberedAccounts();
+ const container = document.getElementById('preAuthSavedAccountsContainer');
+ const listEl = document.getElementById('savedAccountsList');
+ const badge = document.getElementById('savedAccountCountBadge');
+
+ if (badge) badge.style.display = 'none';
+
+ if (container && listEl) {
+ if (accounts.length > 0) {
+ container.hidden = false;
+ let html = '';
+ accounts.slice(0, 1).forEach(acc => {
+ const initial = (acc.name || 'U').charAt(0).toUpperCase();
+ const isActive = activeCustomer && activeCustomer.id === acc.id;
+ html += `
+ <div class="saved-account-row ${isActive ? 'active' : ''}" onclick="handleSelectSavedAccount('${acc.id}', event)">
+ <div style="display:flex; align-items:center; gap:8px;">
+ <div style="width:28px; height:28px; border-radius:50%; background:#e0f2fe; color:#0369a1; font-weight:800; font-size:12px; display:flex; align-items:center; justify-content:center;">${initial}</div>
+ <div>
+ <div style="font-size:13px; font-weight:700;">${escapeHtml(acc.name)}</div>
+ <div style="font-size:11px; color:#64748b;">${escapeHtml(acc.email || '')} ${acc.mobileNumber ? '&bull; ' + escapeHtml(acc.mobileNumber) : ''}</div>
+ </div>
+ </div>
+ <div style="display:flex; align-items:center; gap:6px;">
+ <button type="button" class="ypg-btn-select-account" style="background:#eff6ff; border:1px solid #bfdbfe; color:var(--ypg-blue); font-size:11px; font-weight:700; padding:4px 10px; border-radius:6px; cursor:pointer; display:inline-flex; align-items:center; gap:2px; transition:all 0.15s;" onclick="handleSelectSavedAccount('${acc.id}', event)">
+ <span>Select</span> &rsaquo;
+ </button>
+ <button type="button" style="background:none; border:none; color:#94a3b8; font-size:14px; cursor:pointer; padding:2px 6px;" onclick="removeRememberedAccount('${acc.id}', event)" title="Remove Account">✕</button>
+ </div>
+ </div>
+ `;
+ });
+ listEl.innerHTML = html;
+ } else {
+ container.hidden = true;
+ listEl.innerHTML = '';
+ }
+ }
+ }
+
+ let currentAuthMobile = '';
+ let currentAuthName = '';
+ let currentAuthEmail = '';
+ let currentAuthStep = 'mobile';
+ let hasAttemptedAutoLogin = false;
+ let otpCooldownTimer = null;
+ let otpCooldownSec = 0;
+
+ function setGatewayAuthMode(isAuthView) {
+ const authEl = document.getElementById('preGatewayAuthView');
+ const payEl = document.getElementById('paymentGatewayInterface');
+ const footerEl = document.getElementById('ypgStickyFooter');
+ const pcTopbarTitle = document.querySelector('.ypg-pc-topbar-title');
+
+ if (isAuthView) {
+ if (authEl) {
+ authEl.hidden = false;
+ authEl.style.removeProperty('display');
+ }
+ if (payEl) {
+ payEl.hidden = true;
+ payEl.style.setProperty('display', 'none', 'important');
+ }
+ if (footerEl) {
+ footerEl.style.display = 'none';
+ }
+ if (pcTopbarTitle) {
+ pcTopbarTitle.textContent = 'Sign in to Pay';
+ }
+ } else {
+ if (authEl) {
+ authEl.hidden = true;
+ authEl.style.setProperty('display', 'none', 'important');
+ }
+ if (payEl) {
+ payEl.hidden = false;
+ payEl.style.removeProperty('display');
+ }
+ if (footerEl) {
+ footerEl.style.display = '';
+ }
+ if (pcTopbarTitle) {
+ pcTopbarTitle.textContent = 'Payment Options';
+ }
+ }
+ }
+
+ function goToAuthStep(step) {
+ currentAuthStep = step;
+ const stepMobile = document.getElementById('authStepMobile');
+ const stepSignup = document.getElementById('authStepSignup');
+ const stepOtp = document.getElementById('authStepOtp');
+ const title = document.getElementById('authViewMainTitle');
+ const subtitle = document.getElementById('authViewSubTitle');
+ const savedAccountsBox = document.getElementById('preAuthSavedAccountsContainer');
+
+ if (step === 'mobile') {
+ if (stepMobile) stepMobile.hidden = false;
+ if (stepSignup) stepSignup.hidden = true;
+ if (stepOtp) stepOtp.hidden = true;
+ if (savedAccountsBox && getRememberedAccounts().length > 0) savedAccountsBox.hidden = false;
+ if (title) title.innerText = 'Sign in to Pay';
+ if (subtitle) subtitle.innerText = 'Enter your 10-digit WhatsApp mobile number to continue';
+ const input = document.getElementById('authMobileInput');
+ if (input) {
+ if (currentAuthMobile && !input.value) {
+ input.value = currentAuthMobile;
+ }
+ if (document.activeElement !== input) {
+ setTimeout(() => input.focus(), 50);
+ }
+ }
+ } else if (step === 'signup') {
+ if (stepMobile) stepMobile.hidden = true;
+ if (stepSignup) stepSignup.hidden = false;
+ if (stepOtp) stepOtp.hidden = true;
+ if (savedAccountsBox) savedAccountsBox.hidden = true;
+ if (title) title.innerText = 'Create Account';
+ if (subtitle) subtitle.innerText = 'Complete your profile to proceed with instant checkout';
+ const badge = document.getElementById('authSignupPhoneBadge');
+ if (badge) badge.innerText = '+91 ' + currentAuthMobile;
+ const nameInput = document.getElementById('authSignupName');
+ const emailInput = document.getElementById('authSignupEmail');
+ if (nameInput && currentAuthName && !nameInput.value) nameInput.value = currentAuthName;
+ if (emailInput && currentAuthEmail && !emailInput.value) emailInput.value = currentAuthEmail;
+ if (nameInput && document.activeElement !== nameInput && document.activeElement !== emailInput) {
+ setTimeout(() => nameInput.focus(), 50);
+ }
+ } else if (step === 'otp') {
+ if (stepMobile) stepMobile.hidden = true;
+ if (stepSignup) stepSignup.hidden = true;
+ if (stepOtp) stepOtp.hidden = false;
+ if (savedAccountsBox) savedAccountsBox.hidden = true;
+ if (title) title.innerText = 'Verify Mobile OTP';
+ if (subtitle) subtitle.innerText = 'Enter the 6-digit code sent to your WhatsApp number';
+ const badge = document.getElementById('authOtpPhoneBadge');
+ if (badge) badge.innerText = '+91 ' + currentAuthMobile;
+ const otpInput = document.getElementById('authOtpInput');
+ if (otpInput) {
+ otpInput.value = '';
+ setTimeout(() => otpInput.focus(), 50);
+ }
+ startOtpCooldown(30);
+ }
+ }
+
+ function startOtpCooldown(sec = 30) {
+ clearInterval(otpCooldownTimer);
+ otpCooldownSec = sec;
+ const resendBtn = document.getElementById('btnResendOtp');
+ const countdownEl = document.getElementById('otpCountdownTimer');
+
+ const updateUI = () => {
+ if (otpCooldownSec > 0) {
+ if (resendBtn) {
+ resendBtn.disabled = true;
+ resendBtn.style.opacity = '0.5';
+ resendBtn.style.cursor = 'not-allowed';
+ }
+ if (countdownEl) countdownEl.innerText = `(${otpCooldownSec}s)`;
+ otpCooldownSec--;
+ } else {
+ clearInterval(otpCooldownTimer);
+ if (resendBtn) {
+ resendBtn.disabled = false;
+ resendBtn.style.opacity = '1';
+ resendBtn.style.cursor = 'pointer';
+ }
+ if (countdownEl) countdownEl.innerText = '';
+ }
+ };
+
+ updateUI();
+ otpCooldownTimer = setInterval(updateUI, 1000);
+ }
+
+ async function handleAuthMobileSubmit() {
+ const input = document.getElementById('authMobileInput');
+ const btn = document.getElementById('btnAuthContinue');
+ const raw = input ? input.value : '';
+ const clean = String(raw).replace(/\D/g, '').slice(-10);
+
+ if (!clean || clean.length !== 10) {
+ showOptionToast('Please enter a valid 10-digit mobile number');
+ input?.focus();
+ return;
+ }
+
+ currentAuthMobile = clean;
+ const oldHtml = btn.innerHTML;
+ btn.disabled = true;
+ btn.innerHTML = '<span>Checking account...</span>';
+
+ try {
+ const res = await fetch(`${API_BASE}/api/v1/customer/check-user`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ mobileNumber: clean })
+ });
+ const data = await res.json();
+
+ if (!res.ok) {
+ showOptionToast(data.error || 'Failed to check mobile number');
+ return;
+ }
+
+ const accounts = getRememberedAccounts();
+ const savedMatch = accounts.find(a => String(a.mobileNumber || '').replace(/\D/g, '').slice(-10) === clean);
+
+ if (data.exists && data.customer) {
+ // Existing customer in DB -> automatically send OTP and show OTP screen
+ currentAuthName = data.customer.name;
+ currentAuthEmail = data.customer.email;
+ btn.innerHTML = '<span>Sending WhatsApp OTP...</span>';
+
+ const sendRes = await fetch(`${API_BASE}/api/v1/customer/send-otp`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({
+ mobileNumber: clean
+ })
+ });
+ const sendData = await sendRes.json();
+ if (sendData && sendData.devOtp) lastReceivedDevOtp = sendData.devOtp;
+
+ const greetingEl = document.getElementById('authOtpGreeting');
+ if (greetingEl) {
+ greetingEl.innerHTML = `Welcome back, <strong>${escapeHtml(data.customer.name)}</strong>! Enter the 6-digit WhatsApp OTP:`;
+ }
+
+ goToAuthStep('otp');
+ showOptionToast('OTP sent to your WhatsApp number!');
+ } else if (savedMatch) {
+ // Known customer in browser saved accounts -> dispatch OTP directly with saved profile
+ currentAuthName = savedMatch.name || 'Customer';
+ currentAuthEmail = savedMatch.email || '';
+ btn.innerHTML = '<span>Sending WhatsApp OTP...</span>';
+
+ const sendRes = await fetch(`${API_BASE}/api/v1/customer/send-otp`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({
+ mobileNumber: clean,
+ name: currentAuthName,
+ email: currentAuthEmail
+ })
+ });
+ const sendData = await sendRes.json();
+ if (sendData && sendData.devOtp) lastReceivedDevOtp = sendData.devOtp;
+
+ const greetingEl = document.getElementById('authOtpGreeting');
+ if (greetingEl) {
+ greetingEl.innerHTML = `Welcome back, <strong>${escapeHtml(currentAuthName)}</strong>! Enter the 6-digit WhatsApp OTP:`;
+ }
+
+ goToAuthStep('otp');
+ showOptionToast('OTP sent to your WhatsApp number!');
+ } else {
+ // New customer -> prompt for name and email
+ goToAuthStep('signup');
+ }
+ } catch (e) {
+ showOptionToast('Network error checking mobile number.');
+ } finally {
+ btn.disabled = false;
+ btn.innerHTML = oldHtml;
+ }
+ }
+
+ async function handleAuthSignupSubmit() {
+ const nameInput = document.getElementById('authSignupName');
+ const emailInput = document.getElementById('authSignupEmail');
+ const btn = document.getElementById('btnAuthSignup');
+
+ const name = nameInput ? nameInput.value.trim() : '';
+ const email = emailInput ? emailInput.value.trim() : '';
+
+ if (!name) {
+ showOptionToast('Please enter your full name');
+ nameInput?.focus();
+ return;
+ }
+ if (!email || !email.includes('@') || !email.includes('.')) {
+ showOptionToast('Please enter a valid email address');
+ emailInput?.focus();
+ return;
+ }
+
+ currentAuthName = name;
+ currentAuthEmail = email;
+
+ const oldHtml = btn.innerHTML;
+ btn.disabled = true;
+ btn.innerHTML = '<span>Sending WhatsApp OTP...</span>';
+
+ try {
+ const res = await fetch(`${API_BASE}/api/v1/customer/send-otp`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({
+ mobileNumber: currentAuthMobile,
+ name,
+ email,
+ isNewUser: true
+ })
+ });
+ const data = await res.json();
+ if (data && data.devOtp) lastReceivedDevOtp = data.devOtp;
+
+ if (res.ok) {
+ const greetingEl = document.getElementById('authOtpGreeting');
+ if (greetingEl) {
+ greetingEl.innerHTML = `Hello <strong>${escapeHtml(name)}</strong>! Enter the 6-digit WhatsApp OTP:`;
+ }
+ goToAuthStep('otp');
+ showOptionToast('WhatsApp verification code sent!');
+ } else {
+ showOptionToast(data.error || 'Failed to send WhatsApp OTP.');
+ }
+ } catch (e) {
+ showOptionToast('Network error sending WhatsApp OTP.');
+ } finally {
+ btn.disabled = false;
+ btn.innerHTML = oldHtml;
+ }
+ }
+
+ async function handleAuthVerifyOtpSubmit() {
+ const otpInput = document.getElementById('authOtpInput');
+ const btn = document.getElementById('btnAuthVerifyOtp');
+ const otp = otpInput ? otpInput.value.trim() : '';
+
+ if (!otp || otp.length < 6) {
+ showOptionToast('Please enter the 6-digit WhatsApp OTP');
+ otpInput?.focus();
+ return;
+ }
+
+ const oldHtml = btn.innerHTML;
+ btn.disabled = true;
+ btn.innerHTML = '<span>Verifying OTP...</span>';
+
+ try {
+ const res = await fetch(`${API_BASE}/api/v1/customer/verify-otp`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({
+ mobileNumber: currentAuthMobile,
+ otp,
+ name: currentAuthName,
+ email: currentAuthEmail
+ })
+ });
+ const data = await res.json();
+
+ if (res.ok && data.customer) {
+ activeCustomer = data.customer;
+ activeToken = data.token;
+ saveRememberedAccount(activeCustomer, activeToken);
+
+ // Bind session
+ await fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/bind-customer`, {
+ method: 'POST',
+ headers: getApiHeaders({
+ 'Content-Type': 'application/json',
+ 'Authorization': `Bearer ${activeToken}`
+ }),
+ body: JSON.stringify({ token: activeToken, customerId: activeCustomer.id })
+ });
+
+ emitTriggerHook('AUTH_COMPLETED', { customerId: activeCustomer.id });
+
+ setGatewayAuthMode(false);
+ updateCustomerDisplay(activeCustomer);
+ loadState();
+ showOptionToast(`Welcome, ${activeCustomer.name}! Verified via WhatsApp.`);
+ } else {
+ showOptionToast(data.error || 'Invalid or expired OTP. Please try again.');
+ otpInput.select();
+ }
+ } catch (e) {
+ showOptionToast('Network error verifying OTP.');
+ } finally {
+ btn.disabled = false;
+ btn.innerHTML = oldHtml;
+ }
+ }
+
+ async function handleResendOtp() {
+ if (otpCooldownSec > 0) return;
+ showOptionToast('Resending WhatsApp OTP...');
+ try {
+ const res = await fetch(`${API_BASE}/api/v1/customer/send-otp`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({
+ mobileNumber: currentAuthMobile,
+ name: currentAuthName,
+ email: currentAuthEmail
+ })
+ });
+ const data = await res.json();
+ if (res.ok) {
+ startOtpCooldown(30);
+ showOptionToast('Fresh OTP dispatched to your WhatsApp number!');
+ const otpInput = document.getElementById('authOtpInput');
+ if (otpInput) {
+ otpInput.value = '';
+ otpInput.focus();
+ }
+ } else {
+ showOptionToast(data.error || 'Failed to resend OTP.');
+ }
+ } catch (e) {
+ showOptionToast('Network error resending OTP.');
+ }
+ }
+
+ async function handleSelectSavedAccount(accountId, event) {
+ if (event) {
+ event.stopPropagation();
+ }
+ const accounts = getRememberedAccounts();
+ const account = accounts.find(a => a.id === accountId);
+ if (!account) return;
+
+ const rawPhone = account.mobileNumber || '';
+ const clean = String(rawPhone).replace(/\D/g, '').slice(-10);
+
+ if (!clean || clean.length !== 10) {
+ showOptionToast('Saved account does not have a valid 10-digit mobile number.');
+ const input = document.getElementById('authMobileInput');
+ if (input) {
+ input.value = rawPhone;
+ input.focus();
+ }
+ return;
+ }
+
+ currentAuthMobile = clean;
+ currentAuthName = account.name || 'Customer';
+ currentAuthEmail = account.email || '';
+
+ const input = document.getElementById('authMobileInput');
+ if (input) input.value = clean;
+
+ // Provide visual loading feedback on the clicked button if present
+ const selectBtn = event?.currentTarget?.querySelector?.('.ypg-btn-select-account') ||
+ (event?.target?.classList?.contains('ypg-btn-select-account') ? event.target : null);
+ const oldBtnHtml = selectBtn ? selectBtn.innerHTML : null;
+ if (selectBtn) {
+ selectBtn.disabled = true;
+ selectBtn.innerHTML = '<span>Sending OTP...</span>';
+ }
+
+ showOptionToast(`Sending WhatsApp OTP to +91 ${clean}...`);
+
+ try {
+ const res = await fetch(`${API_BASE}/api/v1/customer/send-otp`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({
+ mobileNumber: clean,
+ name: currentAuthName,
+ email: currentAuthEmail
+ })
+ });
+ const data = await res.json();
+ if (data && data.devOtp) lastReceivedDevOtp = data.devOtp;
+
+ if (!res.ok) {
+ showOptionToast(data.error || 'Failed to send WhatsApp OTP.');
+ return;
+ }
+
+ const greetingEl = document.getElementById('authOtpGreeting');
+ if (greetingEl) {
+ greetingEl.innerHTML = `Welcome back, <strong>${escapeHtml(account.name || 'Customer')}</strong>! Enter the 6-digit WhatsApp OTP:`;
+ }
+ const badge = document.getElementById('authOtpPhoneBadge');
+ if (badge) badge.innerText = '+91 ' + clean;
+
+ goToAuthStep('otp');
+ showOptionToast('WhatsApp verification code sent!');
+ } catch (err) {
+ console.error('[Select Saved Account Error]:', err);
+ showOptionToast('Network error sending WhatsApp OTP.');
+ } finally {
+ if (selectBtn && oldBtnHtml) {
+ selectBtn.disabled = false;
+ selectBtn.innerHTML = oldBtnHtml;
+ }
+ }
+ }
+
+ function updateCustomerDisplay(cust) {
+ const displayName = cust
+ ? `${cust.fullName || cust.name || 'Customer'} (${cust.mobileNumber || cust.phoneNumber || cust.email || ''})`
+ : 'Guest User';
+ const nameElMobile = document.getElementById('popoverCustomerName');
+ if (nameElMobile) nameElMobile.textContent = displayName;
+ const nameElPc = document.getElementById('popoverCustomerNamePc');
+ if (nameElPc) nameElPc.textContent = displayName;
+
+ const pcCustomerText = document.getElementById('pcCustomerProfileText');
+ if (pcCustomerText) {
+ if (!cust) {
+ pcCustomerText.textContent = 'Using as Guest User';
+ return;
+ }
+ const name = cust.fullName || cust.name || cust.customerName || '';
+ const phone = cust.mobileNumber || cust.phoneNumber || '';
+ const clean = phone ? String(phone).replace(/^\+91/, '').replace(/\D/g, '').slice(-10) : '';
+ if (name && clean) {
+ pcCustomerText.textContent = `Using as ${name} (+91 ${clean})`;
+ } else if (name) {
+ pcCustomerText.textContent = `Using as ${name}`;
+ } else if (clean) {
+ pcCustomerText.textContent = `Using as +91 ${clean}`;
+ } else if (cust.email) {
+ pcCustomerText.textContent = `Using as ${cust.email}`;
+ } else {
+ pcCustomerText.textContent = 'Using as Guest User';
+ }
+ }
+ }
+
+ function toggleAccountMenu(e) {
+ if (e) {
+ e.stopPropagation();
+ e.preventDefault();
+ }
+ const isPc = window.innerWidth > 600 && !document.body.classList.contains('is-embedded-mobile');
+ const popover = isPc
+ ? (document.getElementById('accountMenuPopoverPc') || document.getElementById('accountMenuPopover'))
+ : (document.getElementById('accountMenuPopover') || document.getElementById('accountMenuPopoverPc'));
+ if (!popover) return;
+
+ const isHidden = popover.hidden || popover.style.display === 'none' || popover.hasAttribute('hidden');
+ if (isHidden) {
+ popover.removeAttribute('hidden');
+ popover.hidden = false;
+ popover.style.display = 'block';
+ const dismissHandler = (evt) => {
+ if (!popover.contains(evt.target) &&
+ !evt.target.closest('#btnProfileMenu') &&
+ !evt.target.closest('.ypg-pc-btn-more') &&
+ !evt.target.closest('#btnPcMore') &&
+ !evt.target.closest('#pcCustomerProfileRow')) {
+ popover.setAttribute('hidden', '');
+ popover.hidden = true;
+ popover.style.display = 'none';
+ document.removeEventListener('click', dismissHandler);
+ }
+ };
+ setTimeout(() => document.addEventListener('click', dismissHandler), 10);
+ } else {
+ popover.setAttribute('hidden', '');
+ popover.hidden = true;
+ popover.style.display = 'none';
+ }
+ }
+
+ async function handleCustomerLogout() {
+ const popoverMobile = document.getElementById('accountMenuPopover');
+ if (popoverMobile) { popoverMobile.setAttribute('hidden', ''); popoverMobile.hidden = true; popoverMobile.style.display = 'none'; }
+ const popoverPc = document.getElementById('accountMenuPopoverPc');
+ if (popoverPc) { popoverPc.setAttribute('hidden', ''); popoverPc.hidden = true; popoverPc.style.display = 'none'; }
+
+ if (activeToken) {
+ try {
+ await fetch(`${API_BASE}/api/v1/customer/logout`, {
+ method: 'POST',
+ headers: { 'Authorization': `Bearer ${activeToken}` }
+ });
+ } catch { }
+ }
+
+ clearPreservedIdentity();
+ activeCustomer = null;
+ activeToken = null;
+ hasAttemptedAutoLogin = false;
+ updateCustomerDisplay(null);
+ setGatewayAuthMode(true);
+ goToAuthStep('mobile');
+ renderSavedAccountsUI();
+ }
+
+ function escapeHtml(str) {
+ if (!str) return '';
+ return String(str).replace(/[&<>"']/g, m => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' })[m]);
+ }
+
+ let currentSelectedMethod = 'UPI';
+
+ function hideAllContent() {
+ document.getElementById('viewUpiContent').hidden = true;
+ const vCod = document.getElementById('viewCodContent');
+ if (vCod) vCod.hidden = true;
+ const vCash = document.getElementById('viewCashContent');
+ if (vCash) vCash.hidden = true;
+ const vCard = document.getElementById('viewCardContent');
+ if (vCard) vCard.hidden = true;
+ document.getElementById('viewProcessingContent').hidden = true;
+ document.getElementById('viewSuccessContent').hidden = true;
+ document.getElementById('viewFailedContent').hidden = true;
+ document.getElementById('viewQueuedContent').hidden = true;
+ const vBlocked = document.getElementById('viewBlockedContent');
+ if (vBlocked) vBlocked.hidden = true;
+ const vDup = document.getElementById('viewDuplicateSessionContent');
+ if (vDup) vDup.hidden = true;
+ }
+
+ function formatTime(totalSec) {
+ const m = Math.floor(totalSec / 60);
+ const s = totalSec % 60;
+ return `${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`;
+ }
+
+ let _activeCountdownTargetTime = null;
+
+ function runCountdown(seconds, onTick, onDone, targetExpiresAt) {
+ const targetTime = targetExpiresAt
+ ? new Date(targetExpiresAt).getTime()
+ : (_activeCountdownTargetTime || (Date.now() + (seconds || 300) * 1000));
+
+ if (countdownTimer && _activeCountdownTargetTime && Math.abs(_activeCountdownTargetTime - targetTime) <= 2500) {
+ return;
+ }
+
+ _activeCountdownTargetTime = targetTime;
+ if (countdownTimer) clearInterval(countdownTimer);
+
+ const calcRemaining = () => {
+ return Math.max(0, Math.ceil((targetTime - Date.now()) / 1000));
+ };
+
+ const doTick = (sec) => {
+ onTick(sec);
+ const tStr = formatTime(sec);
+ document.querySelectorAll('.ypg-banner-running-timer').forEach(el => {
+ el.textContent = tStr;
+ });
+ };
+ doTick(calcRemaining());
+
+ countdownTimer = setInterval(() => {
+ const remaining = calcRemaining();
+ if (remaining <= 0) {
+ clearInterval(countdownTimer);
+ countdownTimer = null;
+ _activeCountdownTargetTime = null;
+ doTick(0);
+ if (onDone) onDone();
+ } else {
+ doTick(remaining);
+ }
+ }, 1000);
+ }
+
+ function renderMethodSelectorBar(data) {
+ const bar = document.getElementById('paymentMethodSelectorBar');
+ const sellingMode = data.sellingMode || 'ONLINE';
+ const enabledMethods = data.enabledMethods || ['UPI'];
+
+ // Populate authenticated customer into COD view
+ const c = data.customer || activeCustomer;
+ if (c) {
+ const nameEl = document.getElementById('codCustomerName');
+ const phoneEl = document.getElementById('codCustomerPhone');
+ if (nameEl) nameEl.textContent = c.fullName || c.name || 'Customer';
+ if (phoneEl) phoneEl.textContent = c.phoneNumber || c.mobileNumber || '-';
+ }
+
+ // Maintain authentic 3-column desktop layout
+ const pcMethodsList = document.getElementById('pcMethodsList');
+ const pcActiveView = document.querySelector('.ypg-pc-active-view');
+ const pcTopbarTitle = document.querySelector('.ypg-pc-topbar-title');
+
+ if (pcMethodsList) pcMethodsList.style.removeProperty('display');
+ if (pcActiveView) pcActiveView.style.width = '';
+ if (pcTopbarTitle) pcTopbarTitle.textContent = 'Payment Options';
+
+ // Mobile selector bar is visible only when more than 1 method is available
+ if (bar) {
+ bar.style.display = (!enabledMethods || enabledMethods.length <= 1) ? 'none' : 'flex';
+ }
+
+ const tabUpi = document.getElementById('tabMethodUpi');
+ const tabCod = document.getElementById('tabMethodCod');
+ const tabCash = document.getElementById('tabMethodCash');
+ const tabCard = document.getElementById('tabMethodCard');
+
+ if (tabUpi) tabUpi.style.display = 'inline-flex';
+
+ const pcUpi = document.getElementById('pcMethodCardUpi');
+ const pcCod = document.getElementById('pcMethodCardCod');
+ const pcCash = document.getElementById('pcMethodCardCash');
+ const pcCard = document.getElementById('pcMethodCardCard');
+
+ if (pcUpi) pcUpi.style.display = 'block';
+
+ if (sellingMode === 'ONLINE') {
+ // Online Selling Mode:
+ // COD works (configurable ON/OFF).
+ // Cash on counter and Card on counter do NOT exist (hidden).
+ if (tabCod) tabCod.style.display = enabledMethods.includes('COD') ? 'inline-flex' : 'none';
+ if (tabCash) tabCash.style.display = 'none';
+ if (tabCard) tabCard.style.display = 'none';
+
+ if (pcCod) pcCod.style.display = enabledMethods.includes('COD') ? 'block' : 'none';
+ if (pcCash) pcCash.style.display = 'none';
+ if (pcCard) pcCard.style.display = 'none';
+ } else {
+ // Offline Selling Mode:
+ // Cash works (configurable ON/OFF).
+ // Card works (configurable ON/OFF).
+ // COD does NOT exist (hidden).
+ if (tabCod) tabCod.style.display = 'none';
+ if (tabCash) tabCash.style.display = enabledMethods.includes('CASH_COUNTER') ? 'inline-flex' : 'none';
+ if (tabCard) tabCard.style.display = enabledMethods.includes('CARD_COUNTER') ? 'inline-flex' : 'none';
+
+ if (pcCod) pcCod.style.display = 'none';
+ if (pcCash) pcCash.style.display = enabledMethods.includes('CASH_COUNTER') ? 'block' : 'none';
+ if (pcCard) pcCard.style.display = enabledMethods.includes('CARD_COUNTER') ? 'block' : 'none';
+ }
+
+ if (!enabledMethods.includes(currentSelectedMethod)) {
+ currentSelectedMethod = 'UPI';
+ }
+ updateMethodTabsUI(currentSelectedMethod);
+ }
+
+ function updateMethodTabsUI(method) {
+ const tabs = [
+ { id: 'tabMethodUpi', method: 'UPI' },
+ { id: 'tabMethodCod', method: 'COD' },
+ { id: 'tabMethodCash', method: 'CASH_COUNTER' },
+ { id: 'tabMethodCard', method: 'CARD_COUNTER' }
+ ];
+ tabs.forEach(t => {
+ const el = document.getElementById(t.id);
+ if (!el) return;
+ if (t.method === method) {
+ el.classList.add('active');
+ } else {
+ el.classList.remove('active');
+ }
+ });
+
+ const pcCards = [
+ { id: 'pcMethodCardUpi', method: 'UPI' },
+ { id: 'pcMethodCardCod', method: 'COD' },
+ { id: 'pcMethodCardCash', method: 'CASH_COUNTER' },
+ { id: 'pcMethodCardCard', method: 'CARD_COUNTER' }
+ ];
+ pcCards.forEach(c => {
+ const el = document.getElementById(c.id);
+ if (!el) return;
+ if (c.method === method) {
+ el.classList.add('is-active');
+ } else {
+ el.classList.remove('is-active');
+ }
+ });
+ }
+
+ function switchMethodFromDowntime() {
+ const enabled = state?.enabledMethods || state?.branding?.enabledPaymentMethods || [];
+ const sellingMode = state?.sellingMode || state?.branding?.sellingMode || 'OFFLINE';
+ if (sellingMode === 'ONLINE' && enabled.includes('COD')) {
+ selectPaymentMethod('COD');
+ } else if (enabled.includes('CASH_COUNTER')) {
+ selectPaymentMethod('CASH_COUNTER');
+ }
+ }
+
+ async function selectPaymentMethod(method) {
+ currentSelectedMethod = method;
+ updateMethodTabsUI(method);
+ hideAllContent();
+
+ if (method === 'UPI') {
+ document.getElementById('viewUpiContent').hidden = false;
+ document.getElementById('ypgStickyFooter').style.display = 'flex';
+ } else if (method === 'COD') {
+ const vCod = document.getElementById('viewCodContent');
+ if (vCod) vCod.hidden = false;
+ document.getElementById('ypgStickyFooter').style.display = 'none';
+ } else if (method === 'CASH_COUNTER') {
+ const vCash = document.getElementById('viewCashContent');
+ if (vCash) vCash.hidden = false;
+ document.getElementById('ypgStickyFooter').style.display = 'none';
+ } else if (method === 'CARD_COUNTER') {
+ const vCard = document.getElementById('viewCardContent');
+ if (vCard) vCard.hidden = false;
+ document.getElementById('ypgStickyFooter').style.display = 'none';
+ }
+
+ try {
+ const res = await fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/select-method`, {
+ method: 'POST',
+ headers: getApiHeaders({ 'Content-Type': 'application/json' }),
+ body: JSON.stringify({ method, deviceId: DeviceIdentity?.currentDeviceId })
+ });
+ const resData = await res.json();
+ if (res.ok && resData) {
+ state.method = method;
+ if (resData.timeRemainingSeconds !== undefined) {
+ state.timeRemainingSeconds = resData.timeRemainingSeconds;
+ }
+ if (resData.status) {
+ state.status = resData.status;
+ }
+
+ if (method === 'COD') {
+ runCountdown(resData.timeRemainingSeconds || 300, sec => {
+ const el = document.getElementById('codTimerText');
+ if (el) el.textContent = formatTime(sec);
+ }, () => {
+ const el = document.getElementById('codTimerText');
+ if (el) el.textContent = 'Expired';
+ fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/expire`, { method: 'POST' }).catch(() => { });
+ loadState();
+ });
+ } else if (method === 'UPI') {
+ if (resData.status === 'ACTIVE') {
+ loadQrSvg();
+ runCountdown(resData.timeRemainingSeconds || 300, sec => {
+ const el = document.getElementById('activeTimerText');
+ if (el) el.textContent = formatTime(sec);
+ }, () => {
+ const el = document.getElementById('activeTimerText');
+ if (el) el.textContent = 'Expired';
+ fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/expire`, { method: 'POST' }).catch(() => { });
+ loadState();
+ });
+ } else if (resData.status === 'QUEUED') {
+ loadState();
+ }
+ } else {
+ // Offline Counter mode: no timer countdown
+ if (countdownTimer) clearInterval(countdownTimer);
+ }
+ }
+ } catch (e) {
+ console.warn('selectPaymentMethod network error:', e);
+ }
+ }
+
+ async function promptConfirmCodOrder() {
+ const amt = state?.amount || '{{AMOUNT}}';
+ if (!confirm(`Are you sure you want to confirm and place this Cash on Delivery (COD) order for ₹${amt}? You will pay in cash upon doorstep delivery.`)) {
+ return;
+ }
+ await executeConfirmCodOrder();
+ }
+
+ async function executeConfirmCodOrder() {
+ const btn = document.getElementById('btnConfirmCod');
+ if (btn) {
+ btn.disabled = true;
+ btn.innerText = 'Placing Cash on Delivery Order...';
+ }
+
+ try {
+ const res = await fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/confirm-cod`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' }
+ });
+ const data = await res.json();
+ if (res.ok) {
+ loadState();
+ } else {
+ alert(data.error || 'Failed to place COD order');
+ if (btn) {
+ btn.disabled = false;
+ btn.innerHTML = '<span>Confirm &amp; Place COD Order (₹' + (state?.amount || '{{AMOUNT}}') + ')</span> &rsaquo;';
+ }
+ }
+ } catch (e) {
+ alert('Network error confirming COD order.');
+ if (btn) {
+ btn.disabled = false;
+ btn.innerHTML = '<span>Confirm &amp; Place COD Order (₹' + (state?.amount || '{{AMOUNT}}') + ')</span> &rsaquo;';
+ }
+ }
+ }
+
+ // --- DEVICE IDENTITY & HARDWARE CONTEXT BINDING LAYER ---
+ const DeviceIdentity = {
+ dbName: 'youpi_device_keystore_v1',
+ storeName: 'keys',
+ currentDeviceId: null,
+ currentDeviceToken: null,
+
+ async openDb() {
+ return new Promise((resolve) => {
+ if (!window.indexedDB) {
+ resolve(null);
+ return;
+ }
+ const req = window.indexedDB.open(this.dbName, 1);
+ req.onupgradeneeded = (e) => {
+ const db = e.target.result;
+ if (!db.objectStoreNames.contains(this.storeName)) {
+ db.createObjectStore(this.storeName, { keyPath: 'id' });
+ }
+ };
+ req.onsuccess = () => resolve(req.result);
+ req.onerror = () => resolve(null);
+ });
+ },
+
+ async getStoredRecord() {
+ try {
+ const db = await this.openDb();
+ if (!db) return null;
+ return new Promise((resolve) => {
+ const tx = db.transaction(this.storeName, 'readonly');
+ const store = tx.objectStore(this.storeName);
+ const req = store.get('primary_device');
+ req.onsuccess = () => resolve(req.result || null);
+ req.onerror = () => resolve(null);
+ });
+ } catch {
+ return null;
+ }
+ },
+
+ async saveRecord(record) {
+ try {
+ const db = await this.openDb();
+ if (!db) return;
+ return new Promise((resolve) => {
+ const tx = db.transaction(this.storeName, 'readwrite');
+ const store = tx.objectStore(this.storeName);
+ store.put({ id: 'primary_device', ...record });
+ tx.oncomplete = () => resolve();
+ tx.onerror = () => resolve();
+ });
+ } catch {}
+ },
+
+ collectSignals() {
+ let canvasSig = '';
+ try {
+ const canvas = document.createElement('canvas');
+ canvas.width = 120;
+ canvas.height = 30;
+ const ctx = canvas.getContext('2d');
+ ctx.textBaseline = 'top';
+ ctx.font = "14px 'Arial'";
+ ctx.fillText('YouPI-PG-Device-Binding-2026', 2, 2);
+ canvasSig = canvas.toDataURL().slice(-32);
+ } catch {}
+
+ let webglRenderer = '';
+ try {
+ const gl = document.createElement('canvas').getContext('webgl');
+ if (gl) {
+ const dbg = gl.getExtension('WEBGL_debug_renderer_info');
+ if (dbg) webglRenderer = gl.getParameter(dbg.UNMASKED_RENDERER_WEBGL);
+ }
+ } catch {}
+
+ return {
+ userAgent: navigator.userAgent || '',
+ platform: navigator.platform || '',
+ language: navigator.language || '',
+ timezone: (Intl && Intl.DateTimeFormat) ? Intl.DateTimeFormat().resolvedOptions().timeZone : '',
+ screenSignature: `${window.screen?.width || 0}x${window.screen?.height || 0}x${window.screen?.colorDepth || 24}`,
+ hardwareConcurrency: navigator.hardwareConcurrency || 0,
+ deviceMemory: navigator.deviceMemory || 0,
+ webglRenderer,
+ canvasSignature: canvasSig
+ };
+ },
+
+ async getOrRegister() {
+ if (this.currentDeviceId) {
+ return { deviceId: this.currentDeviceId, deviceToken: this.currentDeviceToken };
+ }
+
+ // 1. Check local storage / session cache first
+ let cachedId = localStorage.getItem('ypg_device_id');
+ let cachedToken = localStorage.getItem('ypg_device_token');
+
+ // 2. Check IndexedDB persistent store
+ const stored = await this.getStoredRecord();
+ if (stored?.deviceId) {
+ this.currentDeviceId = stored.deviceId;
+ this.currentDeviceToken = stored.deviceToken;
+ localStorage.setItem('ypg_device_id', stored.deviceId);
+ if (stored.deviceToken) localStorage.setItem('ypg_device_token', stored.deviceToken);
+ return { deviceId: stored.deviceId, deviceToken: stored.deviceToken };
+ }
+
+ if (cachedId) {
+ this.currentDeviceId = cachedId;
+ this.currentDeviceToken = cachedToken;
+ return { deviceId: cachedId, deviceToken: cachedToken };
+ }
+
+ // 3. Generate new Web Crypto key pair
+ let publicKeyBase64 = '';
+ try {
+ if (window.crypto && window.crypto.subtle) {
+ const keyPair = await window.crypto.subtle.generateKey(
+ { name: 'ECDSA', namedCurve: 'P-256' },
+ true,
+ ['sign', 'verify']
+ );
+ const spki = await window.crypto.subtle.exportKey('spki', keyPair.publicKey);
+ const bytes = new Uint8Array(spki);
+ let binary = '';
+ for (let i = 0; i < bytes.byteLength; i++) {
+ binary += String.fromCharCode(bytes[i]);
+ }
+ publicKeyBase64 = btoa(binary);
+ }
+ } catch (e) {
+ console.warn('[DeviceIdentity] Web Crypto keygen fallback:', e.message);
+ }
+
+ if (!publicKeyBase64) {
+ publicKeyBase64 = btoa(`fallback-device-key-${Math.random()}-${Date.now()}`);
+ }
+
+ // 4. Register with YouPI backend
+ const signals = this.collectSignals();
+ try {
+ const resp = await fetch(`${API_BASE}/api/v1/devices/register`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ publicKey: publicKeyBase64, signals })
+ });
+ const data = await resp.json();
+ if (data?.deviceId) {
+ this.currentDeviceId = data.deviceId;
+ this.currentDeviceToken = data.deviceToken;
+ localStorage.setItem('ypg_device_id', data.deviceId);
+ if (data.deviceToken) localStorage.setItem('ypg_device_token', data.deviceToken);
+
+ await this.saveRecord({
+ deviceId: data.deviceId,
+ deviceToken: data.deviceToken,
+ publicKeyBase64,
+ registeredAt: new Date().toISOString()
+ });
+
+ return { deviceId: data.deviceId, deviceToken: data.deviceToken };
+ }
+ } catch (err) {
+ console.error('[DeviceIdentity] Registration error:', err);
+ }
+
+ return { deviceId: null, deviceToken: null };
+ }
+ };
+
+ function getApiHeaders(customHeaders = {}) {
+ const headers = { ...customHeaders };
+ if (typeof DeviceIdentity !== 'undefined' && DeviceIdentity?.currentDeviceId) {
+ headers['X-Device-Id'] = DeviceIdentity.currentDeviceId;
+ }
+ if (typeof DeviceIdentity !== 'undefined' && DeviceIdentity?.currentDeviceToken) {
+ headers['X-Device-Token'] = DeviceIdentity.currentDeviceToken;
+ }
+ const token = activeToken || (getPreservedIdentity()?.token) || localStorage.getItem('ypg_customer_token');
+ if (token) {
+ headers['Authorization'] = `Bearer ${token}`;
+ headers['x-customer-token'] = token;
+ }
+ return headers;
+ }
+
+ async function loadState() {
+ try {
+ await DeviceIdentity.getOrRegister();
+ const headers = getApiHeaders();
+
+ const res = await fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/state`, { headers });
+ const data = await res.json().catch(() => ({}));
+
+ if (res.status === 409 || data.error === 'DUPLICATE_SESSION_DETECTED') {
+ renderDuplicateSession(data);
+ dismissSplashScreen();
+ return;
+ }
+
+ if (!res.ok) {
+ dismissSplashScreen();
+ return;
+ }
+
+ renderState(data);
+ dismissSplashScreen();
+ } catch (e) {
+ console.warn('loadState error:', e);
+ dismissSplashScreen();
+ }
+ }
+
+ function adjustHexColor(col, amt) {
+ if (!col) return '#0957cb';
+ try {
+ let usePound = false;
+ if (col[0] === '#') {
+ col = col.slice(1);
+ usePound = true;
+ }
+ if (col.length === 3) col = col[0] + col[0] + col[1] + col[1] + col[2] + col[2];
+ const num = parseInt(col, 16);
+ let r = (num >> 16) + amt;
+ if (r > 255) r = 255; else if (r < 0) r = 0;
+ let b = ((num >> 8) & 0x00FF) + amt;
+ if (b > 255) b = 255; else if (b < 0) b = 0;
+ let g = (num & 0x0000FF) + amt;
+ if (g > 255) g = 255; else if (g < 0) g = 0;
+ return (usePound ? '#' : '') + (g | (b << 8) | (r << 16)).toString(16).padStart(6, '0');
+ } catch {
+ return col;
+ }
+ }
+
+ function applyDynamicTheme(branding) {
+ if (!branding) return;
+ const primary = branding.primaryColor || '#0b63e5';
+ const darkPrimary = adjustHexColor(primary, -25);
+ const root = document.documentElement;
+
+ root.style.setProperty('--ypg-blue', primary);
+ root.style.setProperty('--ypg-blue-dark', darkPrimary);
+
+ const theme = branding.headerTheme || 'gradient';
+ const headerEl = document.querySelector('.ypg-header');
+ const sidebarEl = document.querySelector('.ypg-pc-sidebar');
+
+ if (theme === 'solid') {
+ root.style.setProperty('--ypg-header-gradient', primary);
+ if (headerEl) headerEl.style.background = primary;
+ if (sidebarEl) sidebarEl.style.background = primary;
+ } else if (theme === 'dark') {
+ root.style.setProperty('--ypg-header-gradient', '#0b1329');
+ if (headerEl) headerEl.style.background = '#0b1329';
+ if (sidebarEl) sidebarEl.style.background = 'linear-gradient(180deg, #0b1329 0%, #030712 100%)';
+ } else {
+ const grad = `linear-gradient(180deg, ${primary} 0%, ${darkPrimary} 100%)`;
+ root.style.setProperty('--ypg-header-gradient', grad);
+ if (headerEl) headerEl.style.background = grad;
+ if (sidebarEl) sidebarEl.style.background = grad;
+ }
+ }
+
+ function renderHelpLegalLinks(branding) {
+ const container = document.getElementById('helpModalLegalLinks');
+ if (!container) return;
+ let html = '';
+ if (branding.privacyPolicyUrl) {
+ html += `<a href="${branding.privacyPolicyUrl}" target="_blank" rel="noopener noreferrer" style="color:var(--ypg-blue); text-decoration:underline; font-weight:600;">Privacy Policy &nearr;</a>`;
+ }
+ if (branding.termsUrl) {
+ html += `<a href="${branding.termsUrl}" target="_blank" rel="noopener noreferrer" style="color:var(--ypg-blue); text-decoration:underline; font-weight:600;">Terms of Service &nearr;</a>`;
+ }
+ container.innerHTML = html;
+ container.style.display = html ? 'flex' : 'none';
+ }
+
+ function renderState(data) {
+ state = data;
+ const b = data.branding || {};
+ const appName = b.appName || 'YouPI PG';
+ const merchantName = data.merchantName || 'Merchant Checkout';
+
+ // 1. Dynamic Title
+ document.title = `${merchantName} · ${appName} Checkout`;
+
+ // 2. Dynamic Colors & Header Theme
+ applyDynamicTheme(b);
+
+ // 3. Merchant Name & Avatar
+ if (data.merchantName) {
+ const nameEl = document.getElementById('headerMerchantName');
+ if (nameEl) nameEl.textContent = data.merchantName;
+ const letterEl = document.getElementById('merchantAvatarLetter');
+ if (letterEl) letterEl.textContent = data.merchantName.charAt(0).toUpperCase();
+ }
+
+ // 4. Verified Trust Badge
+ const trustBadgeEl = document.getElementById('headerTrustBadge');
+ const trustBadgeTextEl = document.getElementById('headerTrustBadgeText');
+ if (trustBadgeEl) {
+ if (b.hideTrustBadge) {
+ trustBadgeEl.style.display = 'none';
+ } else {
+ trustBadgeEl.style.display = 'inline-flex';
+ if (trustBadgeTextEl) {
+ trustBadgeTextEl.textContent = b.trustBadgeText || `${appName} Verified Business`;
+ }
+ }
+ }
+
+ // 5. Header Sub-banner
+ const subbannerEl = document.getElementById('headerSubbanner');
+ const subbannerTextEl = document.getElementById('subbannerText');
+ if (subbannerEl) {
+ if (b.hideSubbanner) {
+ subbannerEl.style.display = 'none';
+ } else {
+ subbannerEl.style.display = 'flex';
+ if (subbannerTextEl) {
+ subbannerTextEl.textContent = b.subbannerText || b.sidebarNotice || `Verified & Protected by ${appName}`;
+ }
+ }
+ }
+
+ // 6. Time-Saving Conversion Banner
+ if (b.hideTimeSavingBanner) {
+ document.querySelectorAll('.ypg-time-saving-banner').forEach(el => el.style.setProperty('display', 'none', 'important'));
+ }
+
+
+ // Update PC Sidebar Details & Whitelabeling
+ const pcMerchant = document.getElementById('pcMerchantName');
+ if (pcMerchant) pcMerchant.textContent = data.merchantName || 'Merchant Name';
+ const pcPrice = document.getElementById('pcPriceSummaryAmount');
+ if (pcPrice && data.amount) pcPrice.textContent = `₹${data.amount}`;
+ const pcAppBrand = document.getElementById('pcAppNameBrand');
+ if (pcAppBrand) pcAppBrand.textContent = appName;
+
+ // PC Avatar Logo Whitelabel
+ const pcLogoImg = document.getElementById('pcMerchantAvatarImg');
+ const pcLetterSpan = document.getElementById('pcMerchantAvatarLetter');
+ if (b.logoUrl) {
+ if (pcLogoImg) {
+ pcLogoImg.src = b.logoUrl;
+ pcLogoImg.style.display = 'block';
+ }
+ if (pcLetterSpan) pcLetterSpan.style.display = 'none';
+ } else {
+ if (pcLogoImg) pcLogoImg.style.display = 'none';
+ if (pcLetterSpan) {
+ pcLetterSpan.style.display = 'block';
+ pcLetterSpan.textContent = (data.merchantName || 'M').charAt(0).toUpperCase();
+ }
+ }
+
+ // PC Trust Badge Whitelabel
+ const pcTrust = document.getElementById('pcTrustBadge');
+ const pcTrustText = document.getElementById('pcTrustBadgeText');
+ if (pcTrust) {
+ if (b.hideTrustBadge) {
+ pcTrust.style.display = 'none';
+ } else {
+ pcTrust.style.display = 'inline-flex';
+ if (pcTrustText) {
+ pcTrustText.textContent = b.trustBadgeText || `${appName} Trusted Business`;
+ }
+ }
+ }
+
+ // PC Social Proof / Subbanner Whitelabel
+ const pcSocialWrap = document.getElementById('pcSocialProofWrap');
+ const pcSocialProof = document.getElementById('pcSocialProofText');
+ if (pcSocialWrap) {
+ if (b.hideSubbanner) {
+ pcSocialWrap.style.display = 'none';
+ } else {
+ pcSocialWrap.style.display = 'flex';
+ if (pcSocialProof) {
+ pcSocialProof.textContent = b.subbannerText || b.sidebarNotice || `500+ orders last month from ${data.merchantName || 'Merchant'}`;
+ }
+ }
+ }
+
+ const pcCustomerText = document.getElementById('pcCustomerProfileText');
+ if (pcCustomerText) {
+ const c = data.customer || activeCustomer || (typeof getPreservedIdentity === 'function' ? getPreservedIdentity()?.customer : null);
+ if (c) {
+ const name = c.fullName || c.name || c.customerName || '';
+ const mobile = c.mobileNumber || c.phoneNumber || '';
+ const clean = mobile ? String(mobile).replace(/^\+91/, '').replace(/\D/g, '').slice(-10) : '';
+ if (name && clean) {
+ pcCustomerText.textContent = `Using as ${name} (+91 ${clean})`;
+ } else if (name) {
+ pcCustomerText.textContent = `Using as ${name}`;
+ } else if (clean) {
+ pcCustomerText.textContent = `Using as +91 ${clean}`;
+ } else if (c.email) {
+ pcCustomerText.textContent = `Using as ${c.email}`;
+ } else {
+ pcCustomerText.textContent = 'Using as Guest User';
+ }
+ } else {
+ pcCustomerText.textContent = 'Using as Guest User';
+ }
+ }
+ document.querySelectorAll('.cancelMerchantNameSpan').forEach(el => {
+ el.textContent = data.merchantName || 'Merchant';
+ });
+
+ // Update UPI Server Health Indicator (3-Tier Model: Green, Yellow, Red)
+ const serverIndicator = data.upiServerIndicator || { status: 'WORKING', tier: 'GREEN', label: 'Server Is Working', isWorking: true };
+ const healthPill = document.getElementById('upiServerHealthIndicator');
+ const healthLabel = document.getElementById('serverStatusLabel');
+ const downtimeAlert = document.getElementById('serverDowntimeAlert');
+ const downtimeHeading = document.querySelector('#serverDowntimeAlert .ypg-downtime-heading');
+ const downtimeMsg = document.getElementById('serverDowntimeMsg');
+ const btnCashAlt = document.getElementById('btnDowntimeCashAlt');
+
+ const tier = serverIndicator.tier || (serverIndicator.status === 'WORKING' ? 'GREEN' : (serverIndicator.status === 'BUSY' ? 'YELLOW' : 'RED'));
+
+ if (healthPill) {
+ if (tier === 'GREEN') {
+ healthPill.className = 'ypg-server-health-indicator is-working';
+ } else if (tier === 'YELLOW') {
+ healthPill.className = 'ypg-server-health-indicator is-busy';
+ } else {
+ healthPill.className = 'ypg-server-health-indicator is-issues';
+ }
+ }
+ if (healthLabel) {
+ healthLabel.textContent = serverIndicator.label || (tier === 'GREEN' ? 'Server Is Working' : (tier === 'YELLOW' ? 'UPI Server Busy' : 'Server Facing Issues'));
+ }
+ if (downtimeAlert) {
+ if (tier === 'GREEN') {
+ downtimeAlert.style.display = 'none';
+ } else if (tier === 'YELLOW') {
+ downtimeAlert.style.display = 'flex';
+ downtimeAlert.className = 'ypg-downtime-alert is-warning';
+ if (downtimeHeading) downtimeHeading.textContent = 'UPI Server Busy';
+ if (downtimeMsg) {
+ downtimeMsg.textContent = serverIndicator.warning || 'Your payment may go through extra processing seconds.';
+ }
+ if (btnCashAlt) {
+ btnCashAlt.style.display = (serverIndicator.recommendedAlternative === 'CASH') ? 'inline-block' : 'none';
+ }
+ } else {
+ // RED
+ downtimeAlert.style.display = 'flex';
+ downtimeAlert.className = 'ypg-downtime-alert';
+ if (downtimeHeading) downtimeHeading.textContent = 'UPI Server Facing Issues';
+ if (downtimeMsg) {
+ downtimeMsg.textContent = serverIndicator.warning || 'It is not recommended to pay by UPI right now as the server is facing downtime. We recommend paying by cash instead.';
+ }
+ if (btnCashAlt) {
+ btnCashAlt.style.display = (serverIndicator.recommendedAlternative === 'CASH') ? 'inline-block' : 'none';
+ }
+ }
+ }
+
+ // CRITICAL INVARIANT: In all 3 states (Green, Yellow, Red), ALWAYS display the dynamic UPI QR code
+ const upiCardContainer = document.querySelector('.ypg-qr-card-container');
+ if (upiCardContainer) upiCardContainer.style.display = 'flex';
+ const qrContainerBox = document.getElementById('qrImageContainer');
+ if (qrContainerBox && (!qrContainerBox.querySelector('svg') || qrContainerBox.textContent.includes('Loading QR'))) {
+ loadQrSvg();
+ }
+
+ // 7. Amount & Order IDs
+ if (data.amount) {
+ const formatted = `₹${data.amount}`;
+ const bAmt = document.getElementById('bottomAmountDisplay');
+ if (bAmt) bAmt.textContent = formatted;
+ const dAmt = document.getElementById('drawerTotalAmount');
+ if (dAmt) dAmt.textContent = formatted;
+ const sAmt = document.getElementById('successAmountVal');
+ if (sAmt) sAmt.textContent = formatted;
+ }
+ if (data.orderId || data.id) {
+ const oId = data.orderId || data.id;
+ const dOrder = document.getElementById('drawerOrderId');
+ if (dOrder) dOrder.textContent = oId;
+ const formattedOId = String(oId).startsWith('#') ? oId : `#${oId}`;
+ document.querySelectorAll('.cashierOrderIdDisplay').forEach(el => {
+ el.textContent = formattedOId;
+ });
+ }
+ // Note: Payee VPA / UPI ID is strictly hidden from customer view in all screens
+
+ // 8. In-Store Offline Counter Instructions
+ const cashNoticeEl = document.getElementById('cashCounterDeskNotice');
+ if (cashNoticeEl && b.counterInstructions) {
+ cashNoticeEl.textContent = b.counterInstructions;
+ }
+ const cardNoticeEl = document.getElementById('cardCounterDeskNotice');
+ if (cardNoticeEl && b.cardCounterInstructions) {
+ cardNoticeEl.textContent = b.cardCounterInstructions;
+ }
+
+ // 9. Dynamic Logo
+ const logoImg = document.getElementById('merchantAvatarImg');
+ const letterSpan = document.getElementById('merchantAvatarLetter');
+ if (b.logoUrl) {
+ if (logoImg) {
+ logoImg.src = b.logoUrl;
+ logoImg.style.display = 'block';
+ }
+ if (letterSpan) letterSpan.style.display = 'none';
+ } else {
+ if (logoImg) logoImg.style.display = 'none';
+ if (letterSpan) letterSpan.style.display = 'block';
+ }
+
+ // 10. Splash Screen Branding
+ const splashLogo = document.getElementById('splashBrandLogo');
+ const splashDefaultShield = document.getElementById('splashDefaultShield');
+ const splashSecuredText = document.getElementById('splashSecuredByText');
+ if (b.logoUrl && splashLogo) {
+ splashLogo.src = b.logoUrl;
+ splashLogo.style.display = 'block';
+ if (splashDefaultShield) splashDefaultShield.style.display = 'none';
+ }
+ if (splashSecuredText) {
+ splashSecuredText.textContent = b.hidePoweredBy ? (b.appName || '256-Bit Bank Encryption') : (b.appName || 'YouPI PG');
+ }
+
+ // 12. Legal Disclaimer Strip
+ const legalDisclaimerEl = document.getElementById('footerLegalDisclaimer');
+ if (legalDisclaimerEl) {
+ if (b.hidePoweredBy) {
+ legalDisclaimerEl.innerHTML = `Secured by 256-Bit Bank Encryption &bull; <span style="text-decoration:underline; cursor:pointer;" onclick="openHelpModal()">Privacy &amp; Terms</span>`;
+ } else {
+ legalDisclaimerEl.innerHTML = `Secured by ${appName} &amp; 256-Bit Bank Encryption &bull; <span style="text-decoration:underline; cursor:pointer;" onclick="openHelpModal()">Privacy Notice</span>`;
+ }
+ }
+
+ // 13. Success Screen Description
+ const successDesc = document.getElementById('successDescText');
+ if (successDesc) {
+ successDesc.textContent = b.hidePoweredBy
+ ? 'Your payment was automatically verified by banking observers.'
+ : `Your payment was automatically verified by ${appName} banking observers.`;
+ }
+
+ // 14. Help Modal Title & Legal Links
+ const helpTitle = document.getElementById('helpModalTitle');
+ if (helpTitle) helpTitle.textContent = `${appName} Help & Support`;
+ renderHelpLegalLinks(b);
+
+ // 1. Conflict detection: single active payment session invariant
+ const conflictModal = document.getElementById('activeConflictModal');
+ if (data.hasActiveSessionConflict) {
+ hideAllContent();
+ if (conflictModal) conflictModal.hidden = false;
+ if (data.conflictingSession && data.conflictingSession.id) {
+ activeConflictSessionId = data.conflictingSession.id;
+ const desc = document.getElementById('conflictModalDesc');
+ if (desc) {
+ desc.textContent = `You already have an active payment session (#${data.conflictingSession.orderId || data.conflictingSession.id.slice(0, 8)}) for ₹${data.conflictingSession.amount}. For security, only one active payment session is allowed per customer.`;
+ }
+ }
+ if (queuePollTimer) { clearInterval(queuePollTimer); queuePollTimer = null; }
+ if (countdownTimer) { clearInterval(countdownTimer); countdownTimer = null; }
+ return;
+ } else {
+ if (conflictModal) conflictModal.hidden = true;
+ }
+
+ // --- 2. TERMINAL PAYMENT STATES (TERMINAL & OVER) ---
+ if (['SUCCESS', 'CANCELLED', 'EXPIRED', 'FAILED'].includes(data.status)) {
+ if (queuePollTimer) { clearInterval(queuePollTimer); queuePollTimer = null; }
+ if (countdownTimer) { clearInterval(countdownTimer); countdownTimer = null; }
+ setGatewayAuthMode(false);
+
+ if (data.status === 'CANCELLED') {
+ emitTriggerHook('CANCEL_CONFIRMED');
+ closeProcessingModal();
+ closeCancelPaymentModal();
+ const bar = document.getElementById('paymentMethodSelectorBar');
+ if (bar) bar.style.display = 'none';
+
+ hideAllContent();
+ const failedView = document.getElementById('viewFailedContent');
+ if (failedView) failedView.hidden = false;
+ const footer = document.getElementById('ypgStickyFooter');
+ if (footer) footer.style.display = 'none';
+
+ const titleEl = document.getElementById('failedTitleText');
+ if (titleEl) titleEl.textContent = 'Payment Cancelled';
+ const descEl = document.getElementById('failedDescriptionText');
+ if (descEl) descEl.textContent = 'You cancelled this payment session. Click below to return to the store.';
+
+ const btnAction = document.getElementById('btnFailedAction');
+ if (btnAction) {
+ btnAction.textContent = 'Back to Store';
+ btnAction.onclick = () => {
+ const storeUrl = getStoreReturnUrl();
+ const isEmbedded = (window.parent && window.parent !== window) || location.search.includes('embedded=1');
+ if (isEmbedded) {
+ try {
+ postToMerchant({ event: 'youpi.modal_close', redirectUrl: storeUrl });
+ postToMerchant({ event: 'youpi.close', redirectUrl: storeUrl });
+ postToMerchant({ event: 'youpi.cancel', redirectUrl: storeUrl });
+ postToMerchant({ event: 'youpi.payment_cancelled', redirectUrl: storeUrl });
+ } catch (e) {}
+ } else if (storeUrl && storeUrl !== '#' && sanitizeRedirectUrl(storeUrl)) {
+ window.location.href = sanitizeRedirectUrl(storeUrl);
+ } else {
+ window.history.back();
+ }
+ };
+ }
+ return;
+ }
+
+ if (data.status === 'FAILED' || data.status === 'EXPIRED') {
+ closeProcessingModal();
+ closeCancelPaymentModal();
+ const bar = document.getElementById('paymentMethodSelectorBar');
+ if (bar) bar.style.display = 'none';
+
+ hideAllContent();
+ const failedView = document.getElementById('viewFailedContent');
+ if (failedView) failedView.hidden = false;
+ const footer = document.getElementById('ypgStickyFooter');
+ if (footer) footer.style.display = 'none';
+ emitTriggerHook('PAYMENT_FAILED', { status: data.status });
+
+ const isExpired = data.status === 'EXPIRED';
+ const titleEl = document.getElementById('failedTitleText');
+ if (titleEl) titleEl.textContent = isExpired ? 'Payment Session Expired' : 'Payment Not Confirmed';
+ const descEl = document.getElementById('failedDescriptionText');
+ if (descEl) {
+ descEl.textContent = isExpired
+ ? 'The 5-minute active payment window elapsed before verification was completed. For security, please return to the store and initiate a new order.'
+ : 'No matching UPI bank confirmation was detected during the verification window.';
+ }
+ const btnAction = document.getElementById('btnFailedAction');
+ if (btnAction) {
+ btnAction.textContent = isExpired ? 'Start New Payment' : 'Try Again';
+ btnAction.onclick = () => retryNewPayment();
+ }
+ return;
+ }
+
+ if (data.status === 'SUCCESS') {
+ closeProcessingModal();
+ closeCancelPaymentModal();
+ const bar = document.getElementById('paymentMethodSelectorBar');
+ if (bar) bar.style.display = 'none';
+
+ hideAllContent();
+ const successView = document.getElementById('viewSuccessContent');
+ if (successView) successView.hidden = false;
+ const footer = document.getElementById('ypgStickyFooter');
+ if (footer) footer.style.display = 'none';
+ const amtEl = document.getElementById('successAmountVal');
+ if (amtEl) amtEl.textContent = `₹${data.amount}`;
+
+ const isCodOrder = data.method === 'COD' || data.verificationTag === 'COD_ORDER_PLACED';
+ const titleEl = document.getElementById('successTitleText');
+ const descEl = document.getElementById('successDescText');
+ const codApiBox = document.getElementById('successCodDevApiBox');
+ const codApiUrl = document.getElementById('successCodDevApiUrl');
+
+ if (isCodOrder) {
+ if (titleEl) titleEl.textContent = 'Order Placed (Cash on Delivery)!';
+ if (descEl) descEl.textContent = `Your order #${data.orderId} was confirmed. Please keep ₹${data.amount} in cash ready for the courier agent upon arrival.`;
+ if (codApiBox && codApiUrl) {
+ codApiBox.style.display = 'block';
+ codApiUrl.textContent = `${API_BASE}/api/v1/orders/${data.orderId}/cod`;
+ }
+ } else {
+ if (titleEl) titleEl.textContent = 'Payment Successful!';
+ if (descEl) descEl.textContent = 'Your payment was automatically verified by YouPI PG banking observers.';
+ if (codApiBox) codApiBox.style.display = 'none';
+ }
+
+ emitTriggerHook('PAYMENT_COMPLETED', { amount: data.amount, method: data.method, isCod: isCodOrder });
+
+ try {
+ postToMerchant({
+ event: 'youpi.payment_success',
+ data: data
+ });
+ } catch (e) { }
+
+ const btnReturn = document.getElementById('btnReturnStore') || document.getElementById('btnReturnToStore');
+ if (btnReturn && data.returnUrl) {
+ btnReturn.hidden = false;
+ btnReturn.href = data.returnUrl;
+ if (window.parent === window) {
+ setTimeout(() => { window.location.href = data.returnUrl; }, 2000);
+ }
+ } else if (btnReturn) {
+ btnReturn.hidden = true;
+ }
+ return;
+ }
+ }
+
+ // Sync payment methods & modality layout immediately to match merchant settings
+ renderMethodSelectorBar(data);
+
+ // Auto-bind customer to payment session helper
+ if (typeof autoBindCustomerToSession !== 'function') {
+ window.autoBindCustomerToSession = async function(customerId, token) {
+ if (window.isAutoBindingSession || !PAYMENT_ID || !token || !customerId) return;
+ window.isAutoBindingSession = true;
+ try {
+ const res = await fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/bind-customer`, {
+ method: 'POST',
+ headers: getApiHeaders({
+ 'Content-Type': 'application/json',
+ 'Authorization': `Bearer ${token}`
+ }),
+ body: JSON.stringify({ token, customerId })
+ });
+ if (res.ok) {
+ const boundData = await res.json().catch(() => null);
+ if (boundData && boundData.status && boundData.status !== 'INITIATED') {
+ loadState();
+ }
+ } else if (res.status === 401) {
+ clearPreservedIdentity();
+ activeCustomer = null;
+ activeToken = null;
+ setGatewayAuthMode(true);
+ goToAuthStep('mobile');
+ renderSavedAccountsUI();
+ }
+ } catch (e) {
+ } finally {
+ window.isAutoBindingSession = false;
+ }
+ };
+ }
+
+ // 3. Pre-Gateway Customer Authentication Gate (Preserves identity up to 1 week)
+ if (data.customer) {
+ activeCustomer = data.customer;
+ } else if (!activeCustomer) {
+ const preserved = getPreservedIdentity();
+ if (preserved && preserved.customer) {
+ activeCustomer = preserved.customer;
+ activeToken = preserved.token || activeToken;
+ }
+ }
+ const hasCustomer = Boolean(data.customer || activeCustomer);
+
+ if (!hasCustomer) {
+ setGatewayAuthMode(true);
+ const stepMob = document.getElementById('authStepMobile');
+ const isAlreadyOnMobile = stepMob && !stepMob.hidden;
+ if (!currentAuthStep || (currentAuthStep === 'mobile' && !isAlreadyOnMobile)) {
+ goToAuthStep('mobile');
+ }
+ renderSavedAccountsUI();
+ return;
+ }
+
+ // Authenticated Customer
+ if (activeCustomer) {
+ updateCustomerDisplay(activeCustomer);
+ // Automatically bind customer to the payment session if session is INITIATED or unbound
+ if (activeToken && PAYMENT_ID && (!data.customer || data.status === 'INITIATED')) {
+ window.autoBindCustomerToSession(activeCustomer.id, activeToken);
+ }
+ }
+ setGatewayAuthMode(false);
+
+ // Render psychological time-saving conversion banner
+ renderTimeSavingBanners(data);
+
+ // 3. Status Handling: BLOCKED, QUEUED, ACTIVE, PROCESSING, SUCCESS, FAILED, EXPIRED
+ if (data.status === 'BLOCKED' || data.isBanned || data.fraudDetected) {
+ renderBlockedState(data);
+ return;
+ }
+
+ if (data.status === 'QUEUED') {
+ const bar = document.getElementById('paymentMethodSelectorBar');
+ if (bar) bar.style.display = 'none';
+
+ hideAllContent();
+ document.getElementById('viewQueuedContent').hidden = false;
+ document.getElementById('ypgStickyFooter').style.display = 'none';
+ document.getElementById('queueNumberDisplay').textContent = `#${data.queuePosition || 1}`;
+ runCountdown(data.timeRemainingSeconds, sec => {
+ document.getElementById('queueTimerText').textContent = formatTime(sec);
+ }, () => loadState());
+
+ if (!queuePollTimer) {
+ queuePollTimer = setInterval(() => { loadState(); }, 1200);
+ }
+ return;
+ }
+
+ if (queuePollTimer) {
+ clearInterval(queuePollTimer);
+ queuePollTimer = null;
+ }
+
+ if (data.status === 'ACTIVE' || (data.status === 'INITIATED' && (data.timeRemainingSeconds > 0 || data.expiresAt))) {
+ renderMethodSelectorBar(data);
+
+ hideAllContent();
+ if (currentSelectedMethod === 'COD') {
+ const vCod = document.getElementById('viewCodContent');
+ if (vCod) vCod.hidden = false;
+ document.getElementById('ypgStickyFooter').style.display = 'none';
+
+ runCountdown(data.timeRemainingSeconds || 300, sec => {
+ const el = document.getElementById('codTimerText');
+ if (el) el.textContent = formatTime(sec);
+ }, () => {
+ const el = document.getElementById('codTimerText');
+ if (el) el.textContent = 'Expired';
+ fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/expire`, { method: 'POST' }).catch(() => { });
+ loadState();
+ }, data.expiresAt);
+ } else if (currentSelectedMethod === 'CASH_COUNTER') {
+ const vCash = document.getElementById('viewCashContent');
+ if (vCash) vCash.hidden = false;
+ document.getElementById('ypgStickyFooter').style.display = 'none';
+ } else if (currentSelectedMethod === 'CARD_COUNTER') {
+ const vCard = document.getElementById('viewCardContent');
+ if (vCard) vCard.hidden = false;
+ document.getElementById('ypgStickyFooter').style.display = 'none';
+ } else {
+ // Default: UPI QR (CRITICAL INVARIANT: ALWAYS SHOWS QR AND RUNS 5:00 TIMER REGARDLESS OF GREEN/YELLOW/RED)
+ document.getElementById('viewUpiContent').hidden = false;
+ document.getElementById('ypgStickyFooter').style.display = 'flex';
+ loadQrSvg();
+
+ const btnContinue = document.getElementById('btnContinuePayment');
+
+ const remainingSec = (data.timeRemainingSeconds !== undefined && data.timeRemainingSeconds !== null) ? Number(data.timeRemainingSeconds) : 300;
+ const targetExpiry = data.expiresAt || new Date(Date.now() + remainingSec * 1000).toISOString();
+
+ if (remainingSec <= 0 && data.status === 'ACTIVE') {
+ const tText = document.getElementById('activeTimerText');
+ if (tText) tText.textContent = 'Expired';
+ if (btnContinue) btnContinue.style.display = 'none';
+ return;
+ }
+
+ // Initialize button hidden until 4:00 has elapsed
+ if (btnContinue && remainingSec > 240) {
+ btnContinue.style.display = 'none';
+ } else if (btnContinue) {
+ btnContinue.style.display = 'block';
+ }
+
+ runCountdown(remainingSec, sec => {
+ const tText = document.getElementById('activeTimerText');
+ if (tText) tText.textContent = formatTime(sec);
+
+ // "Payment Done — Check My Payment" button appears after 4:00
+ if (sec <= 240 && btnContinue) {
+ btnContinue.style.display = 'block';
+ } else if (btnContinue) {
+ btnContinue.style.display = 'none';
+ }
+ }, () => {
+ const tText = document.getElementById('activeTimerText');
+ if (tText) tText.textContent = 'Expired';
+ if (btnContinue) btnContinue.style.display = 'none';
+ closeProcessingModal();
+ fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/expire`, { method: 'POST' }).catch(() => { });
+ loadState();
+ }, targetExpiry);
+ }
+ return;
+ }
+
+ if (data.status === 'PROCESSING') {
+ const bar = document.getElementById('paymentMethodSelectorBar');
+ if (bar) bar.style.display = 'none';
+
+ hideAllContent();
+ document.getElementById('viewProcessingContent').hidden = false;
+ document.getElementById('ypgStickyFooter').style.display = 'none';
+ runCountdown(data.timeRemainingSeconds, sec => {
+ document.getElementById('procTimerText').textContent = `${sec}s`;
+ }, () => loadState(), data.expiresAt);
+ return;
+ }
+
+
+ }
+
+ // --- PSYCHOLOGICAL TIME-SAVING CONVERSION BANNER ---
+ // Invariant: ONLY display when the customer is actively waiting in the UPI queue!
+ function renderTimeSavingBanners(data) {
+ const qBanner = document.getElementById('queueTimeSavingBanner');
+ const uBanner = document.getElementById('upiTimeSavingBanner');
+ if (uBanner) uBanner.style.display = 'none'; // NEVER show on active UPI session
+
+ const isQueued = (data.status === 'QUEUED') || (Number(data.queuePosition || 0) > 0);
+ if (!isQueued || !qBanner) {
+ if (qBanner) qBanner.style.display = 'none';
+ return;
+ }
+
+ const enabled = data.enabledMethods || ['UPI'];
+ const sellingMode = data.sellingMode || 'OFFLINE';
+
+ let altMethodCode = null;
+ let altMethodName = null;
+
+ if (sellingMode === 'ONLINE') {
+ if (enabled.includes('COD')) {
+ altMethodCode = 'COD';
+ altMethodName = 'Cash on Delivery';
+ } else if (enabled.includes('CARD')) {
+ altMethodCode = 'CARD';
+ altMethodName = 'Credit / Debit Card';
+ } else if (enabled.includes('NETBANKING')) {
+ altMethodCode = 'NETBANKING';
+ altMethodName = 'Net Banking';
+ } else if (enabled.includes('CARD_COUNTER')) {
+ altMethodCode = 'CARD_COUNTER';
+ altMethodName = 'Card Payment';
+ }
+ } else {
+ // Offline selling mode (retail till / counter)
+ if (enabled.includes('CASH_COUNTER')) {
+ altMethodCode = 'CASH_COUNTER';
+ altMethodName = 'Cash at Till';
+ } else if (enabled.includes('CARD_COUNTER')) {
+ altMethodCode = 'CARD_COUNTER';
+ altMethodName = 'Card at Counter';
+ }
+ }
+
+ // CRITICAL REQUIREMENT: If both cash and card are disabled by merchant, psychological conversion MUST NOT BE SHOWN!
+ if (!altMethodCode || (!enabled.includes('COD') && !enabled.includes('CASH_COUNTER') && !enabled.includes('CARD') && !enabled.includes('CARD_COUNTER'))) {
+ qBanner.style.display = 'none';
+ return;
+ }
+
+ const timeSec = data.timeRemainingSeconds || (data.queuePosition ? data.queuePosition * 30 : 30);
+ const initialTimerStr = formatTime(timeSec);
+
+ const actionText = sellingMode === 'ONLINE'
+ ? `Skip the UPI wait! Pay with <strong>${altMethodName}</strong> instantly without waiting for a UPI slot.`
+ : `Don't wait in the UPI line! Pay with <strong>${altMethodName}</strong> right now to complete your purchase instantly.`;
+
+ qBanner.innerHTML = `
+ <div class="ypg-queue-conversion-card">
+ <div class="ypg-queue-conversion-header">
+ <div class="ypg-queue-conversion-badge"><svg width="12" height="12" viewBox="0 0 24 24" fill="currentColor" style="display:inline-block; vertical-align:middle; margin-right:3px;"><path d="M12 2l2.4 7.2h7.6l-6 4.8 2.4 7.2-6.4-4.8-6.4 4.8 2.4-7.2-6-4.8h7.6z"/></svg>Instant Alternative</div>
+ <div class="ypg-queue-conversion-pill">Save <span class="ypg-banner-running-timer">${initialTimerStr}</span></div>
+ </div>
+ <div class="ypg-queue-conversion-text">
+ ${actionText}
+ </div>
+ <button type="button" class="ypg-btn-queue-switch" onclick="selectPaymentMethod('${altMethodCode}')">
+ <span>Pay with ${altMethodName}</span>
+ <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><polyline points="9 18 15 12 9 6"/></svg>
+ </button>
+ </div>
+ `;
+ qBanner.style.display = 'block';
+ }
+
+ // --- PURE QR SVG LOADER (ALWAYS LOADS & SHOWS QR CODE REGARDLESS OF SERVER STATUS) ---
+ async function loadQrSvg() {
+ const container = document.getElementById('qrImageContainer');
+ if (!container) return;
+ try {
+ const res = await fetch(`/checkout/${PAYMENT_ID}/qr.svg`);
+ if (res.ok) {
+ const svg = await res.text();
+ if (svg && svg.includes('<svg')) {
+ container.innerHTML = svg;
+ return;
+ }
+ }
+ } catch (e) {
+ console.warn('loadQrSvg error:', e);
+ }
+ // Automatic retry to guarantee QR code is loaded
+ setTimeout(async () => {
+ try {
+ const res = await fetch(`/checkout/${PAYMENT_ID}/qr.svg`);
+ if (res.ok) {
+ const svg = await res.text();
+ if (svg && svg.includes('<svg')) {
+ container.innerHTML = svg;
+ }
+ }
+ } catch (e) {}
+ }, 400);
+ }
+
+ function copyVpaId() {
+ const vpa = (state?.payeeVpa || document.getElementById('displayVpaVal')?.textContent || '').trim();
+ if (!vpa) return;
+
+ const btn = document.getElementById('btnCopyVpa');
+ const text = document.getElementById('copyVpaText');
+ if (btn && text) {
+ navigator.clipboard.writeText(vpa).then(() => {
+ text.textContent = 'Copied!';
+ btn.classList.add('copied');
+ setTimeout(() => {
+ text.textContent = 'Copy';
+ btn.classList.remove('copied');
+ }, 2000);
+ }).catch(() => {
+ text.textContent = 'Copied!';
+ setTimeout(() => { text.textContent = 'Copy'; }, 2000);
+ });
+ }
+ }
+
+ // --- RAZORPAY AUTHENTIC "PROCESSING YOUR PAYMENT" MODAL SHEET ---
+ function openProcessingModal(title = 'Processing your payment', subtitle = 'This will only take a few seconds.') {
+ const modal = document.getElementById('rzpProcessingModal');
+ const tEl = document.getElementById('rzpProcessingTitle');
+ const sEl = document.getElementById('rzpProcessingSubtitle');
+ if (tEl) tEl.textContent = title;
+ if (sEl) sEl.textContent = subtitle;
+ if (modal) {
+ modal.hidden = false;
+ requestAnimationFrame(() => modal.classList.add('active'));
+ }
+ }
+
+ function closeProcessingModal() {
+ const modal = document.getElementById('rzpProcessingModal');
+ if (modal) {
+ modal.classList.remove('active');
+ setTimeout(() => { modal.hidden = true; }, 300);
+ }
+ }
+
+ // --- RAZORPAY AUTHENTIC "CANCEL PAYMENT" CONFIRMATION MODAL SHEET ---
+ function openCancelPaymentModal() {
+ emitTriggerHook('CANCEL_CLICKED');
+ document.body.style.overflow = 'hidden';
+ const yBody = document.querySelector('.ypg-body');
+ if (yBody) yBody.style.overflow = 'hidden';
+
+ const modal = document.getElementById('rzpCancelModal');
+ const promptView = document.getElementById('rzpCancelPromptView');
+ const progressView = document.getElementById('rzpCancellingProgressView');
+ if (promptView) promptView.style.display = 'flex';
+ if (progressView) progressView.style.display = 'none';
+ if (modal) {
+ modal.hidden = false;
+ requestAnimationFrame(() => modal.classList.add('active'));
+ }
+ }
+
+ function closeCancelPaymentModal() {
+ document.body.style.overflow = '';
+ const yBody = document.querySelector('.ypg-body');
+ if (yBody) yBody.style.overflow = '';
+
+ const modal = document.getElementById('rzpCancelModal');
+ if (modal) {
+ modal.classList.remove('active');
+ setTimeout(() => { modal.hidden = true; }, 300);
+ }
+ }
+
+ async function confirmCancelPayment() {
+ emitTriggerHook('CANCEL_CONFIRMED');
+ if (countdownTimer) { clearInterval(countdownTimer); countdownTimer = null; }
+ if (queuePollTimer) { clearInterval(queuePollTimer); queuePollTimer = null; }
+
+ const promptView = document.getElementById('rzpCancelPromptView');
+ const progressView = document.getElementById('rzpCancellingProgressView');
+ if (promptView) promptView.style.display = 'none';
+ if (progressView) progressView.style.display = 'flex';
+
+ try {
+ await fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/cancel`, {
+ method: 'POST',
+ headers: getApiHeaders()
+ });
+ } catch (e) { }
+
+ // Smooth realistic delay matching Razorpay transition
+ setTimeout(() => {
+ document.body.style.overflow = '';
+ const yBody = document.querySelector('.ypg-body');
+ if (yBody) yBody.style.overflow = '';
+
+ closeCancelPaymentModal();
+ closeProcessingModal();
+
+ const storeUrl = getStoreReturnUrl();
+ const isEmbedded = (window.parent && window.parent !== window) || location.search.includes('embedded=1');
+
+ if (isEmbedded) {
+ try {
+ postToMerchant({ event: 'youpi.modal_close', redirectUrl: storeUrl });
+ postToMerchant({ event: 'youpi.close', redirectUrl: storeUrl });
+ postToMerchant({ event: 'youpi.cancel', redirectUrl: storeUrl });
+ postToMerchant({ event: 'youpi.payment_cancelled', redirectUrl: storeUrl });
+ } catch (e) { }
+ }
+
+ loadState();
+ }, 500);
+ }
+
+ // Direct invocation from header back button, close button, or inline links
+ function cancelPayment() {
+ openCancelPaymentModal();
+ }
+
+ // Listen for cancel requests from parent iframe cross button
+ window.addEventListener('message', (e) => {
+ if (!e.data || e.data.event !== 'youpi.request_cancel') return;
+ // Only the storefront that embeds this iframe may drive it.
+ const merchantOrigin = resolveMerchantOrigin();
+ if (merchantOrigin && e.origin !== merchantOrigin) return;
+ openCancelPaymentModal();
+ });
+
+ // --- MANUAL PAYMENT DONE VERIFICATION ---
+ async function submitPaymentDone() {
+ openProcessingModal('Processing your payment', 'This will only take a few seconds.');
+ emitTriggerHook('PAYMENT_DONE_CLICKED');
+
+ try {
+ const res = await fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/payment-done`, {
+ method: 'POST'
+ });
+ if (res.ok) {
+ loadState();
+ }
+ } catch (e) { }
+ }
+
+ // --- SINGLE ACTIVE PAYMENT SESSION CONFLICT RESOLUTION ---
+ let activeConflictSessionId = null;
+
+ async function executeRetryNewPayment() {
+ try {
+ const btn = document.getElementById('btnEndSessionAndStartNew');
+ if (btn) {
+ btn.disabled = true;
+ btn.textContent = 'Activating new payment...';
+ }
+ const headers = { 'Content-Type': 'application/json' };
+ if (typeof DeviceIdentity !== 'undefined') {
+ if (DeviceIdentity.currentDeviceId) headers['X-Device-Id'] = DeviceIdentity.currentDeviceId;
+ if (DeviceIdentity.currentDeviceToken) headers['X-Device-Token'] = DeviceIdentity.currentDeviceToken;
+ }
+ const token = activeToken || localStorage.getItem('ypg_customer_token');
+ if (token) {
+ headers['Authorization'] = `Bearer ${token}`;
+ headers['x-customer-token'] = token;
+ }
+ const res = await fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/retry-new-payment`, {
+ method: 'POST',
+ headers
+ });
+ if (res.ok) {
+ const conflictModal = document.getElementById('activeConflictModal');
+ if (conflictModal) conflictModal.hidden = true;
+ await loadState();
+ } else {
+ const err = await res.json().catch(() => ({}));
+ alert(err.error || 'Failed to retry payment');
+ if (btn) {
+ btn.disabled = false;
+ btn.textContent = 'END SESSION & START NEW PAYMENT';
+ }
+ }
+ } catch (e) {
+ alert('Network error while retrying payment');
+ const btn = document.getElementById('btnEndSessionAndStartNew');
+ if (btn) {
+ btn.disabled = false;
+ btn.textContent = 'END SESSION & START NEW PAYMENT';
+ }
+ }
+ }
+
+ function continueExistingActivePayment() {
+ if (activeConflictSessionId) {
+ window.location.href = `/checkout/${activeConflictSessionId}`;
+ } else {
+ const conflictModal = document.getElementById('activeConflictModal');
+ if (conflictModal) conflictModal.hidden = true;
+ loadState();
+ }
+ }
+
+
+
+ function renderBlockedState(data) {
+ closeProcessingModal();
+ closeCancelPaymentModal();
+ if (countdownTimer) { clearInterval(countdownTimer); countdownTimer = null; }
+ if (queuePollTimer) { clearInterval(queuePollTimer); queuePollTimer = null; }
+
+ const bar = document.getElementById('paymentMethodSelectorBar');
+ if (bar) bar.style.display = 'none';
+ const footer = document.getElementById('ypgStickyFooter');
+ if (footer) footer.style.display = 'none';
+
+ hideAllContent();
+ const vBlocked = document.getElementById('viewBlockedContent');
+ if (vBlocked) vBlocked.hidden = false;
+
+ // Expand to full width on PC desktop split view
+ const pcMethodsList = document.getElementById('pcMethodsList');
+ if (pcMethodsList) pcMethodsList.style.setProperty('display', 'none', 'important');
+ const pcActiveView = document.querySelector('.ypg-pc-active-view');
+ if (pcActiveView) pcActiveView.style.width = '100%';
+ const pcTopbarTitle = document.querySelector('.ypg-pc-topbar-title');
+ if (pcTopbarTitle) pcTopbarTitle.textContent = 'Security Incident Investigation';
+
+ const orderRef = data.orderId || state?.orderId || 'N/A';
+ const orderEl = document.getElementById('blockedOrderRefVal');
+ if (orderEl) orderEl.textContent = `#${orderRef}`;
+
+ const sessEl = document.getElementById('blockedSessionIdVal');
+ if (sessEl) sessEl.textContent = data.id || PAYMENT_ID || 'N/A';
+
+ const timeEl = document.getElementById('blockedTimestampVal');
+ if (timeEl) timeEl.textContent = new Date().toLocaleTimeString();
+
+ if (data.blockedNotice?.title) {
+ const titleEl = document.getElementById('blockedTitleText');
+ if (titleEl) titleEl.textContent = data.blockedNotice.title;
+ }
+ if (data.blockedNotice?.subtitle) {
+ const subEl = document.getElementById('blockedSubtitleText');
+ if (subEl) subEl.textContent = data.blockedNotice.subtitle;
+ }
+ if (data.blockedNotice?.message) {
+ const msgEl = document.getElementById('blockedNoticeText');
+ if (msgEl) msgEl.textContent = data.blockedNotice.message;
+ }
+
+ emitTriggerHook('PAYMENT_BLOCKED', {
+ reason: data.reason || data.blockedNotice?.reason || 'FRAUDULENT_PAYMENT_INJECTION',
+ orderId: orderRef
+ });
+
+ try {
+ postToMerchant({
+ event: 'youpi.payment_blocked',
+ data: {
+ status: 'BLOCKED',
+ isBanned: true,
+ orderId: orderRef,
+ paymentId: data.id || PAYMENT_ID
+ }
+ });
+ } catch (e) {}
+ }
+
+ function handleBlockedExit() {
+ const failRedirectUrl = getFailedRedirectUrl();
+ if (window.parent && window.parent !== window) {
+ try {
+ postToMerchant({
+ event: 'youpi.payment_blocked_exit',
+ data: { status: 'BLOCKED', isBanned: true, orderId: state?.orderId }
+ });
+ } catch (e) {}
+ }
+ window.location.href = failRedirectUrl;
+ }
+
+ let lastDuplicateSessionPayload = null;
+ function renderDuplicateSession(data) {
+ lastDuplicateSessionPayload = data;
+ if (countdownTimer) { clearInterval(countdownTimer); countdownTimer = null; }
+ if (queuePollTimer) { clearInterval(queuePollTimer); queuePollTimer = null; }
+ if (sse) { try { sse.close(); } catch(e){} sse = null; }
+
+ const bar = document.getElementById('paymentMethodSelectorBar');
+ if (bar) bar.style.display = 'none';
+ const footer = document.getElementById('ypgStickyFooter');
+ if (footer) footer.style.display = 'none';
+
+ hideAllContent();
+ const vDup = document.getElementById('viewDuplicateSessionContent');
+ if (vDup) vDup.hidden = false;
+
+ // Expand to full width on PC desktop split view
+ const pcMethodsList = document.getElementById('pcMethodsList');
+ if (pcMethodsList) pcMethodsList.style.setProperty('display', 'none', 'important');
+ const pcActiveView = document.querySelector('.ypg-pc-active-view');
+ if (pcActiveView) pcActiveView.style.width = '100%';
+ const pcTopbarTitle = document.querySelector('.ypg-pc-topbar-title');
+ if (pcTopbarTitle) pcTopbarTitle.textContent = 'Device Security Verification';
+
+ emitTriggerHook('DUPLICATE_SESSION_DETECTED', {
+ sessionId: data.session_id || PAYMENT_ID,
+ action: 'END_SESSION_AND_CREATE_NEW'
+ });
+ }
+
+ async function handleEndAndStartNewPayment() {
+ const btn = document.getElementById('btnEndAndRestartPayment');
+ if (btn) {
+ btn.disabled = true;
+ btn.innerHTML = '<span class="ypg-spinner" style="display:inline-block;width:15px;height:15px;border:2px solid #fff;border-top-color:transparent;border-radius:50%;animation:rzpRotate 0.8s linear infinite;margin-right:8px;vertical-align:middle;"></span> CREATING NEW PAYMENT...';
+ }
+
+ try {
+ await DeviceIdentity.getOrRegister();
+ const headers = { 'Content-Type': 'application/json' };
+ if (DeviceIdentity.currentDeviceId) {
+ headers['X-Device-Id'] = DeviceIdentity.currentDeviceId;
+ }
+ if (DeviceIdentity.currentDeviceToken) {
+ headers['X-Device-Token'] = DeviceIdentity.currentDeviceToken;
+ }
+
+ const conflictingSessionId = lastDuplicateSessionPayload?.conflicting_session_id || activeConflictSessionId || null;
+
+ const res = await fetch(`${API_BASE}/api/v1/checkout/${PAYMENT_ID}/end-and-restart-payment`, {
+ method: 'POST',
+ headers,
+ body: JSON.stringify({
+ deviceId: DeviceIdentity.currentDeviceId,
+ conflictingSessionId
+ })
+ });
+
+ const data = await res.json();
+ if (data.success && (data.checkoutUrl || data.newSessionId)) {
+ const targetUrl = data.checkoutUrl || `/checkout/${data.newSessionId}`;
+ window.location.href = targetUrl;
+ } else {
+ alert(data.error || 'Failed to start new payment session. Please try again.');
+ if (btn) {
+ btn.disabled = false;
+ btn.innerHTML = '<span>END SESSION &amp; START NEW PAYMENT</span>';
+ }
+ }
+ } catch (err) {
+ alert('Network error while starting new payment session.');
+ if (btn) {
+ btn.disabled = false;
+ btn.innerHTML = '<span>END SESSION &amp; START NEW PAYMENT</span>';
+ }
+ }
+ }
+
+ // --- SSE REALTIME STREAM (AUTOMATIC OBSERVER & CASHIER INTEGRATION) ---
+ function connectSse() {
+ if (sse) {
+ try { sse.close(); } catch (e) { }
+ sse = null;
+ }
+
+ try {
+ const devQuery = DeviceIdentity.currentDeviceId ? `?deviceId=${encodeURIComponent(DeviceIdentity.currentDeviceId)}` : '';
+ const streamUrl = `${API_BASE}/api/v1/checkout/${PAYMENT_ID}/stream${devQuery}`;
+ sse = new EventSource(streamUrl);
+
+ const handleRealtimePayload = (event) => {
+ try {
+ const data = JSON.parse(event.data);
+ if (data && (data.error === 'DUPLICATE_SESSION_DETECTED' || data.type === 'DUPLICATE_SESSION_DETECTED')) {
+ renderDuplicateSession(data);
+ } else if (data && (data.status === 'BLOCKED' || data.isBanned || data.fraudDetected || data.type === 'FRAUD_BLOCKED' || data.event === 'account_blocked')) {
+ renderBlockedState(data);
+ } else if (data && data.type === 'STATE_CHANGE' && data.payload) {
+ renderState(data.payload);
+ } else if (data && data.status) {
+ if (data.status === 'BLOCKED' || data.isBanned) {
+ renderBlockedState(data);
+ } else {
+ loadState();
+ }
+ } else {
+ loadState();
+ }
+ } catch (e) {
+ loadState();
+ }
+ };
+
+ sse.onmessage = handleRealtimePayload;
+ sse.addEventListener('status_change', handleRealtimePayload);
+ sse.addEventListener('state_change', handleRealtimePayload);
+ sse.addEventListener('account_blocked', handleRealtimePayload);
+ sse.addEventListener('fraud_detected', handleRealtimePayload);
+ sse.addEventListener('counter_payment_confirmed', handleRealtimePayload);
+ sse.addEventListener('counter_payment_declined', handleRealtimePayload);
+ sse.addEventListener('payment_success', handleRealtimePayload);
+ sse.addEventListener('queue_cleared', handleRealtimePayload);
+ sse.addEventListener('session_expired', handleRealtimePayload);
+
+ sse.onerror = () => {
+ if (sse) {
+ try { sse.close(); } catch (e) { }
+ sse = null;
+ }
+ setTimeout(connectSse, 3000);
+ };
+ } catch (e) {
+ setTimeout(connectSse, 3000);
+ }
+ }
+
+ // Continuous 2-second fallback polling: guarantees instant response when cashier confirms/declines or queues clear
+ setInterval(() => {
+ if (state && (state.status === 'SUCCESS' || state.status === 'FAILED' || state.status === 'EXPIRED' || state.status === 'BLOCKED' || state.isBanned)) {
+ return;
+ }
+ loadState();
+ }, 2000);
+
+ // --- SUPPORT / HELP MODAL ---
+ function openHelpModal() {
+ document.getElementById('helpModal').hidden = false;
+ const popoverMobile = document.getElementById('accountMenuPopover');
+ if (popoverMobile) { popoverMobile.setAttribute('hidden', ''); popoverMobile.hidden = true; popoverMobile.style.display = 'none'; }
+ const popoverPc = document.getElementById('accountMenuPopoverPc');
+ if (popoverPc) { popoverPc.setAttribute('hidden', ''); popoverPc.hidden = true; popoverPc.style.display = 'none'; }
+
+ const branding = state?.branding || {};
+ renderSupportInfo({
+ supportEmails: branding.supportEmails || (branding.supportEmail ? [branding.supportEmail] : []),
+ supportMobiles: branding.supportMobiles || (branding.supportMobile ? [branding.supportMobile] : [])
+ });
+ }
+
+ function renderSupportInfo(info) {
+ const emailBox = document.getElementById('helpModalEmailsContainer');
+ const phoneBox = document.getElementById('helpModalMobilesContainer');
+ const caseBox = document.getElementById('helpModalCaseInfoText');
+
+ if (emailBox && info.supportEmails) {
+ emailBox.innerHTML = `<strong>Email:</strong> ${info.supportEmails.join(', ')}`;
+ }
+ if (phoneBox && info.supportMobiles) {
+ phoneBox.innerHTML = `<strong>Phone:</strong> ${info.supportMobiles.join(', ')}`;
+ }
+ if (caseBox) {
+ caseBox.textContent = `PAYMENT_ID: ${PAYMENT_ID}\nORDER_ID: ${state?.orderId || 'N/A'}\nAMOUNT: ₹${state?.amount || 'N/A'}\nSTATUS: ${state?.status || 'N/A'}`;
+ }
+ }
+
+ function closeHelpModal() {
+ document.getElementById('helpModal').hidden = true;
+ }
+
+ function copyCaseInfo() {
+ const text = document.getElementById('helpModalCaseInfoText').textContent;
+ navigator.clipboard.writeText(text).then(() => {
+ document.getElementById('copyCaseText').textContent = 'Copied to Clipboard!';
+ setTimeout(() => {
+ document.getElementById('copyCaseText').textContent = 'Copy Payment Case Details';
+ }, 2000);
+ });
+ }
+
+ // WhatsApp OTP Input Keyboard Listeners
+ const mobIn = document.getElementById('authMobileInput');
+ if (mobIn) {
+ mobIn.addEventListener('input', (e) => {
+ const clean = e.target.value.replace(/\D/g, '').slice(0, 10);
+ if (e.target.value !== clean) {
+ e.target.value = clean;
+ }
+ currentAuthMobile = clean;
+ });
+ mobIn.addEventListener('keydown', (e) => {
+ if (e.key === 'Enter') {
+ e.preventDefault();
+ handleAuthMobileSubmit();
+ }
+ });
+ }
+
+ const nameIn = document.getElementById('authSignupName');
+ const emailIn = document.getElementById('authSignupEmail');
+ if (nameIn) {
+ nameIn.addEventListener('input', (e) => {
+ currentAuthName = e.target.value;
+ });
+ nameIn.addEventListener('keydown', (e) => {
+ if (e.key === 'Enter') {
+ e.preventDefault();
+ emailIn?.focus();
+ }
+ });
+ }
+ if (emailIn) {
+ emailIn.addEventListener('input', (e) => {
+ currentAuthEmail = e.target.value;
+ });
+ emailIn.addEventListener('keydown', (e) => {
+ if (e.key === 'Enter') {
+ e.preventDefault();
+ handleAuthSignupSubmit();
+ }
+ });
+ }
+
+ const otpIn = document.getElementById('authOtpInput');
+ if (otpIn) {
+ otpIn.addEventListener('input', (e) => {
+ const val = e.target.value.replace(/\D/g, '');
+ e.target.value = val;
+ if (val.length === 6) {
+ handleAuthVerifyOtpSubmit();
+ }
+ });
+ otpIn.addEventListener('keydown', (e) => {
+ if (e.key === 'Enter') {
+ e.preventDefault();
+ handleAuthVerifyOtpSubmit();
+ }
+ });
+ }
+
+ // Boot checkout - restore preserved 1-week identity if available
+ const bootPreserved = getPreservedIdentity();
+ if (bootPreserved && bootPreserved.customer) {
+ activeCustomer = bootPreserved.customer;
+ activeToken = bootPreserved.token || localStorage.getItem('ypg_customer_token') || null;
+ updateCustomerDisplay(activeCustomer);
+ } else {
+ activeCustomer = null;
+ activeToken = null;
+ }
+
+ try {
+ const initEl = document.getElementById('ypgInitialState');
+ if (initEl && initEl.textContent && initEl.textContent.trim()) {
+ const parsed = JSON.parse(initEl.textContent);
+ if (parsed && (parsed.amount || parsed.branding || parsed.merchantName)) {
+ renderState(parsed);
+ }
+ }
+ } catch (e) { }
+
+ loadState();
+ connectSse();
</script>
+ <script src="/js/checkout.js"></script>
</body>
-</html>
+</html>
diff --cc apps/coin-wallet-store/src/server.js
index d66ee6e,89ab0c2..4d84030
--- a/apps/coin-wallet-store/src/server.js
+++ b/apps/coin-wallet-store/src/server.js
@@@ -8,56 -7,31 +8,70 @@@ import { getCoinStoreWebhookSecret } fr
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
-const HTML_ESCAPES = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' };
+/** Security headers applied to every HTML response served by the store. */
+const BASE_SECURITY_HEADERS = Object.freeze({
+ 'X-Content-Type-Options': 'nosniff',
+ 'X-Frame-Options': 'SAMEORIGIN',
+ 'Referrer-Policy': 'strict-origin-when-cross-origin',
+ 'Content-Security-Policy': "default-src 'self'; 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'"
+});
/**
- * Escapes a value for interpolation into the order outcome pages. These values
- * come straight from the query string, so they are attacker controlled.
+ * Escapes a value for interpolation into HTML text or attribute context.
+ * Order ids, UTRs and failure reasons arrive from query parameters, so they must never be
+ * injected raw into the order-status templates.
*/
-export function escapeHtml(value) {
- if (value === null || value === undefined) return '';
- return String(value).replace(/[&<>"']/g, (ch) => HTML_ESCAPES[ch]);
+function escapeHtml(value) {
+ return String(value ?? '')
+ .replaceAll('&', '&amp;')
+ .replaceAll('<', '&lt;')
+ .replaceAll('>', '&gt;')
+ .replaceAll('"', '&quot;')
+ .replaceAll("'", '&#39;');
+}
+
+/** Upper bound for an unauthenticated JSON request body. */
+const MAX_JSON_BODY_BYTES = 64 * 1024;
+/** Largest single top-up the demo store accepts. */
+const MAX_TOPUP_RUPEES = 100000;
+/** Accepted clock skew for an inbound webhook's X-Gateway-Timestamp. */
+const WEBHOOK_MAX_SKEW_MS = 5 * 60 * 1000;
+
+/** Keeps a client-supplied pack name printable and bounded before it is stored as metadata. */
+function sanitizePackName(value) {
+ const cleaned = String(value ?? '').replace(/[^\w .&()'-]/g, '').trim().slice(0, 60);
+ return cleaned || 'Gamer Pack';
+}
+
+/**
+ * Strips headers that would let a browser claim to be an internal renderer before the
+ * request is relayed to the payment core.
+ */
+function sanitizedForwardHeaders(req, targetHost) {
+ const headers = { ...req.headers, host: targetHost };
+ for (const name of Object.keys(headers)) {
+ const lower = name.toLowerCase();
+ if (lower === 'x-internal-render' || lower === 'x-internal-token') {
+ delete headers[name];
+ }
+ }
+ return headers;
}
+ /**
+ * Replaces {{PLACEHOLDER}} tokens with escaped values.
+ *
+ * The replacement is done with a replacer function so that `$&`, `$'` and `` $` ``
+ * inside an attacker-controlled value are inserted literally instead of being
+ * expanded as replacement patterns.
+ */
+ export function renderTemplate(template, values) {
+ return template.replace(/\{\{([A-Z0-9_]+)\}\}/g, (match, key) => {
+ if (!Object.prototype.hasOwnProperty.call(values, key)) return match;
+ return escapeHtml(values[key]);
+ });
+ }
+
// 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 {
diff --cc apps/merchant-admin/src/views/admin.html
index 3890448,1c0e078..5d8ada4
--- a/apps/merchant-admin/src/views/admin.html
+++ b/apps/merchant-admin/src/views/admin.html
@@@ -4016,11 -3961,11 +4040,11 @@@
}
tbody.innerHTML = queue.map(q => `
<tr>
- <td><strong>#${escapeHtmlValue(q.queuePosition)}</strong></td>
- <td>Order #${escapeHtmlValue(q.orderId)}</td>
+ <td><strong>#${q.queuePosition}</strong></td>
+ <td>Order #${escapeHtml(q.orderId)}</td>
<td>₹${(Number(q.amountPaise) / 100).toFixed(2)}</td>
<td><span class="badge badge-warning">QUEUED</span></td>
- <td>${new Date(q.createdAt).toLocaleTimeString()}</td>
+ <td>${escapeHtmlValue(new Date(q.createdAt).toLocaleTimeString())}</td>
</tr>
`).join('');
}
@@@ -4215,15 -4160,15 +4239,15 @@@
return `
<tr>
- <td><strong>Order #${escapeHtmlValue(p.orderId)}</strong></td>
- <td><code>${escapeHtmlValue(p.id)}</code></td>
+ <td><strong>Order #${escapeHtml(p.orderId)}</strong></td>
+ <td><code>${p.id}</code></td>
<td>₹${(Number(p.amountPaise) / 100).toFixed(2)}</td>
- <td>${p.method}</td>
- <td><span class="badge ${p.status === 'SUCCESS' ? 'badge-success' : (p.status === 'ACTIVE' ? 'badge-info' : 'badge-danger')}">${p.status}</span></td>
+ <td>${escapeHtmlValue(p.method)}</td>
+ <td><span class="badge ${p.status === 'SUCCESS' ? 'badge-success' : (p.status === 'ACTIVE' ? 'badge-info' : 'badge-danger')}">${escapeHtmlValue(p.status)}</span></td>
<td>${verifyBadge}</td>
<td>${sourcesChips}</td>
- <td>${new Date(p.createdAt).toLocaleTimeString()}</td>
- <td>${p.completedAt ? new Date(p.completedAt).toLocaleTimeString() : '-'}</td>
+ <td>${escapeHtmlValue(new Date(p.createdAt).toLocaleTimeString())}</td>
+ <td>${p.completedAt ? escapeHtmlValue(new Date(p.completedAt).toLocaleTimeString()) : '-'}</td>
</tr>
`;
}).join('');
diff --cc docs/CONFIGURATION.md
index 9f2a942,353fc34..b1bf78e
--- a/docs/CONFIGURATION.md
+++ b/docs/CONFIGURATION.md
@@@ -20,14 -20,9 +20,15 @@@
| `PAYMENT_ACTIVE_WINDOW_MS` | `300000` | Duration of active payment slot (5 minutes = 300,000ms) |
| `PAYMENT_PROCESSING_WINDOW_MS` | `30000` | Duration of verification slot (30 seconds = 30,000ms) |
| `LATE_OBSERVATION_WINDOW_MS` | `1800000` | Safety window for matching late observations (30 minutes) |
-| `ANDROID_ENROLLMENT_SECRET` | `32+ chars` | Ephemeral secret token used to authorize device QR pairing |
+| `ANDROID_ENROLLMENT_SECRET` | auto-generated | Secret used to authorize device QR pairing. If unset, a random value is created in `data/.secrets/android_enrollment_secret` |
+| `ADMIN_API_TOKEN` | auto-generated | Bearer token required by `/api/v1/admin/*` (the Merchant Admin page asks for it once per tab). If unset, a random value is created in `data/.secrets/admin_api_token` |
+| `OBSERVER_API_TOKEN` | auto-generated | Bearer token the local Gmail/SMS observers use to submit observations (`data/.secrets/observer_api_token`) |
+| `COIN_STORE_WEBHOOK_SECRET` | auto-generated | Webhook secret seeded for the default merchant and used by the demo coin store (`data/.secrets/coin_store_webhook_secret`) |
+| `YOUPI_EXPOSE_DEV_OTP` | unset | Set to `true` for local development to get the OTP in the `send-otp` response. Ignored when `NODE_ENV=production` |
+| `YOUPI_ALLOW_DEV_TOOLS` | unset | Developer test-mode / simulator endpoints (`/admin/dev/*`, `simulate-observation`) are refused when `NODE_ENV=production` unless this is `true` |
+| `GMAIL_REDIRECT_URI` | derived | Gmail OAuth redirect URI. Defaults to `<gmail host from your configured domain>/oauth/callback` |
| `ANDROID_HEARTBEAT_TIMEOUT_MS` | `180000` | Inactivity window before Android device is marked DISCONNECTED |
+ | `CHECKOUT_FRAME_ANCESTORS` | `*` | `frame-ancestors` CSP for the checkout page. Set it to your storefront origins (for example `https://shop.example.com`) to stop other sites framing the checkout page. |
---
diff --cc packages/merchant-sdk/src/youpi-checkout.js
index 030b79b,e140e93..5ad6879
--- a/packages/merchant-sdk/src/youpi-checkout.js
+++ b/packages/merchant-sdk/src/youpi-checkout.js
@@@ -369,21 -364,24 +369,36 @@@
backdrop.addEventListener('mouseup', stopOutsideDismiss, true);
}
- _isSafeRedirect(url) {
++_isSafeRedirect(url) {
+ try {
+ const u = new URL(url, window.location.href);
+ return u.protocol === 'http:' || u.protocol === 'https:';
+ } catch {
+ return false;
+ }
+ }
+
+ /**
+ * Origin of the checkout iframe this instance embeds. Messages from any
+ * other origin are ignored so a third party cannot forge payment events.
+ */
+ _checkoutOrigin() {
+ try {
+ const url = this.options.checkoutUrl || this.options.portalUrl;
+ if (url) return new URL(url, window.location.href).origin;
+ } catch (e) { }
+ return null;
+ }
+
_handleMessage(e) {
+ // Ignore messages from any window other than our checkout iframe (spoofed success events).
+ if (!this._iframe || e.source !== this._iframe.contentWindow) return;
+ if (!this._iframeOrigin || e.origin !== this._iframeOrigin) return;
if (!e.data || typeof e.data !== 'object') return;
+ const expectedOrigin = this._checkoutOrigin();
+ if (expectedOrigin && e.origin !== expectedOrigin) return;
+
const event = e.data.event;
const rawData = e.data.data || e.data;
diff --cc packages/merchant-sdk/src/youpi.js
index 9fbafb7,ebcac4e..c4077fe
--- a/packages/merchant-sdk/src/youpi.js
+++ b/packages/merchant-sdk/src/youpi.js
@@@ -377,21 -372,23 +377,35 @@@
backdrop.addEventListener('mouseup', stopOutsideDismiss, true);
}
- _isSafeRedirect(url) {
++_isSafeRedirect(url) {
+ try {
+ const u = new URL(url, window.location.href);
+ return u.protocol === 'http:' || u.protocol === 'https:';
+ } catch {
+ return false;
+ }
+ }
+
+ /**
+ * Origin of the checkout iframe this instance embeds. Messages from any
+ * other origin are ignored so a third party cannot forge payment events.
+ */
+ _checkoutOrigin() {
+ try {
+ if (this.options.checkoutUrl) return new URL(this.options.checkoutUrl, window.location.href).origin;
+ } catch (e) { }
+ return null;
+ }
+
_handleMessage(e) {
+ // Ignore messages from any window other than our checkout iframe (spoofed success events).
+ if (!this._iframe || e.source !== this._iframe.contentWindow) return;
+ if (!this._iframeOrigin || e.origin !== this._iframeOrigin) return;
if (!e.data || typeof e.data !== 'object') return;
+ const expectedOrigin = this._checkoutOrigin();
+ if (expectedOrigin && e.origin !== expectedOrigin) return;
+
const event = e.data.event;
const rawData = e.data.data || e.data;