SuperAPI AI 工具集

convex-billing

通用与其它

查看项目

更新于 10-15 21:51

技能介绍

用途:在 Convex 应用中接入 Stripe 计费与支付,使用 @convex-dev/stripe 组件完成结账、Webhook、订阅状态存储与服务端订阅校验。

主要能力:提供结账 action;通过组件注册并自动验签的 httpAction Webhook;订阅状态存入组件表中;通过查询在服务端判断订阅状态。

使用方式:先安装组件 npm install @convex-dev/stripe。在 convex/convex.config.ts 中从 '@convex-dev/stripe/convex.config.js' 导入并 app.use(stripe)。在 Convex 环境变量中配置 STRIPE_SECRET_KEY(sk_test_… / sk_live_…)与 STRIPE_WEBHOOK_SECRET(whsec_…)。在 convex/http.ts 中调用 registerRoutes(http, components.stripe, { webhookPath: '/stripe/webhook' }) 注册 Webhook,组件自动处理签名验证。在 convex/billing.ts 中创建订阅结账 action(getOrCreateCustomer、createCheckoutSession,mode 为 subscription)与 isSubscribed 查询,查询读取组件表 listSubscriptionsByUserId,当 status 为 active 或 trialing 时返回 true。运行 npx convex dev --once 安装组件并推送函数。在 Stripe 后台添加 Webhook 端点 https://<deployment>.convex.site/stripe/webhook,订阅 checkout.session.completed、customer.subscription.、invoice.、payment_intent.* 等事件,并将签名密钥填入 STRIPE_WEBHOOK_SECRET。

限制与规则:使用 @convex-dev/stripe(npm: @convex-dev/stripe@^0.1.4),由 registerRoutes 内部处理 Webhook 验签,不要手写 constructEvent Webhook。Stripe 密钥放在 Convex 环境变量中。订阅门槛必须基于服务端存储的状态(isSubscribed 查询读取组件表),不要依赖客户端声明。convex.config.ts 必须从 '@convex-dev/stripe/convex.config.js' 导入,.js 扩展名是 Convex 打包器要求。