SuperAPI AI 工具集

adding-stripe

编程与开发

查看项目

更新于 10-15 21:05

技能介绍

用途:在用户要求添加支付、账单、订阅或 Stripe 集成时使用本技能,指导把 Stripe 支付接入 Web 应用。

主要能力与步骤:安装 SDK,命令为 npm install stripe @stripe/stripe-js;添加环境变量 STRIPE_SECRET_KEY、NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY、STRIPE_WEBHOOK_SECRET(示例值分别为 sk_test_...、pk_test_...、whsec_...);创建 Stripe 客户端模块 lib/stripe.ts,使用 new Stripe(process.env.STRIPE_SECRET_KEY!, { apiVersion: "2024-12-18.acacia" });创建 Checkout API 路由,通过 stripe.checkout.sessions.create 创建会话,支持 mode 为 subscription 或 payment,配置 payment_method_types、line_items、success_url、cancel_url、customer_email,并重定向至 session.url;创建 /api/webhooks/stripe 的 POST 处理器,用 stripe.webhooks.constructEvent 验证签名,处理 checkout.session.completed、customer.subscription.updated、customer.subscription.deleted、invoice.payment_failed 等事件,并更新数据库中的订阅状态;可选添加客户门户,重定向至 stripe.billingPortal.sessions.create,让用户管理订阅;添加定价页 UI,用方案卡片调用 Checkout API 路由。

限制与注意:始终验证 Webhook 签名,不要信任未验证的负载;本地测试用 Stripe CLI 命令 stripe listen --forward-to localhost:3000/api/webhooks/stripe;在用户数据库中保存 Stripe 客户 ID 以避免创建重复客户;用 stripe.prices.list 动态获取价格而非硬编码。

未提供的信息:具体收费、价格、支持平台范围与完整代码示例细节暂未提供。