Handling info when redirecting

This commit is contained in:
Mitchell Magro 2026-01-15 09:10:56 +01:00
parent 6f4c1d00de
commit 83336d3555

View File

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