From 6675a76d0b2166d4ae2e34c331b4967b65e7295f Mon Sep 17 00:00:00 2001 From: Usman Baig Date: Fri, 30 Jan 2026 22:52:04 +0100 Subject: [PATCH] feat: implement subscription handling in PricingSection component; add OAuth flow and checkout session creation for user plans --- components/PricingSection.tsx | 46 ++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/components/PricingSection.tsx b/components/PricingSection.tsx index 8bb2a42..9aa215d 100644 --- a/components/PricingSection.tsx +++ b/components/PricingSection.tsx @@ -2,6 +2,10 @@ import { useState } from 'react' import { Button, CheckCircleIcon } from '@ciphera-net/ui' +import { useAuth } from '@/lib/auth/context' +import { initiateOAuthFlow } from '@/lib/api/oauth' +import { toast } from 'sonner' +import { getClient } from '@/lib/api/client' // 1. Define Plans with IDs and Site Limits const PLANS = [ @@ -98,6 +102,8 @@ const TRAFFIC_TIERS = [ export default function PricingSection() { const [isYearly, setIsYearly] = useState(false) const [sliderIndex, setSliderIndex] = useState(2) // Default to 100k (index 2) + const [loadingPlan, setLoadingPlan] = useState(null) + const { user } = useAuth() const currentTraffic = TRAFFIC_TIERS[sliderIndex] @@ -119,6 +125,42 @@ export default function PricingSection() { } } + const handleSubscribe = async (planId: string) => { + try { + setLoadingPlan(planId) + + // 1. If not logged in, redirect to login/signup + if (!user) { + initiateOAuthFlow() // Or redirect to /signup?plan=... + return + } + + // 2. Call backend to create checkout session + const client = getClient() + const interval = isYearly ? 'year' : 'month' + const limit = currentTraffic.value + + const res = await client.post('/api/billing/checkout', { + plan_id: planId, + interval: interval, + limit: limit + }) + + // 3. Redirect to Stripe Checkout + if (res.data.url) { + window.location.href = res.data.url + } else { + throw new Error('No checkout URL returned') + } + + } catch (error: any) { + console.error('Checkout error:', error) + toast.error('Failed to start checkout. Please try again.') + } finally { + setLoadingPlan(null) + } + } + return (
@@ -236,13 +278,15 @@ export default function PricingSection() {