"use client"; import { Loader2 } from "lucide-react"; import { useRouter } from "next/navigation"; import { useSession } from "next-auth/react"; import { useState } from "react"; import { cn } from "@/lib/utils"; export function PricingActionButton({ planId, planName, checkoutEnabled, highlight, }: { planId: string; planName: string; checkoutEnabled: boolean; highlight?: boolean; }) { const { data: session, status } = useSession(); const router = useRouter(); const [isPending, setIsPending] = useState(false); async function handleCheckout() { if (isPending) { return; } if (status === "loading") { return; } if (!checkoutEnabled) { window.alert("支付尚未配置,请先在环境变量中配置 Stripe Price ID。"); return; } if (!session?.user || session.user.type === "guest") { router.push("/login?callbackUrl=/lovart/pricing"); return; } setIsPending(true); try { const response = await fetch("/api/billing/checkout", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ planId }), }); const data = (await response.json()) as { error?: string; url?: string }; if (!response.ok || !data.url) { window.alert(data.error || "创建支付会话失败,请稍后再试。"); return; } window.location.assign(data.url); } catch (error) { console.error("[PricingActionButton] checkout error", error); window.alert("创建支付会话失败,请稍后再试。"); } finally { setIsPending(false); } } return ( ); }