'use client'; import { useEffect, useState, useCallback , Suspense} from 'react'; import { useSearchParams, useRouter } from 'next/navigation'; import { api, formatPrice } from '@/lib/api'; import { Copy, Check, Clock, Banknote, CheckCircle2, AlertCircle, ChevronLeft } from 'lucide-react'; import Link from 'next/link'; import { useToast } from '@/hooks/use-toast'; import { PageSkeleton } from '@/components/page-skeleton'; const PALMPAY_ACCOUNT = '8088561764'; const PALMPAY_NAME = 'Abdlrazaq Bidemi Awofolaji'; const PALMPAY_BANK = 'PalmPay'; function PaymentContent() { const params = useSearchParams(); const orderId = params.get('order') || ''; const router = useRouter(); const { toast } = useToast(); const [order, setOrder] = useState(null); const [loading, setLoading] = useState(true); const [step, setStep] = useState<'details' | 'sent' | 'verifying' | 'paid' | 'failed'>('details'); const [copied, setCopied] = useState(''); const [pollTimer, setPollTimer] = useState(null); const load = useCallback(async () => { if (!orderId) return; setLoading(true); const result = await api.payment.checkStatus(orderId); setLoading(false); if (result.success && result.order) { setOrder(result.order); if (result.order.status === 'paid' || result.order.status === 'confirmed') { setStep('paid'); } else if (result.order.status === 'sent' || result.order.status === 'pending_payment_sent') { setStep('sent'); } else if (result.order.status === 'failed' || result.order.status === 'expired') { setStep('failed'); } } else { toast({ title: 'Order not found', variant: 'destructive' }); } }, [orderId, toast]); useEffect(() => { load(); }, [load]); // Auto-poll when in 'sent' or 'verifying' state useEffect(() => { if (step !== 'sent' && step !== 'verifying') { if (pollTimer) clearInterval(pollTimer); return; } const interval = setInterval(async () => { const result = await api.payment.checkStatus(orderId); if (result.success && result.order) { if (result.order.status === 'paid' || result.order.status === 'confirmed') { setOrder(result.order); setStep('paid'); clearInterval(interval); } } }, 8000); setPollTimer(interval); return () => clearInterval(interval); }, [step, orderId]); const copyText = (text: string, label: string) => { navigator.clipboard.writeText(text); setCopied(label); setTimeout(() => setCopied(''), 2000); }; const confirmSent = async () => { setStep('verifying'); const result = await api.payment.confirmSent(orderId); if (result.success) { setStep('sent'); toast({ title: 'Payment confirmation started', description: 'We will verify your transfer automatically.' }); } else { setStep('details'); toast({ title: 'Error', description: result.error, variant: 'destructive' }); } }; if (loading) { return ; } if (!order) { return (

Payment

Order not found

Back to cart
); } return (
{/* Top bar */}

Complete Payment

{/* Stepper */}
{[ { key: 'details', label: 'Transfer', icon: Banknote }, { key: 'sent', label: 'Confirm', icon: Clock }, { key: 'paid', label: 'Done', icon: CheckCircle2 }, ].map((s, i) => { const isActive = step === s.key || (step === 'verifying' && s.key === 'sent') || (step === 'paid' && s.key === 'paid'); const isDone = (step === 'sent' && s.key === 'details') || (step === 'verifying' && s.key === 'details') || (step === 'paid' && i < 2); const Icon = s.icon; return (
{s.label}
{i < 2 && (
)}
); })}
{/* Step: Details - show bank info */} {(step === 'details' || step === 'sent' || step === 'verifying') && (

Bank Transfer Details

Account number
Account name
{PALMPAY_NAME}
Bank
{PALMPAY_BANK}
Amount to transfer
Order reference #{order.id?.slice(0, 8)}
Transfer exactly {formatPrice(order.total)} to the account above. Your payment will be auto-verified within 30 seconds of transfer.
{step === 'details' && ( )} {step === 'sent' && (
Verifying your payment — this usually takes 30 seconds
)} {step === 'verifying' && (
Confirming...
)}
)} {/* Step: Paid - success */} {step === 'paid' && (

Payment confirmed!

Thank you for your purchase. Your order has been placed successfully.

Order ID#{order.id?.slice(0, 8)}
Amount paid{formatPrice(order.total)}
Items{order.item_count} item(s)
StatusConfirmed
)} {/* Step: Failed */} {step === 'failed' && (

Payment failed

We couldn't verify your payment. Please try again or contact support.

)}
); } export default function PaymentPage() { return ( }> ); }