import React, { useState, useEffect } from 'react'; import { createBoricaPayment } from '../../services/api/paymentService'; import { X, Loader2, AlertCircle, CheckCircle, CreditCard } from 'lucide-react'; import { toast } from 'react-toastify'; import { useFormatCurrency } from '../../hooks/useFormatCurrency'; interface BoricaPaymentModalProps { isOpen: boolean; bookingId: number; amount: number; currency?: string; onSuccess: () => void; onClose: () => void; } const BoricaPaymentModal: React.FC = ({ isOpen, bookingId, amount, currency: propCurrency, onSuccess, onClose, }) => { const { currency: contextCurrency } = useFormatCurrency(); const currency = propCurrency || contextCurrency || 'BGN'; const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [paymentRequest, setPaymentRequest] = useState(null); useEffect(() => { if (!isOpen) return; const initializeBorica = async () => { try { setLoading(true); setError(null); const currentUrl = window.location.origin; const returnUrl = `${currentUrl}/payment/borica/return?bookingId=${bookingId}`; const response = await createBoricaPayment( bookingId, amount, currency, returnUrl ); if (response.success && response.data) { const { payment_request } = response.data; if (!payment_request) { throw new Error('Payment request not received from server'); } setPaymentRequest(payment_request); } else { throw new Error(response.message || 'Failed to initialize Borica payment'); } } catch (err: any) { console.error('Error initializing Borica:', err); const errorMessage = err.response?.data?.message || err.message || 'Failed to initialize Borica payment'; setError(errorMessage); toast.error(errorMessage); } finally { setLoading(false); } }; initializeBorica(); }, [isOpen, bookingId, amount, currency]); const handleSubmitPayment = () => { if (!paymentRequest) return; // Create a form and submit it to Borica gateway const form = document.createElement('form'); form.method = 'POST'; form.action = paymentRequest.gateway_url; form.style.display = 'none'; // Add all form fields const fields = { TERMINAL: paymentRequest.terminal_id, MERCHANT: paymentRequest.merchant_id, ORDER: paymentRequest.order_id, AMOUNT: paymentRequest.amount, CURRENCY: paymentRequest.currency, DESC: paymentRequest.description, TIMESTAMP: paymentRequest.timestamp, P_SIGN: paymentRequest.signature, TRTYPE: paymentRequest.trtype, BACKREF: paymentRequest.return_url, }; Object.entries(fields).forEach(([key, value]) => { const input = document.createElement('input'); input.type = 'hidden'; input.name = key; input.value = value as string; form.appendChild(input); }); document.body.appendChild(form); form.submit(); }; if (!isOpen) return null; return (
{/* Header */}

Borica Payment

{/* Content */}
{loading ? (

Initializing Borica payment...

) : error ? (

Payment Initialization Failed

{error || 'Unable to initialize Borica payment. Please try again.'}

) : paymentRequest ? (

Complete Payment with Borica

You will be redirected to Borica payment gateway to securely complete your payment of{' '} {new Intl.NumberFormat('en-US', { style: 'currency', currency: currency, }).format(amount)}

Order ID: {paymentRequest.order_id}
Amount: {new Intl.NumberFormat('en-US', { style: 'currency', currency: currency, }).format(amount)}

Your payment will be processed securely through Borica payment gateway. You will be redirected to complete the transaction.

) : null}
{/* Footer */} {!loading && !error && paymentRequest && (
)}
); }; export default BoricaPaymentModal;