162 lines
6.5 KiB
TypeScript
162 lines
6.5 KiB
TypeScript
import React, { useState, useEffect } from 'react';
|
|
import { createPayPalOrder } from '../../services/api/paymentService';
|
|
import { Loader2, AlertCircle } from 'lucide-react';
|
|
|
|
interface PayPalPaymentWrapperProps {
|
|
bookingId: number;
|
|
amount: number;
|
|
currency?: string;
|
|
onError?: (error: string) => void;
|
|
}
|
|
|
|
const PayPalPaymentWrapper: React.FC<PayPalPaymentWrapperProps> = ({
|
|
bookingId,
|
|
amount,
|
|
currency = 'USD',
|
|
onError,
|
|
}) => {
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [approvalUrl, setApprovalUrl] = useState<string | null>(null);
|
|
|
|
// Initialize PayPal order
|
|
useEffect(() => {
|
|
const initializePayPal = async () => {
|
|
try {
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
// Get current URL for return/cancel URLs
|
|
const currentUrl = window.location.origin;
|
|
const returnUrl = `${currentUrl}/payment/paypal/return?bookingId=${bookingId}`;
|
|
const cancelUrl = `${currentUrl}/payment/paypal/cancel?bookingId=${bookingId}`;
|
|
|
|
const response = await createPayPalOrder(
|
|
bookingId,
|
|
amount,
|
|
currency,
|
|
returnUrl,
|
|
cancelUrl
|
|
);
|
|
|
|
if (response.success && response.data) {
|
|
const { approval_url } = response.data;
|
|
|
|
if (!approval_url) {
|
|
throw new Error('Approval URL not received from server');
|
|
}
|
|
|
|
setApprovalUrl(approval_url);
|
|
} else {
|
|
throw new Error(response.message || 'Failed to initialize PayPal payment');
|
|
}
|
|
} catch (err: any) {
|
|
console.error('Error initializing PayPal:', err);
|
|
const errorMessage = err.response?.data?.message || err.message || 'Failed to initialize PayPal payment';
|
|
setError(errorMessage);
|
|
if (onError) {
|
|
onError(errorMessage);
|
|
}
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
initializePayPal();
|
|
}, [bookingId, amount, currency, onError]);
|
|
|
|
const handlePayPalClick = () => {
|
|
if (approvalUrl) {
|
|
// Redirect to PayPal approval page
|
|
window.location.href = approvalUrl;
|
|
}
|
|
};
|
|
|
|
if (loading) {
|
|
return (
|
|
<div className="flex items-center justify-center p-8">
|
|
<Loader2 className="w-8 h-8 animate-spin text-indigo-600" />
|
|
<span className="ml-2 text-gray-600">Initializing PayPal payment...</span>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
if (error) {
|
|
return (
|
|
<div className="bg-red-50 border border-red-200 rounded-lg p-6">
|
|
<div className="flex items-start gap-3">
|
|
<AlertCircle className="w-5 h-5 text-red-600 mt-0.5" />
|
|
<div>
|
|
<h3 className="text-lg font-semibold text-red-900 mb-1">
|
|
Payment Initialization Failed
|
|
</h3>
|
|
<p className="text-sm text-red-800">
|
|
{error || 'Unable to initialize PayPal payment. Please try again.'}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
if (!approvalUrl) {
|
|
return (
|
|
<div className="flex items-center justify-center p-8">
|
|
<Loader2 className="w-8 h-8 animate-spin text-indigo-600" />
|
|
<span className="ml-2 text-gray-600">Loading PayPal...</span>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className="bg-white rounded-lg border border-gray-200 p-6">
|
|
<div className="text-center">
|
|
<div className="mb-4">
|
|
<svg
|
|
className="mx-auto h-12 w-auto"
|
|
viewBox="0 0 283 64"
|
|
fill="none"
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
>
|
|
<path
|
|
d="M141.04 16c-11.04 0-19 7.2-19 18s8.96 18 20 18c6.67 0 12.55-2.64 16.19-7.09l-7.65-4.42c-2.02 2.21-5.09 3.5-8.54 3.5-4.79 0-8.86-2.5-10.37-6.5h28.02c.22-1.12.35-2.28.35-3.5 0-10.79-7.96-17.99-19-17.99zm-9.46 14.5c1.25-3.99 4.67-6.5 9.45-6.5 4.79 0 8.21 2.51 9.45 6.5h-18.9zM248.72 16c-11.04 0-19 7.2-19 18s8.96 18 20 18c6.67 0 12.55-2.64 16.19-7.09l-7.65-4.42c-2.02 2.21-5.09 3.5-8.54 3.5-4.79 0-8.86-2.5-10.37-6.5h28.02c.22-1.12.35-2.28.35-3.5 0-10.79-7.96-17.99-19-17.99zm-9.45 14.5c1.25-3.99 4.67-6.5 9.45-6.5 4.79 0 8.21 2.51 9.45 6.5h-18.9zM200.24 34c0 6 3.92 10 10 10 4.12 0 7.21-1.87 8.8-4.92l7.68 4.43c-3.18 5.3-9.14 8.49-16.48 8.49-11.05 0-19-7.2-19-18s7.96-18 19-18c7.34 0 13.29 3.19 16.48 8.49l-7.68 4.43c-1.59-3.05-4.68-4.92-8.8-4.92-6.07 0-10 4-10 10zm82.48-29v46h-9V5h9zM36.95 0L73.9 64H0L36.95 0zm92.38 5l-27.71 48L73.91 5H84.3l17.32 30 17.32-30h10.39zm58.91 12v9.69c-1-.29-2.06-.49-3.2-.49-5.81 0-10 4-10 10V51h-9V17h9v9.2c0-5.08 5.7-9.2 12.2-9.2z"
|
|
fill="#003087"
|
|
/>
|
|
</svg>
|
|
</div>
|
|
<h3 className="text-lg font-semibold text-gray-900 mb-2">
|
|
Complete Payment with PayPal
|
|
</h3>
|
|
<p className="text-sm text-gray-600 mb-6">
|
|
You will be redirected to PayPal to securely complete your payment of{' '}
|
|
<span className="font-semibold">
|
|
{new Intl.NumberFormat('en-US', {
|
|
style: 'currency',
|
|
currency: currency,
|
|
}).format(amount)}
|
|
</span>
|
|
</p>
|
|
<button
|
|
onClick={handlePayPalClick}
|
|
className="w-full bg-[#0070ba] hover:bg-[#005ea6] text-white font-semibold py-3 px-6 rounded-lg transition-colors duration-200 flex items-center justify-center gap-2"
|
|
>
|
|
<svg
|
|
className="w-6 h-6"
|
|
viewBox="0 0 24 24"
|
|
fill="currentColor"
|
|
>
|
|
<path d="M7.076 21.337H2.47a.641.641 0 0 1-.633-.74L4.944.901C5.026.382 5.474 0 5.998 0h7.46c2.57 0 4.578.543 5.69 1.81 1.01 1.15 1.304 2.42 1.012 4.287-.023.143-.047.288-.077.437-.983 5.05-4.349 6.797-8.647 6.797h-2.19c-.524 0-.968.382-1.05.9l-1.12 7.203zm14.146-14.42a.477.477 0 0 0-.414-.24h-3.84c-.48 0-.856.355-.932.826-.075.47-.232 1.21-.232 1.21s-.156-.74-.232-1.21a.957.957 0 0 0-.932-.826H5.342a.957.957 0 0 0-.932.826c-.076.47-.232 1.21-.232 1.21s-.156-.74-.232-1.21a.957.957 0 0 0-.932-.826H.477a.477.477 0 0 0-.414.24c-.11.19-.14.426-.08.643.06.217.2.4.388.51l.04.02c.19.11.426.14.643.08.217-.06.4-.2.51-.388l.01-.02c.11-.19.14-.426.08-.643a.955.955 0 0 0-.388-.51l-.01-.01a.955.955 0 0 0-.51-.388.955.955 0 0 0-.643.08l-.01.01a.955.955 0 0 0-.388.51c-.06.217-.03.453.08.643l.01.02c.11.188.293.328.51.388.217.06.453.03.643-.08l.01-.02c.188-.11.328-.293.388-.51.06-.217.03-.453-.08-.643l-.01-.01z"/>
|
|
</svg>
|
|
Pay with PayPal
|
|
</button>
|
|
<p className="text-xs text-gray-500 mt-4">
|
|
Secure payment powered by PayPal
|
|
</p>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default PayPalPaymentWrapper;
|
|
|