Fix: Iframe params
This commit is contained in:
parent
fb79298d31
commit
bf8c77f490
@ -1,5 +1,35 @@
|
|||||||
import type { ICustomer, IRedirect } from '@/features/cashier/types';
|
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 {
|
export interface ICashierConfig {
|
||||||
// Core payment configuration
|
// Core payment configuration
|
||||||
paymentType?: 'deposit' | 'withdrawal';
|
paymentType?: 'deposit' | 'withdrawal';
|
||||||
@ -39,7 +69,25 @@ export interface ICashierConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function getCashierConfig(): 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 = {
|
const config: ICashierConfig = {
|
||||||
};
|
};
|
||||||
@ -63,13 +111,10 @@ export function getCashierConfig(): ICashierConfig {
|
|||||||
if (amount && amount !== 'undefined') {
|
if (amount && amount !== 'undefined') {
|
||||||
const parsedAmount = parseFloat(amount);
|
const parsedAmount = parseFloat(amount);
|
||||||
if (!isNaN(parsedAmount)) {
|
if (!isNaN(parsedAmount)) {
|
||||||
config.amount = 200;
|
config.amount = parsedAmount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: remove this default value
|
|
||||||
config.amount = 200;
|
|
||||||
|
|
||||||
// Account from URL (for withdrawal)
|
// Account from URL (for withdrawal)
|
||||||
const account = params.get('account');
|
const account = params.get('account');
|
||||||
if (account) {
|
if (account) {
|
||||||
@ -86,8 +131,6 @@ export function getCashierConfig(): ICashierConfig {
|
|||||||
if (userId) {
|
if (userId) {
|
||||||
config.userId = userId;
|
config.userId = userId;
|
||||||
}
|
}
|
||||||
// TODO: remove this default value
|
|
||||||
config.userId = '12345';
|
|
||||||
|
|
||||||
|
|
||||||
const sessionId = params.get('sessionId');
|
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)
|
// Customer data from URL (support both new 'userId' and old 'customer_id' format)
|
||||||
const customer: Partial<ICustomer> = {};
|
// Start with postMessage customer if available, otherwise create new object
|
||||||
// TODO: remove this default value
|
const customer: Partial<ICustomer> = postMessageCustomer ? { ...postMessageCustomer } : {};
|
||||||
const customerId =
|
|
||||||
// params.get('customer_id') || userId ||
|
const customerId = params.get('customer_id') || userId;
|
||||||
'12345';
|
|
||||||
|
|
||||||
if (customerId) customer.id = customerId;
|
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) {
|
if (Object.keys(customer).length > 0) {
|
||||||
config.customer = customer;
|
config.customer = customer;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user