import Link from "next/link"; import { redirect } from "next/navigation"; import { CalendarClock, CreditCard, ShieldCheck, Sparkles } from "lucide-react"; import { auth } from "@/app/(auth)/auth"; import { ManageSubscriptionButton } from "@/components/billing/manage-subscription-button"; import { getCheckoutEnabledPlans, isWechatPayEnabled, resolvePlanByMonthlyImageLimit, } from "@/lib/billing/plans"; import { getBillingTimelineByUser, getUserById } from "@/lib/db/queries"; function formatLimit(limit: number | null) { if (limit == null || limit === 0) { return "无限制"; } return `${limit.toLocaleString("zh-CN")} 张 / 月`; } function formatDateTime(value: string) { const date = new Date(value); if (Number.isNaN(date.getTime())) { return value; } return new Intl.DateTimeFormat("zh-CN", { year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", }).format(date); } function formatEventType(type: string) { switch (type) { case "checkout.session.completed": return "支付完成"; case "customer.subscription.updated": return "订阅更新"; case "customer.subscription.deleted": return "订阅取消"; default: return type; } } function formatChannel(channel: string) { if (channel === "wechat") { return "微信"; } return "Stripe"; } export default async function BillingPage() { const session = await auth(); if (!session?.user?.id) { redirect("/login?callbackUrl=/billing"); } const user = await getUserById({ id: session.user.id }); if (!user) { redirect("/login?callbackUrl=/billing"); } const [billingEvents, checkoutEnabledPlans, wechatEnabled] = await Promise.all([ getBillingTimelineByUser({ userId: user.id, email: user.email, limit: 12, }), Promise.resolve(getCheckoutEnabledPlans()), Promise.resolve(isWechatPayEnabled()), ]); const currentPlan = resolvePlanByMonthlyImageLimit(user.monthlyImageLimit); const checkoutAvailableCount = Object.values(checkoutEnabledPlans).filter(Boolean).length; return (

Billing Center

订阅与账单管理

查看当前套餐、权益额度,并可进入 Stripe 门户管理续费、取消和发票信息。

当前订阅

Active Plan

{currentPlan.name}

月度出图额度:{formatLimit(currentPlan.monthlyImageLimit)}

更换套餐 {currentPlan.id !== "starter" ? : null}

订阅状态

当前账号:{session.user.name || session.user.email}

已配置可下单套餐:{checkoutAvailableCount}/3

微信支付通道:{wechatEnabled ? "已启用" : "未启用"}

订阅权益按 Webhook 自动写回账户额度(`monthlyImageLimit`)。

最近订阅事件

{billingEvents.length > 0 ? (
{billingEvents.map((event) => (
{formatEventType(event.type)} {event.status === "processed" ? "已处理" : "失败"}

{formatChannel(event.channel)} ·{" "} {formatDateTime(event.occurredAt)} {event.planId ? ` · ${event.planId}` : ""}

{event.detail ?

{event.detail}

: null}
))}
) : (

暂无订阅事件记录。

)}

说明

  • 如已完成支付但权益未刷新,可稍后重试或联系管理员检查 Webhook 配置。
  • 取消订阅后,到期会自动回退为 Starter 套餐额度。
  • 账单、付款方式、发票下载统一在 Stripe Portal 中管理。
); }