import { useEffect, useState, useRef } from "react"; import { Button } from "@/components/ui/button"; import { useToast } from "@/hooks/use-toast"; import { Loader2, CreditCard, Check } from "lucide-react"; // PayPal SDK types declare global { interface Window { paypal?: any; } } interface PayPalPaymentProps { amount: string; currency?: string; item: string; onPaymentComplete?: (orderId: string) => void; } type PaymentMethod = 'paypal' | 'card' | 'ideal'; const PayPalPayment = ({ amount, currency = "EUR", item, onPaymentComplete }: PayPalPaymentProps) => { const [sdkReady, setSdkReady] = useState(false); const [loading, setLoading] = useState(true); const [selectedMethod, setSelectedMethod] = useState('paypal'); const [paymentComplete, setPaymentComplete] = useState(false); const [orderId, setOrderId] = useState(null); const paypalButtonRef = useRef(null); const cardButtonRef = useRef(null); const idealButtonRef = useRef(null); const { toast } = useToast(); // Replace with your PayPal Client ID // Get one at: https://developer.paypal.com/dashboard/applications const PAYPAL_CLIENT_ID = "YOUR_PAYPAL_CLIENT_ID"; useEffect(() => { const loadPayPalSDK = () => { if (window.paypal) { setSdkReady(true); setLoading(false); return; } const script = document.createElement("script"); script.src = `https://www.paypal.com/sdk/js?client-id=${PAYPAL_CLIENT_ID}¤cy=${currency}&components=buttons,funding-eligibility&enable-funding=ideal,card`; script.async = true; script.onload = () => { setSdkReady(true); setLoading(false); }; script.onerror = () => { console.error("PayPal SDK failed to load"); setLoading(false); toast({ title: "Error", description: "Failed to load payment system. Check your Client ID.", variant: "destructive", }); }; document.body.appendChild(script); }; loadPayPalSDK(); return () => { // Cleanup if needed }; }, [currency, toast]); useEffect(() => { if (!sdkReady || !window.paypal || paymentComplete) return; const createOrder = (_data: any, actions: any) => { return actions.order.create({ purchase_units: [{ description: item, amount: { currency_code: currency, value: amount, }, }], }); }; const onApprove = async (_data: any, actions: any) => { try { const order = await actions.order.capture(); console.log("Payment successful:", order); setPaymentComplete(true); setOrderId(order.id); toast({ title: "Payment Successful!", description: `Order ${order.id} has been confirmed.`, }); onPaymentComplete?.(order.id); } catch (error) { console.error("Payment capture error:", error); toast({ title: "Payment Failed", description: "There was an error processing your payment.", variant: "destructive", }); } }; const onError = (err: any) => { console.error("PayPal error:", err); toast({ title: "Payment Error", description: "Something went wrong. Please try again.", variant: "destructive", }); }; // Render PayPal button if (paypalButtonRef.current && selectedMethod === 'paypal') { paypalButtonRef.current.innerHTML = ''; window.paypal.Buttons({ fundingSource: window.paypal.FUNDING.PAYPAL, style: { layout: 'vertical', color: 'black', shape: 'rect', label: 'paypal', height: 40, }, createOrder, onApprove, onError, }).render(paypalButtonRef.current); } // Render Card button if (cardButtonRef.current && selectedMethod === 'card') { cardButtonRef.current.innerHTML = ''; window.paypal.Buttons({ fundingSource: window.paypal.FUNDING.CARD, style: { layout: 'vertical', color: 'black', shape: 'rect', height: 40, }, createOrder, onApprove, onError, }).render(cardButtonRef.current); } // Render iDEAL button (available in Netherlands) if (idealButtonRef.current && selectedMethod === 'ideal') { idealButtonRef.current.innerHTML = ''; if (window.paypal.FUNDING.IDEAL) { window.paypal.Buttons({ fundingSource: window.paypal.FUNDING.IDEAL, style: { layout: 'vertical', shape: 'rect', height: 40, }, createOrder, onApprove, onError, }).render(idealButtonRef.current); } else { idealButtonRef.current.innerHTML = '

iDEAL not available in your region

'; } } }, [sdkReady, selectedMethod, amount, currency, item, toast, onPaymentComplete, paymentComplete]); if (paymentComplete && orderId) { return (
[ PAYMENT CONFIRMED ]

Payment Successful!

{amount} {currency} paid

Order ID:

{orderId}

Your purchase is complete. Check your email for confirmation.

); } return (
[ CARD / PAYPAL PAYMENT ]

{item}

Pay securely with PayPal, card, or iDEAL

Amount: {amount} {currency}
{loading ? (
) : !sdkReady ? (

PayPal SDK not loaded

Replace YOUR_PAYPAL_CLIENT_ID with your actual PayPal Client ID

) : (
)}

{selectedMethod === 'paypal' && "Pay with your PayPal balance or linked cards."} {selectedMethod === 'card' && "Pay securely with Visa, Mastercard, or Amex."} {selectedMethod === 'ideal' && "Pay directly from your Dutch bank account."}

); }; export default PayPalPayment;