"use client"; import React, { useEffect, useState } from "react"; import axios from "axios"; import { useRouter } from "next/navigation"; interface Plan { name: string; desc: string; amountMonthly: number; amountYearly: number; features: string[]; planId: string; yearlyPlanId: string; popular?: boolean; } interface CheckoutSessionResponse { url: string; } const StripePlans: React.FC = () => { const [loadingPlanId, setLoadingPlanId] = useState(null); const [billingCycle, setBillingCycle] = useState<"monthly" | "yearly">("monthly"); const [userId, setUserId] = useState(null); const [user, setUser] = useState(null); const router = useRouter(); // 🧠 Check localStorage-based UID (manual login) useEffect(() => { const uid = localStorage.getItem("data4auto_uid"); const userEmail = localStorage.getItem("d4a_email"); if (uid && userEmail) { setUserId(uid); setUser(userEmail); } else { // ✅ If no uid in localStorage, check Google cookie axios .get("https://ebay.backend.data4autos.com/api/auth/protected", { withCredentials: true, }) .then((res: any) => { setUser(res.data.user?.email); setUserId(res.data.user.userid); localStorage.setItem("data4auto_uid", res.data.user.userid); }) .catch(() => { router.push("/login"); }); } }, [router]); const handleBuyNow = async (plan: Plan) => { try { setLoadingPlanId(plan.planId); const selectedPlanId = billingCycle === "yearly" ? plan.yearlyPlanId : plan.planId; const response = await axios.post( "https://ebay.backend.data4autos.com/api/payment/create-checkout-session", { email: user, planId: selectedPlanId } ); const { url } = response.data; if (!url) throw new Error("No checkout URL returned from backend"); window.location.href = url; } catch (err: any) { console.error("Error creating checkout session:", err); alert( "Checkout failed: " + (err?.response?.data?.error || err?.message || "Unknown error") ); } finally { setLoadingPlanId(null); } }; const plans: Plan[] = [ { name: "Starter Sync", desc: "Upload up to 100 products per month", amountMonthly: 49, amountYearly: 499, features: [ "Auto price & inventory updates", "Daily sync", "Manual sync option", "Basic reporting dashboard", ], planId: "starter_monthly", yearlyPlanId: "starter_yearly", }, { name: "Growth Sync", desc: "Upload up to 250 products per month", amountMonthly: 99, amountYearly: 999, features: [ "Everything in Starter", "3-hour sync interval", "Bulk product import", "Priority email support", ], planId: "growth_monthly", yearlyPlanId: "growth_yearly", popular: true, }, { name: "Pro Sync", desc: "Upload up to 1000 products per month", amountMonthly: 249, amountYearly: 2499, features: [ "Everything in Growth", "Real-time sync", "Advanced analytics dashboard", "Dedicated account manager", "API access", ], planId: "pro_monthly", yearlyPlanId: "pro_yearly", }, ]; return (
{/* Header */}

Choose Your Plan

Subscribe to a plan and start automating your eBay listings instantly.

{/* Billing Cycle Toggle */}
{/* Plans */}
{plans.map((plan) => (
{/* Badge */} {plan.popular && (
MOST POPULAR
)} {/* Title */}

{plan.name}

{plan.desc}

{/* Price */}
$ {billingCycle === "monthly" ? plan.amountMonthly : plan.amountYearly} {billingCycle === "monthly" ? " / month" : " / year"}
{/* Features */}
    {plan.features.map((feature, i) => (
  • ✔ {feature}
  • ))}
{/* Button */}
))}
); }; export default StripePlans;