"use client" import { useState } from "react" import { Card, CardContent, CardHeader, CardTitle, CardDescription, CardFooter } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { Check, X, ShieldCheck, Loader2, Minus } from "lucide-react" import { useActiveUser } from "@/hooks/useActiveUser" const WEBHOOK_SECRET = "local_webhook_secret" export default function UpgradePage() { const { userId, loading: userLoading, refreshUser } = useActiveUser() const [loadingTier, setLoadingTier] = useState(null) const [showModal, setShowModal] = useState(false) const [orderData, setOrderData] = useState(null) const [processing, setProcessing] = useState(false) const [success, setSuccess] = useState(false) const handleUpgrade = async (tier: string) => { if (userLoading || !userId) return setLoadingTier(tier) try { const res = await fetch(`/api/billing/create-order?user_id=${userId}&plan_tier=${tier}`, { method: "POST" }) if (!res.ok) throw new Error("Order creation failed") const order = await res.json() setOrderData({ ...order, tier }) setShowModal(true) } catch (err) { console.error(err) alert("Failed to create order") } finally { setLoadingTier(null) } } const generateMockSignature = async (payloadStr: string) => { const encoder = new TextEncoder(); const key = await crypto.subtle.importKey( 'raw', encoder.encode(WEBHOOK_SECRET), { name: 'HMAC', hash: 'SHA-256' }, false, ['sign'] ); const signature = await crypto.subtle.sign('HMAC', key, encoder.encode(payloadStr)); return Array.from(new Uint8Array(signature)).map(b => b.toString(16).padStart(2, '0')).join(''); } const simulatePayment = async () => { if (!userId) return setProcessing(true) try { const payloadObj = { event: "payment.captured", payload: { payment: { entity: { id: `pay_mock_${Date.now()}`, amount: orderData.amount, notes: { user_id: userId, tier_requested: orderData.tier } } } } } const payloadStr = JSON.stringify(payloadObj) const signature = await generateMockSignature(payloadStr) const res = await fetch(`/api/billing/webhook/razorpay`, { method: "POST", headers: { "Content-Type": "application/json", "X-Razorpay-Signature": signature }, body: payloadStr }) if (!res.ok) throw new Error("Webhook failed") setSuccess(true) await refreshUser() setTimeout(() => { window.location.href = "/dashboard" }, 2000) } catch (err) { console.error(err) alert("Payment simulation failed") setProcessing(false) } } return (

Upgrade Your Preparation

Unlock the full power of PragatiVeer to master JEE & NEET.

{/* Free Tier */} Free Basic access for everyone.
₹10/month
Subject-wise diagnostics
Basic progress tracking
Tests
AI Spaced Repetition
Parent Analytics
{/* Pro Tier */} Pro For serious aspirants.
₹1299/month
Everything in Free
Unlimited Sectional Tests
Performance Analytics
Parent Dashboard
AI Mistake Heatmap
Spaced Repetition (SM-2)
{/* AI Coach Tier */} AI Coach Personalized mastery loop.
₹1599/month
Everything in Pro
Daily AI Error Review Queue
SM-2 Spaced Repetition
Mistake Heatmap Tracking
AI Chat Tutor Support
{/* Mock Razorpay Modal */} {showModal && (
RZP
Mock Checkout Window Razorpay Test Sandbox Simulation
Order ID: {orderData?.id}
Amount: ₹{(orderData?.amount / 100).toFixed(2)}
Tier: {orderData?.tier}
{success ? (
Payment Captured! Redirecting...
) : (

Clicking confirm will securely sign and POST a payment.captured webhook to the backend.

)}
)}
) }