Fix: Iframe params

This commit is contained in:
Mitchell Magro 2026-01-13 14:40:27 +01:00
parent fb79298d31
commit bf8c77f490

View File

@ -1,5 +1,35 @@
import type { ICustomer, IRedirect } from '@/features/cashier/types';
// Store parameters received via postMessage from parent window
const postMessageParams: Record<string, string> = {};
let postMessageCustomer: Partial<ICustomer> | null = null;
// Initialize postMessage listener to receive params from parent window
if (typeof window !== 'undefined' && window.parent !== window) {
window.addEventListener('message', (event: MessageEvent) => {
// Accept messages from any origin (parent window)
// In production, you might want to validate event.origin
if (event.data && typeof event.data === 'object') {
// If the message contains config params, store them
if (event.data.type === 'CASHIER_CONFIG' || !event.data.type) {
// Convert object to URLSearchParams-like format
Object.entries(event.data).forEach(([key, value]) => {
if (key === 'customer' && typeof value === 'object' && value !== null) {
// Handle customer object separately
postMessageCustomer = value as Partial<ICustomer>;
} else if (key !== 'type' && typeof value === 'string') {
postMessageParams[key] = value;
} else if (typeof value === 'number' || typeof value === 'boolean') {
postMessageParams[key] = String(value);
}
});
console.log('Received params via postMessage:', postMessageParams);
console.log('Received customer via postMessage:', postMessageCustomer);
}
}
});
}
export interface ICashierConfig {
// Core payment configuration
paymentType?: 'deposit' | 'withdrawal';
@ -39,7 +69,25 @@ export interface ICashierConfig {
}
export function getCashierConfig(): ICashierConfig {
const params = new URLSearchParams(window.location.search);
const urlParams = new URLSearchParams(window.location.search);
// Merge URL params with postMessage params (postMessage params take precedence)
const params = new URLSearchParams();
// First add URL params
urlParams.forEach((value, key) => {
params.set(key, value);
});
// Then override/add postMessage params
Object.entries(postMessageParams).forEach(([key, value]) => {
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()));
const config: ICashierConfig = {
};
@ -63,13 +111,10 @@ export function getCashierConfig(): ICashierConfig {
if (amount && amount !== 'undefined') {
const parsedAmount = parseFloat(amount);
if (!isNaN(parsedAmount)) {
config.amount = 200;
config.amount = parsedAmount;
}
}
// TODO: remove this default value
config.amount = 200;
// Account from URL (for withdrawal)
const account = params.get('account');
if (account) {
@ -86,8 +131,6 @@ export function getCashierConfig(): ICashierConfig {
if (userId) {
config.userId = userId;
}
// TODO: remove this default value
config.userId = '12345';
const sessionId = params.get('sessionId');
@ -101,14 +144,28 @@ export function getCashierConfig(): ICashierConfig {
}
// Customer data from URL (support both new 'userId' and old 'customer_id' format)
const customer: Partial<ICustomer> = {};
// TODO: remove this default value
const customerId =
// params.get('customer_id') || userId ||
'12345';
// Start with postMessage customer if available, otherwise create new object
const customer: Partial<ICustomer> = postMessageCustomer ? { ...postMessageCustomer } : {};
const customerId = params.get('customer_id') || userId;
if (customerId) customer.id = customerId;
// Parse additional customer fields from URL params (only if not already set from postMessage)
if (!customer.first_name) {
const firstName = params.get('customer_first_name') || params.get('first_name');
if (firstName) customer.first_name = firstName;
}
if (!customer.last_name) {
const lastName = params.get('customer_last_name') || params.get('last_name');
if (lastName) customer.last_name = lastName;
}
if (!customer.email) {
const email = params.get('customer_email') || params.get('email');
if (email) customer.email = email;
}
if (Object.keys(customer).length > 0) {
config.customer = customer;
}