'use client'; import React, { useState } from 'react'; import { loadStripe, Stripe } from '@stripe/stripe-js'; import axios from 'axios'; interface Plan { name: string; desc: string; monthly: number; yearly: number; features: string[]; btnStyle: string; popular?: boolean; planId: string; } // ⚡ Lazy load Stripe to avoid undefined.match errors let stripePromise: Promise | null = null; const getStripe = () => { if (!stripePromise) { const key = process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY; if (!key) throw new Error('Stripe publishable key is not defined!'); stripePromise = loadStripe(key); } return stripePromise; }; const ComponentsPricingTableToggle: React.FC = () => { const [yearlyPrice, setYearlyPrice] = useState(false); const plans: Plan[] = [ { name: 'Starter SEO Crawl', desc: 'Perfect for small websites. Crawl up to 5,000 pages per month.', monthly: 19, yearly: 19 * 12 * 0.8, features: ['5,000 Pages/Month', 'Basic Crawl Reports', 'Email Support'], btnStyle: 'btn-dark', planId: 'starter-seo-crawl', }, { name: 'Pro SEO Crawl', desc: 'Best for medium websites. Crawl up to 50,000 pages per month.', monthly: 49, yearly: 49 * 12 * 0.8, features: ['50,000 Pages/Month', 'Advanced Crawl Reports', 'Priority Support'], btnStyle: 'btn-primary', popular: true, planId: 'pro-seo-crawl', }, { name: 'Enterprise SEO Crawl', desc: 'For large-scale websites. Unlimited crawling and dedicated support.', monthly: 199, yearly: 199 * 12 * 0.8, features: ['Unlimited Pages', 'Custom Integrations', 'Dedicated Account Manager'], btnStyle: 'btn-dark', planId: 'enterprise-seo-crawl', }, ]; // 🔹 Stripe Checkout handler const handleBuyNow = async (plan: Plan) => { try { const stripe = await getStripe(); if (!stripe) return; // 🔹 Call backend API to create Checkout Session const { data } = await axios.post( 'https://api.crawlerx.co/api/payment/create-intent', { planId: plan.planId, price: yearlyPrice ? plan.yearly : plan.monthly, billing: yearlyPrice ? 'yearly' : 'monthly', } ); if (!data.sessionId) { console.error('No sessionId returned from backend!'); return; } // 🔹 Redirect to Stripe Checkout const { error } = await stripe.redirectToCheckout({ sessionId: data.sessionId }); if (error) console.error('Stripe redirect error:', error.message); } catch (err: any) { console.error('Error creating checkout session:', err.response?.data || err.message); } }; return (
{/* Toggle */}
Monthly Yearly 20% Off
{/* Plans */}
{plans.map((plan, idx) => (
{plan.popular && (
Most Popular
)}

{plan.name}

{plan.desc}

${yearlyPrice ? plan.yearly.toFixed(0) : plan.monthly} {' '} / {yearlyPrice ? 'yearly' : 'monthly'}
{plan.name} Features
    {plan.features.map((f, i) => (
  • {f}
  • ))}
))}
); }; export default ComponentsPricingTableToggle;