'use client'; import { useState } from 'react'; import { useAuth } from '../context/AuthContext'; import { getStripe } from '../lib/stripe'; interface PaymentModalProps { isOpen: boolean; onClose: () => void; box: { id: string; tier: string; price: number; description: string; } | null; } export default function PaymentModal({ isOpen, onClose, box }: PaymentModalProps) { const { user } = useAuth(); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const handlePayment = async () => { if (!user || !box) return; setIsLoading(true); setError(null); try { console.log('Creating checkout session for:', box); // Create checkout session const response = await fetch('/api/create-checkout-session', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ boxId: box.id, boxName: box.tier, amount: box.price, currency: 'usd', }), }); console.log('Checkout session response status:', response.status); if (!response.ok) { const errorData = await response.json(); console.error('Checkout session error:', errorData); throw new Error(errorData.error || 'Failed to create checkout session'); } const { sessionId } = await response.json(); console.log('Checkout session created:', { sessionId }); if (!sessionId) { throw new Error('No session ID received from server'); } // Redirect to Stripe Checkout const stripe = await getStripe(); if (!stripe) { throw new Error('Stripe failed to load'); } console.log('Redirecting to Stripe Checkout...'); const { error: stripeError } = await stripe.redirectToCheckout({ sessionId, }); if (stripeError) { console.error('Stripe error:', stripeError); throw new Error(stripeError.message); } } catch (err) { console.error('Payment error:', err); setError(err instanceof Error ? err.message : 'Payment failed'); } finally { setIsLoading(false); } }; if (!isOpen || !box) return null; return (
{/* Header */}

Open {box.tier} Box

{/* Box Details */}
🎁

{box.tier} Box

{box.description}

Price: ${box.price}
{/* Error Message */} {error && (

{error}

)} {/* Action Buttons */}
{/* Security Notice */}

Your payment is secured by Stripe. We never store your card details.

); }