'use client'; import { useState } from 'react'; import { loadStripe } from '@stripe/stripe-js'; import { Elements } from '@stripe/react-stripe-js'; import PaymentForm from './components/PaymentForm'; import './globals.css'; const stripePromise = loadStripe(''); const products = [ { id: '1', name: 'Premium Widget', price: 2999, description: 'High-quality widget for professionals' }, { id: '2', name: 'Standard Widget', price: 1999, description: 'Reliable widget for everyday use' }, { id: '3', name: 'Basic Widget', price: 999, description: 'Entry-level widget for beginners' } ]; export default function Home() { const [selectedProduct, setSelectedProduct] = useState(null); const [clientSecret, setClientSecret] = useState(''); const [orderId, setOrderId] = useState(''); const [loading, setLoading] = useState(false); const handleProductSelect = async (product) => { setLoading(true); setSelectedProduct(product); const newOrderId = Math.floor(Math.random() * 10000).toString(); setOrderId(newOrderId); try { const response = await fetch('https://impr.ink/api/stripe/create-payment-intent', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ amount: product.price, orderId: newOrderId }), }); const data = await response.json(); if (data.clientSecret) { setClientSecret(data.clientSecret); } else { console.error('Error creating payment intent:', data.error); } } catch (error) { console.error('Error:', error); } finally { setLoading(false); } }; const handlePaymentSuccess = () => { setSelectedProduct(null); setClientSecret(''); setOrderId(''); }; const handleBackToProducts = () => { setSelectedProduct(null); setClientSecret(''); setOrderId(''); }; const appearance = { theme: 'stripe', variables: { colorPrimary: '#0570de', colorBackground: '#ffffff', colorText: '#30313d', colorDanger: '#df1b41', fontFamily: 'Ideal Sans, system-ui, sans-serif', spacingUnit: '2px', borderRadius: '4px', }, }; const options = { clientSecret, appearance, }; return (

🛍️ Stripe Payment Demo

Select a product to purchase

{!selectedProduct ? (

Products

{products.map((product) => (

{product.name}

{product.description}

${(product.price / 100).toFixed(2)}

))}
) : (

Order Summary

Product: {selectedProduct.name}

Order ID: {orderId}

Amount: ${(selectedProduct.price / 100).toFixed(2)}

{clientSecret && ( )}
)}
); }