| 1 | import { useState, useEffect } from 'react'; |
| 2 | import { CreditCard, Zap, ArrowRight, CheckCircle2, AlertCircle, ExternalLink, Sparkles } from 'lucide-react'; |
| 3 | import { api } from '../api.js'; |
| 4 | |
| 5 | const PLANS = [ |
| 6 | { |
| 7 | id: 'free', |
| 8 | name: 'Free', |
| 9 | price: 0, |
| 10 | features: ['1 agent', '20 runs / month', 'Own Anthropic API key required', 'Community support'], |
| 11 | }, |
| 12 | { |
| 13 | id: 'starter', |
| 14 | name: 'Starter', |
| 15 | price: 29, |
| 16 | highlight: true, |
| 17 | features: ['5 agents', '200 runs / month', 'Platform API key included', 'Email support'], |
| 18 | }, |
| 19 | { |
| 20 | id: 'pro', |
| 21 | name: 'Pro', |
| 22 | price: 99, |
| 23 | features: ['20 agents', 'Unlimited runs', 'Platform API key included', 'Priority support'], |
| 24 | }, |
| 25 | ]; |
| 26 | |
| 27 | export default function Billing() { |
| 28 | const [billing, setBilling] = useState(null); |
| 29 | const [loading, setLoading] = useState(true); |
| 30 | const [busy, setBusy] = useState(null); |
| 31 | const [error, setError] = useState(''); |
| 32 | |
| 33 | useEffect(() => { |
| 34 | api('/api/billing/me').then(setBilling).catch(() => {}).finally(() => setLoading(false)); |
| 35 | // Handle Stripe redirect back |
| 36 | const params = new URLSearchParams(window.location.search); |
| 37 | if (params.get('success')) { |
| 38 | setTimeout(() => { |
| 39 | api('/api/billing/me').then(setBilling).catch(() => {}); |
| 40 | window.history.replaceState({}, '', '/billing'); |
| 41 | }, 2000); |
| 42 | } |
| 43 | }, []); |
| 44 | |
| 45 | async function checkout(planId) { |
| 46 | setBusy(planId); setError(''); |
| 47 | try { |
| 48 | const { url } = await api('/api/billing/checkout', { |
| 49 | method: 'POST', |
| 50 | body: JSON.stringify({ planId }), |
| 51 | }); |
| 52 | window.location.href = url; |
| 53 | } catch (e) { |
| 54 | setError(e.message || 'Stripe not configured yet — add STRIPE_SECRET_KEY to .env'); |
| 55 | setBusy(null); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | async function openPortal() { |
| 60 | setBusy('portal'); setError(''); |
| 61 | try { |
| 62 | const { url } = await api('/api/billing/portal', { method: 'POST' }); |
| 63 | window.open(url, '_blank'); |
| 64 | } catch (e) { |
| 65 | setError(e.message); |
| 66 | } finally { setBusy(null); } |
| 67 | } |
| 68 | |
| 69 | const currentPlan = billing?.plan || 'free'; |
| 70 | const params = new URLSearchParams(window.location.search); |
| 71 | const justUpgraded = params.get('success') === '1'; |
| 72 | |
| 73 | if (loading) return <div className="p-6 text-slate-500 text-sm">Loading…</div>; |
| 74 | |
| 75 | return ( |
| 76 | <div className="h-full overflow-y-auto p-6 max-w-4xl"> |
| 77 | <h1 className="text-lg font-semibold text-slate-100 mb-1 flex items-center gap-2"> |
| 78 | <CreditCard size={16} className="text-violet-300" /> Billing & Plan |
| 79 | </h1> |
| 80 | <p className="text-xs text-slate-500 mb-6">Manage your subscription and limits.</p> |
| 81 | |
| 82 | {justUpgraded && ( |
| 83 | <div className="mb-4 flex items-center gap-2 rounded-xl border border-emerald-500/30 bg-emerald-500/10 px-4 py-3 text-sm text-emerald-300"> |
| 84 | <CheckCircle2 size={14} /> Subscription activated — welcome to {PLANS.find(p => p.id === currentPlan)?.name}! |
| 85 | </div> |
| 86 | )} |
| 87 | |
| 88 | {error && ( |
| 89 | <div className="mb-4 flex items-start gap-2 rounded-xl border border-rose-500/30 bg-rose-500/10 px-4 py-3 text-sm text-rose-300"> |
| 90 | <AlertCircle size={14} className="mt-0.5 flex-shrink-0" /> |
| 91 | <span className="font-mono text-xs">{error}</span> |
| 92 | </div> |
| 93 | )} |
| 94 | |
| 95 | {/* Current plan status */} |
| 96 | <div className="card mb-6"> |
| 97 | <div className="flex items-center justify-between"> |
| 98 | <div> |
| 99 | <div className="text-xs uppercase tracking-wider text-slate-500 mb-1">Current plan</div> |
| 100 | <div className="text-xl font-bold text-slate-100"> |
| 101 | {PLANS.find(p => p.id === currentPlan)?.name || 'Free'} |
| 102 | </div> |
| 103 | {billing?.planExpiresAt && ( |
| 104 | <div className="text-xs text-slate-500 mt-1"> |
| 105 | Renews {new Date(billing.planExpiresAt).toLocaleDateString('en')} |
| 106 | </div> |
| 107 | )} |
| 108 | {billing?.subscriptionStatus && billing.subscriptionStatus !== 'free' && ( |
| 109 | <div className={`text-xs mt-1 ${billing.subscriptionStatus === 'active' ? 'text-emerald-400' : 'text-amber-400'}`}> |
| 110 | {billing.subscriptionStatus} |
| 111 | </div> |
| 112 | )} |
| 113 | </div> |
| 114 | <div className="text-right text-sm text-slate-400"> |
| 115 | <div>{billing?.maxAgents ?? 1} agents</div> |
| 116 | <div>{billing?.maxRunsPerMonth ? `${billing.maxRunsPerMonth} runs/mo` : 'Unlimited runs'}</div> |
| 117 | </div> |
| 118 | </div> |
| 119 | {billing?.stripeCustomerId && ( |
| 120 | <button |
| 121 | onClick={openPortal} |
| 122 | disabled={busy === 'portal'} |
| 123 | className="btn-ghost text-xs mt-3" |
| 124 | > |
| 125 | <ExternalLink size={12} /> |
| 126 | {busy === 'portal' ? 'Opening…' : 'Manage subscription in Stripe portal'} |
| 127 | </button> |
| 128 | )} |
| 129 | </div> |
| 130 | |
| 131 | {/* Plan cards */} |
| 132 | <div className="grid grid-cols-1 md:grid-cols-3 gap-4"> |
| 133 | {PLANS.map(plan => { |
| 134 | const isCurrent = plan.id === currentPlan; |
| 135 | return ( |
| 136 | <div key={plan.id} className={`card flex flex-col gap-4 ${plan.highlight ? 'border-violet/50 relative' : ''}`}> |
| 137 | {plan.highlight && ( |
| 138 | <div className="absolute -top-3 left-1/2 -translate-x-1/2 inline-flex items-center gap-1 rounded-full bg-violet text-white text-[10px] font-semibold px-3 py-1"> |
| 139 | <Sparkles size={10} /> Most popular |
| 140 | </div> |
| 141 | )} |
| 142 | <div> |
| 143 | <div className="text-sm font-semibold text-slate-300">{plan.name}</div> |
| 144 | <div className="flex items-end gap-1 mt-1"> |
| 145 | <span className="text-2xl font-bold text-slate-100">${plan.price}</span> |
| 146 | {plan.price > 0 && <span className="text-slate-500 text-xs mb-1">/month</span>} |
| 147 | </div> |
| 148 | </div> |
| 149 | <ul className="space-y-2 flex-1"> |
| 150 | {plan.features.map(f => ( |
| 151 | <li key={f} className="flex items-center gap-2 text-xs text-slate-300"> |
| 152 | <CheckCircle2 size={12} className="text-emerald-400 flex-shrink-0" /> {f} |
| 153 | </li> |
| 154 | ))} |
| 155 | </ul> |
| 156 | {isCurrent ? ( |
| 157 | <div className="flex items-center gap-2 text-xs text-emerald-400 font-medium"> |
| 158 | <CheckCircle2 size={12} /> Current plan |
| 159 | </div> |
| 160 | ) : ( |
| 161 | <button |
| 162 | onClick={() => plan.id !== 'free' ? checkout(plan.id) : null} |
| 163 | disabled={!!busy || plan.id === 'free'} |
| 164 | className={`btn-primary justify-center py-2 text-xs ${plan.id === 'free' ? 'opacity-40 cursor-not-allowed' : ''}`} |
| 165 | > |
| 166 | {busy === plan.id ? 'Redirecting…' : plan.id === 'free' ? 'Downgrade' : `Upgrade to ${plan.name}`} |
| 167 | {busy !== plan.id && plan.id !== 'free' && <ArrowRight size={12} />} |
| 168 | </button> |
| 169 | )} |
| 170 | </div> |
| 171 | ); |
| 172 | })} |
| 173 | </div> |
| 174 | |
| 175 | <p className="text-xs text-slate-600 mt-6"> |
| 176 | Payments are processed securely by Stripe. Cancel anytime from the Stripe portal. |
| 177 | Questions? <a href="mailto:support@utopiaia.com" className="text-slate-500 hover:text-slate-400">support@utopiaia.com</a> |
| 178 | </p> |
| 179 | </div> |
| 180 | ); |
| 181 | } |