import React, { useState } from 'react'; import { loadStripe } from '@stripe/stripe-js'; import { Elements, CardElement, useStripe, useElements } from '@stripe/react-stripe-js'; import './UpgradeFlow.css'; const stripePromise = loadStripe(import.meta.env.VITE_STRIPE_PUBLISHABLE_KEY || 'pk_test_51QTaIiRu6l8tVuJxtest_placeholder'); /** * Checkout form component */ function CheckoutForm({ tier, domain, onSuccess }) { const stripe = useStripe(); const elements = useElements(); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const handleSubmit = async (e) => { e.preventDefault(); if (!stripe || !elements) return; setLoading(true); setError(null); try { // Create payment method const { error: pmError, paymentMethod } = await stripe.createPaymentMethod({ type: 'card', card: elements.getElement(CardElement) }); if (pmError) { throw new Error(pmError.message); } // Subscribe or register domain const endpoint = domain ? '/api/premium/domains/register' : '/api/premium/subscribe'; const body = domain ? { domain: domain, walletAddress: window.ethereum?.selectedAddress || '0x0000000000000000000000000000000000000000', paymentMethodId: paymentMethod.id } : { tier: tier, paymentMethodId: paymentMethod.id, billingPeriod: 'yearly' }; const response = await fetch(endpoint, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem('token')}` }, body: JSON.stringify(body) }); const data = await response.json(); if (data.success) { onSuccess(data); } else { throw new Error(data.error || 'Subscription failed'); } } catch (err) { setError(err.message); } finally { setLoading(false); } }; const getAmount = () => { if (domain) return '$100/year'; if (tier === 'premium') return '$100/year'; if (tier === 'enterprise') return '$500/month'; return '$0'; }; return (
); } /** * Main upgrade flow component */ export default function UpgradeFlow({ currentTier = 'free' }) { const [selectedTier, setSelectedTier] = useState('premium'); const [domainName, setDomainName] = useState(''); const [domainAvailable, setDomainAvailable] = useState(null); const [checkingDomain, setCheckingDomain] = useState(false); const checkDomain = async () => { if (!domainName) { setError('Please enter a domain name'); return; } setCheckingDomain(true); setDomainAvailable(null); try { const response = await fetch('/api/premium/domains/check-availability', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem('token')}` }, body: JSON.stringify({ domain: `${domainName}.aethex` }) }); const data = await response.json(); if (data.success) { setDomainAvailable(data); } else { throw new Error(data.error); } } catch (error) { console.error('Failed to check domain:', error); setDomainAvailable({ available: false, domain: `${domainName}.aethex`, error: error.message }); } finally { setCheckingDomain(false); } }; const handleSuccess = (data) => { alert('Subscription successful! Welcome to premium!'); // Redirect to dashboard or show success modal window.location.href = '/dashboard'; }; return (Your premium blockchain domain with NFT ownership proof
✓ {domainAvailable.domain} is available!
Price: ${domainAvailable.price}/year
> ) : (✗ {domainAvailable.domain} is taken
{domainAvailable.error && ({domainAvailable.error}
)} {domainAvailable.suggestedAlternatives && domainAvailable.suggestedAlternatives.length > 0 && ( <>Try these alternatives:
For Enterprise plans, please contact our sales team: