diff --git a/src/config/cashierConfig.ts b/src/config/cashierConfig.ts index 6cc4faf..097e0f3 100644 --- a/src/config/cashierConfig.ts +++ b/src/config/cashierConfig.ts @@ -1,5 +1,7 @@ import type { ICustomer, IRedirect } from '@/features/cashier/types'; +const CASHIER_CONFIG_STORAGE_KEY = 'cashier_config'; + // Store parameters received via postMessage from parent window const postMessageParams: Record = {}; let postMessageCustomer: Partial | null = null; @@ -88,12 +90,20 @@ export function getCashierConfig(): ICashierConfig { params.set(key, value); }); - // Debug: Log all parameters (URL + postMessage) - console.log('All URL params:', Object.fromEntries(urlParams.entries())); - console.log('All postMessage params:', postMessageParams); - console.log('Merged params:', Object.fromEntries(params.entries())); + let storedConfig: Partial = {}; + if (typeof window !== 'undefined') { + try { + const stored = window.sessionStorage.getItem(CASHIER_CONFIG_STORAGE_KEY); + if (stored) { + storedConfig = JSON.parse(stored) as Partial; + } + } catch { + // Ignore storage parsing errors and fall back to fresh config + } + } const config: ICashierConfig = { + ...storedConfig, }; // Payment type from URL (support both 'method' and 'payment_type' for backward compatibility) @@ -152,8 +162,12 @@ export function getCashierConfig(): ICashierConfig { } // Customer data from URL (support both new 'userId' and old 'customer_id' format) - // Start with postMessage customer if available, otherwise create new object - const customer: Partial = postMessageCustomer ? { ...postMessageCustomer } : {}; + // Start with postMessage customer if available, otherwise fall back to stored customer + const customer: Partial = postMessageCustomer + ? { ...postMessageCustomer } + : storedConfig.customer + ? { ...storedConfig.customer } + : {}; const customerId = params.get('customer_id')?.trim() || userId; if (customerId) { @@ -184,7 +198,7 @@ export function getCashierConfig(): ICashierConfig { } // Redirect URLs from URL - const redirect: Partial = {}; + const redirect: Partial = config.redirect ? { ...config.redirect } : {}; const successUrl = params.get('redirect_success'); const cancelUrl = params.get('redirect_cancel'); const errorUrl = params.get('redirect_error'); @@ -251,7 +265,7 @@ export function getCashierConfig(): ICashierConfig { } // Attributes (handle attributes.* parameters) - const attributes: Record = {}; + const attributes: Record = config.attributes ? { ...config.attributes } : {}; params.forEach((value, key) => { if (key.startsWith('attributes.')) { const attrKey = key.replace('attributes.', ''); @@ -263,6 +277,14 @@ export function getCashierConfig(): ICashierConfig { config.attributes = attributes; } + if (typeof window !== 'undefined') { + try { + window.sessionStorage.setItem(CASHIER_CONFIG_STORAGE_KEY, JSON.stringify(config)); + } catch { + // Ignore storage errors (e.g. quota exceeded) and continue without persistence + } + } + return config; }