import { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Card, CardContent } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Tag, Loader2 } from 'lucide-react'; import { toast } from '@/components/custom-toast'; import { StripePaymentForm } from './stripe-payment-form'; import { PayPalPaymentForm } from './paypal-payment-form'; import { BankTransferForm } from './bank-transfer-form'; import { RazorpayPaymentForm } from './razorpay-payment-form'; import { MercadoPagoPaymentForm } from './mercadopago-payment-form'; import { PaystackPaymentForm } from './paystack-payment-form'; import { FlutterwavePaymentForm } from './flutterwave-payment-form'; import { PayTabsPaymentForm } from './paytabs-payment-form'; import { SkrillPaymentForm } from './skrill-payment-form'; import { CoinGatePaymentForm } from './coingate-payment-form'; import { PayfastPaymentForm } from './payfast-payment-form'; import { ToyyibPayPaymentForm } from './toyyibpay-payment-form'; import { PayTRPaymentForm } from './paytr-payment-form'; import { MolliePaymentForm } from './mollie-payment-form'; import { CashfreePaymentForm } from './cashfree-payment-form'; import { IyzipayPaymentForm } from './iyzipay-payment-form'; import { BenefitPaymentForm } from './benefit-payment-form'; import { OzowPaymentForm } from './ozow-payment-form'; import { EasebuzzPaymentForm } from './easebuzz-payment-form'; import { KhaltiPaymentForm } from './khalti-payment-form'; import { AuthorizeNetPaymentForm } from './authorizenet-payment-form'; import { FedaPayPaymentForm } from './fedapay-payment-form'; import { PayHerePaymentForm } from './payhere-payment-form'; import { CinetPayPaymentForm } from './cinetpay-payment-form'; import { PaiementPaymentForm } from './paiement-payment-form'; import { NepalstePaymentForm } from './nepalste-payment-form'; import { YooKassaPaymentForm } from './yookassa-payment-form'; import { AamarpayPaymentForm } from './aamarpay-payment-form'; import { MidtransPaymentForm } from './midtrans-payment-form'; import { PaymentWallPaymentForm } from './paymentwall-payment-form'; import { SSPayPaymentForm } from './sspay-payment-form'; import { TapPaymentForm } from './tap-payment-form'; import { XenditPaymentForm } from './xendit-payment-form'; interface PaymentMethod { id: string; name: string; icon: React.ReactNode; enabled: boolean; currency?: string; currencySymbol?: string; } interface PaymentProcessorProps { plan: { id: number; name: string; price: string | number; duration: string; paymentMethods?: any; }; billingCycle: 'monthly' | 'yearly'; paymentMethods: PaymentMethod[]; currencySymbol?: string; onSuccess: () => void; onCancel: () => void; } export function PaymentProcessor({ plan, billingCycle, paymentMethods, currencySymbol = '$', onSuccess, onCancel }: PaymentProcessorProps) { const { t } = useTranslation(); // Helper function to safely format currency const formatCurrency = (amount: string | number) => { if (typeof window !== 'undefined' && window.appSettings?.formatCurrency) { const numericAmount = typeof amount === 'number' ? amount : parseFloat(amount); return window.appSettings.formatCurrency(numericAmount, { showSymbol: true }); } return amount; }; const [selectedPaymentMethod, setSelectedPaymentMethod] = useState(''); const [couponCode, setCouponCode] = useState(''); const [appliedCoupon, setAppliedCoupon] = useState(null); const [couponLoading, setCouponLoading] = useState(false); const [showPaymentForm, setShowPaymentForm] = useState(false); const originalPrice = Number(plan.price); const discountAmount = appliedCoupon ? (appliedCoupon.type === 'percentage' ? (originalPrice * appliedCoupon.value / 100) : appliedCoupon.value) : 0; const finalPrice = Math.max(0, originalPrice - discountAmount); const handleApplyCoupon = async () => { if (!couponCode.trim()) { toast.error(t('Please enter a coupon code')); return; } setCouponLoading(true); try { const response = await fetch(route('coupons.validate'), { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') || '' }, body: JSON.stringify({ coupon_code: couponCode, plan_id: plan.id, amount: originalPrice }) }); const data = await response.json(); if (response.ok && data.valid) { setAppliedCoupon(data.coupon); toast.success(t('Coupon applied successfully')); } else { toast.error(data.message || t('Invalid coupon code')); setAppliedCoupon(null); } } catch (error) { toast.error(t('Failed to validate coupon')); setAppliedCoupon(null); } finally { setCouponLoading(false); } }; const handleRemoveCoupon = () => { setAppliedCoupon(null); setCouponCode(''); }; const handlePayNow = () => { if (!selectedPaymentMethod) { toast.error(t('Please select a payment method')); return; } setShowPaymentForm(true); }; const handlePaymentCancel = () => { setShowPaymentForm(false); setSelectedPaymentMethod(''); }; const enabledPaymentMethods = paymentMethods.filter(method => method.enabled); const renderPaymentForm = () => { const commonProps = { planId: plan.id, couponCode, billingCycle, onSuccess, onCancel: handlePaymentCancel, }; switch (selectedPaymentMethod) { case 'stripe': return ( ); case 'paypal': return ( ); case 'bank': return ( ); case 'razorpay': return ( ); case 'mercadopago': return ( ); case 'paystack': return ( ); case 'flutterwave': return ( ); case 'paytabs': return ( ); case 'skrill': return ( ); case 'coingate': return ( ); case 'payfast': return ( ); case 'toyyibpay': return ( ); case 'paytr': return ( ); case 'mollie': return ( ); case 'cashfree': return ( ); case 'iyzipay': return ( ); case 'benefit': return ( ); case 'ozow': return ( ); case 'easebuzz': return ( ); case 'khalti': return ( ); case 'authorizenet': return ( ); case 'fedapay': return ( ); case 'payhere': return ( ); case 'cinetpay': return ( ); case 'paiement': return ( ); case 'nepalste': return ( ); case 'yookassa': return ( ); case 'aamarpay': return ( ); case 'midtrans': return ( ); case 'paymentwall': return ( ); case 'sspay': return ( ); case 'tap': return ( ); case 'xendit': return ( ); default: return null; } }; if (showPaymentForm) { return (

{t('Complete Payment')}

{renderPaymentForm()}
); } return (
{/* Plan Summary */}

{plan.name}

{t(billingCycle)} {t('subscription')}

{currencySymbol} {plan.price}
/{t(plan.duration.toLowerCase())}
{/* Payment Methods */}
{enabledPaymentMethods.length === 0 ? (

{t('No payment methods available')}

) : (
{enabledPaymentMethods.map((method, index) => ( setSelectedPaymentMethod(method.id)} >
{method.icon}
{method.name} {selectedPaymentMethod === method.id && ( {t('Selected')} )}
))}
)}
{/* Coupon Code */}
setCouponCode(e.target.value)} placeholder={t('Enter coupon code')} className="pr-10" disabled={!!appliedCoupon} />
{!appliedCoupon ? ( ) : ( )}
{appliedCoupon && (
{t('Coupon Applied')}: {appliedCoupon.code} -{appliedCoupon.type === 'percentage' ? `${appliedCoupon.value}%` : `${currencySymbol}${appliedCoupon.value}`}
)}
{/* Price Summary */}
{t('Subtotal')} {currencySymbol}{originalPrice}
{appliedCoupon && (
{t('Discount')} -{currencySymbol}{discountAmount}
)}
{t('Total')} {currencySymbol}{finalPrice.toFixed(2)}
{/* Actions */}
); }