import React, { useState, useEffect } from 'react';
const PaymentModal = ({ paymentUrl, onClose, onSuccess, onFailure, onCancel }) => {
useEffect(() => {
const handleMessage = (event) => {
// Security verification
// if (event.origin !== 'https://api.unibee.dev') return;
const { type, paymentId, invoiceId } = event.data;
switch (type) {
case 'UniBee_PaymentSuccess':
onSuccess?.(paymentId, invoiceId);
break;
case 'UniBee_PaymentFailed':
onFailure?.(paymentId, invoiceId);
break;
case 'UniBee_PaymentCancelled':
onCancel?.(paymentId, invoiceId);
break;
}
};
window.addEventListener('message', handleMessage);
return () => window.removeEventListener('message', handleMessage);
}, [onSuccess, onFailure, onCancel]);
return (
<div className="payment-modal">
<div className="payment-content">
<iframe
src={paymentUrl}
className="payment-iframe"
title="Payment"
/>
</div>
</div>
);
};
const PaymentPage = () => {
const [showPayment, setShowPayment] = useState(false);
const [paymentUrl, setPaymentUrl] = useState('');
const createPayment = async () => {
try {
const response = await fetch('/api/merchant/payment/new', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer your-token'
},
body: JSON.stringify({
paymentUIMode: 'embedded',
gatewayId: 1,
amount: 1000,
currency: 'USD',
})
});
const data = await response.json();
setPaymentUrl(data.link);
setShowPayment(true);
} catch (error) {
console.error('Failed to create payment:', error);
}
};
const handleSuccess = (paymentId, invoiceId) => {
setShowPayment(false);
console.log('Payment successful:', { paymentId, invoiceId });
// Handle success
};
const handleFailure = (paymentId, invoiceId) => {
setShowPayment(false);
console.log('Payment failed:', { paymentId, invoiceId });
// Handle failure
};
const handleCancel = (paymentId, invoiceId) => {
setShowPayment(false);
console.log('Payment cancelled:', { paymentId, invoiceId });
// Handle cancellation
};
return (
<div>
<button onClick={createPayment}>Create Payment</button>
{showPayment && (
<PaymentModal
paymentUrl={paymentUrl}
onClose={() => setShowPayment(false)}
onSuccess={handleSuccess}
onFailure={handleFailure}
onCancel={handleCancel}
/>
)}
</div>
);
};