import React, { useState } from 'react'; import { Elements } from '@stripe/react-stripe-js'; import { loadStripe } from '@stripe/stripe-js'; import type { StripeElementsOptions } from '@stripe/stripe-js'; import { format } from 'date-fns'; import { GlassCard } from '../../../shared-minimal/components/mobile/GlassCard'; import { MobileContainer } from '../../../shared-minimal/components/mobile/MobileContainer'; import { PaymentMethodForm } from '../components/PaymentMethodForm'; import { DonationSectionMobile } from '../components/DonationSectionMobile'; import { useSubscription, useCheckout, useCancelSubscription, useReactivateSubscription, useInvoices, } from '../hooks/useSubscription'; import { PLANS } from '../constants/plans'; import type { BillingCycle, SubscriptionTier, SubscriptionPlan } from '../types/subscription.types'; const stripePromise = loadStripe(import.meta.env.VITE_STRIPE_PUBLISHABLE_KEY || ''); const paymentElementsOptions: StripeElementsOptions = { mode: 'setup', currency: 'usd', paymentMethodCreation: 'manual', }; interface MobileTierCardProps { plan: SubscriptionPlan; billingCycle: BillingCycle; currentTier?: string; isLoading?: boolean; onUpgrade: () => void; } const MobileTierCard: React.FC = ({ plan, billingCycle, currentTier, isLoading = false, onUpgrade, }) => { const isCurrent = currentTier === plan.tier; const price = billingCycle === 'monthly' ? plan.monthlyPrice : plan.yearlyPrice; const priceLabel = billingCycle === 'monthly' ? '/month' : '/year'; return ( {isCurrent && (
Current Plan
)}

{plan.name}

${price.toFixed(2)}
{priceLabel}
{isCurrent ? ( ) : ( )}
); }; interface MobileModalProps { isOpen: boolean; onClose: () => void; title: string; children: React.ReactNode; } const MobileModal: React.FC = ({ isOpen, onClose, title, children }) => { if (!isOpen) return null; return (

{title}

{children}
); }; export const SubscriptionMobileScreen: React.FC = () => { const [billingCycle, setBillingCycle] = useState('monthly'); const [selectedTier, setSelectedTier] = useState(null); const [showPaymentDialog, setShowPaymentDialog] = useState(false); const { data: subscriptionData, isLoading: isLoadingSubscription } = useSubscription(); const { data: invoicesData, isLoading: isLoadingInvoices } = useInvoices(); const checkoutMutation = useCheckout(); const cancelMutation = useCancelSubscription(); const reactivateMutation = useReactivateSubscription(); const subscription = subscriptionData?.data; const invoices = invoicesData?.data || []; const handleUpgradeClick = (tier: SubscriptionTier) => { setSelectedTier(tier); setShowPaymentDialog(true); }; const handlePaymentSubmit = (paymentMethodId: string) => { if (!selectedTier) return; checkoutMutation.mutate( { tier: selectedTier, billingCycle, paymentMethodId, }, { onSuccess: () => { setShowPaymentDialog(false); setSelectedTier(null); }, } ); }; const handleCancel = () => { if (window.confirm('Are you sure you want to cancel your subscription? Your plan will remain active until the end of the current billing period.')) { cancelMutation.mutate(); } }; const handleReactivate = () => { reactivateMutation.mutate(); }; const getStatusColor = (status: string) => { switch (status) { case 'active': return 'bg-green-100 text-green-800 dark:bg-green-900/30 dark:text-green-400'; case 'past_due': return 'bg-yellow-100 text-yellow-800 dark:bg-yellow-900/30 dark:text-yellow-400'; case 'canceled': return 'bg-red-100 text-red-800 dark:bg-red-900/30 dark:text-red-400'; default: return 'bg-gray-100 text-gray-800 dark:bg-gray-900/30 dark:text-gray-400'; } }; if (isLoadingSubscription) { return (
); } return (

Subscription

{subscription && (
{subscription.tier.toUpperCase()} {subscription.status}

Current Plan: {PLANS.find((p) => p.tier === subscription.tier)?.name || subscription.tier}

{subscription.currentPeriodEnd && (

Next billing date: {format(new Date(subscription.currentPeriodEnd), 'MMM dd, yyyy')}

)} {subscription.cancelAtPeriodEnd && (

Your subscription will be canceled at the end of the current billing period.

)}
{subscription.cancelAtPeriodEnd ? ( ) : subscription.tier !== 'free' ? ( ) : null}
)}

Available Plans

{PLANS.map((plan) => ( handleUpgradeClick(plan.tier)} /> ))}

Billing History

{isLoadingInvoices ? (
) : invoices.length === 0 ? (

No billing history available

) : (
{invoices.map((invoice: { id: string; date: string; amount: number; status: string; pdfUrl?: string }) => (
{format(new Date(invoice.date), 'MMM dd, yyyy')}
${(invoice.amount / 100).toFixed(2)}
{invoice.status} {invoice.pdfUrl && ( )}
))}
)}
!checkoutMutation.isPending && setShowPaymentDialog(false)} title={`Upgrade to ${selectedTier && PLANS.find((p) => p.tier === selectedTier)?.name}`} >
); };