stripe
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseStripe — Next.js App Router
Stripe — Next.js App Router
Complete Stripe integration patterns for Next.js App Router with Server Actions and Route Handlers.
基于Next.js App Router结合Server Actions和Route Handlers的完整Stripe集成方案。
Setup (3 steps)
设置步骤(共3步)
Step 1: Install
步骤1:安装
bash
npm install stripe @stripe/stripe-js @stripe/react-stripe-jsbash
npm install stripe @stripe/stripe-js @stripe/react-stripe-jsStep 2: Environment variables
步骤2:环境变量
| Env Variable | Value | Exposed to Client |
|---|---|---|
| | NO |
| | Yes |
| | NO |
| | Yes |
| 环境变量 | 取值 | 是否暴露给客户端 |
|---|---|---|
| | 否 |
| | 是 |
| | 否 |
| | 是 |
Step 3: Initialize SDK
步骤3:初始化SDK
Copy → and → .
templates/stripe-server.tslib/stripe.tstemplates/stripe-client.tslib/stripe-client.tsLoad for details.
references/setup.md复制 → ,以及 → 。
templates/stripe-server.tslib/stripe.tstemplates/stripe-client.tslib/stripe-client.ts查看获取详细说明。
references/setup.mdQuick Patterns
快速集成方案
| Task | Approach | Reference |
|---|---|---|
| One-time payment | Checkout Session → redirect to Stripe | |
| Custom payment UI | Payment Intents + Stripe Elements | |
| Recurring billing | Subscriptions + Invoices | |
| Receive events | Webhook Route Handler | |
| Self-service billing | Customer Portal session | |
| Products & pricing | Products + Prices API | |
| Marketplace splits | Stripe Connect | |
| Auto tax calc | Stripe Tax | |
| Fraud prevention | Radar rules + 3D Secure | |
| Usage-based billing | Billing Meter + usage records | |
| Pricing page | Server-fetched prices + checkout button | |
| Stripe ↔ DB sync | Webhook → database update patterns | |
| Paywall / auth guard | Middleware + Server Component guards | |
| 任务 | 实现方式 | 参考文档 |
|---|---|---|
| 一次性支付 | 创建Checkout Session并跳转到Stripe托管页面 | |
| 自定义支付UI | Payment Intents + Stripe Elements组件 | |
| 定期订阅计费 | 订阅服务 + 发票管理 | |
| 接收事件 | Webhook路由处理器 | |
| 自助账单管理 | 创建客户门户会话 | |
| 产品与定价管理 | 产品+价格API | |
| 平台分账 | Stripe Connect | |
| 自动税务计算 | Stripe Tax | |
| 欺诈防护 | Radar规则 + 3D Secure验证 | |
| 基于使用量的计费 | 计费计量器 + 使用记录 | |
| 定价页面 | 服务端获取价格 + 结账按钮 | |
| Stripe与数据库同步 | Webhook触发数据库更新 | |
| 付费墙/权限校验 | 中间件 + 服务端组件校验 | |
Essential Webhook Events
核心Webhook事件
| Event | When | Action |
|---|---|---|
| Payment captured | Fulfill order |
| Subscription renewed | Extend access |
| Payment failed | Notify user, retry |
| Plan changed | Update entitlements |
| Cancelled | Revoke access |
| Custom flow completed | Confirm order |
| Dispute opened | Alert + evidence |
Load for handler implementation.
references/webhooks.md| 事件 | 触发时机 | 处理动作 |
|---|---|---|
| 支付成功到账 | 履行订单 |
| 订阅续费完成 | 延长用户访问权限 |
| 支付失败 | 通知用户并重试支付 |
| 订阅计划变更 | 更新用户权益 |
| 订阅取消 | 收回用户访问权限 |
| 自定义支付流程完成 | 确认订单 |
| 发起争议退款 | 发送告警并准备举证材料 |
查看获取处理器实现细节。
references/webhooks.mdTest Cards
测试卡号
| Number | Result |
|---|---|
| Success |
| Generic decline |
| Insufficient funds |
| Expired card |
| 3D Secure required |
| 3D Secure 2 required |
Load for full test cards + Stripe CLI workflow.
references/testing.md| 卡号 | 测试结果 |
|---|---|
| 支付成功 |
| 通用拒绝 |
| 余额不足 |
| 卡片过期 |
| 需要3D Secure验证 |
| 需要3D Secure 2验证 |
查看获取完整测试卡号列表及Stripe CLI使用流程。
references/testing.mdStripe CLI
Stripe CLI
bash
stripe login
stripe listen --forward-to localhost:3000/api/webhooks/stripe
stripe trigger checkout.session.completed
stripe trigger invoice.payment_failed
stripe logs tail --filter status:400bash
stripe login
stripe listen --forward-to localhost:3000/api/webhooks/stripe
stripe trigger checkout.session.completed
stripe trigger invoice.payment_failed
stripe logs tail --filter status:400Templates
模板文件
| Template | Copy To | Description |
|---|---|---|
| | Server-side Stripe instance |
| | Client-side Stripe.js (lazy) |
| | Webhook Route Handler |
| | Server Action: Checkout Session |
| | Server Actions: subscription CRUD |
| | Server Action: portal session |
| | Environment variables |
| 模板文件 | 复制到目标路径 | 描述 |
|---|---|---|
| | 服务端Stripe实例 |
| | 客户端Stripe.js(懒加载) |
| | Webhook路由处理器 |
| | Server Action:创建Checkout Session |
| | Server Actions:订阅增删改查 |
| | Server Action:创建客户门户会话 |
| | 环境变量配置模板 |
Error Handling
错误处理
Load for Stripe error types and retry patterns.
references/error-handling.md查看获取Stripe错误类型及重试机制说明。
references/error-handling.mdSecurity
安全注意事项
Load for PCI compliance, idempotency, and key management.
references/security.md查看获取PCI合规、幂等性及密钥管理相关内容。
references/security.mdAll References
全部参考文档
- — SDK init, singleton, TypeScript config
references/setup.md - — Checkout Sessions, hosted vs embedded
references/checkout.md - — Payment Intents, 3D Secure, capture
references/payment-intents.md - — Recurring billing, trials, proration
references/subscriptions.md - — Signature verification, event routing, idempotency
references/webhooks.md - — Self-service portal setup
references/customer-portal.md - — Products, Prices, coupons, promo codes
references/products-prices.md - — Marketplace, platform fees, payouts
references/connect.md - — Automatic tax calculation
references/tax.md - — Fraud detection, disputes
references/radar-fraud.md - — Usage-based billing
references/billing-meter.md - — Error types, retry logic
references/error-handling.md - — Test cards, CLI, test clocks
references/testing.md - — PCI, keys, idempotency, CSP
references/security.md - — Pricing page component pattern
references/pricing-page.md - — Stripe ↔ DB sync patterns
references/database-sync.md - — Auth middleware for paid routes
references/middleware-guard.md
- — SDK初始化、单例模式、TypeScript配置
references/setup.md - — Checkout Session、托管式 vs 嵌入式
references/checkout.md - — Payment Intents、3D Secure、资金到账
references/payment-intents.md - — 定期订阅计费、试用、计价调整
references/subscriptions.md - — 签名验证、事件路由、幂等性
references/webhooks.md - — 自助客户门户设置
references/customer-portal.md - — 产品、定价、优惠券、促销码
references/products-prices.md - — 平台分账、平台手续费、资金提现
references/connect.md - — 自动税务计算
references/tax.md - — 欺诈检测、争议处理
references/radar-fraud.md - — 基于使用量的计费
references/billing-meter.md - — 错误类型、重试逻辑
references/error-handling.md - — 测试卡号、CLI、测试时钟
references/testing.md - — PCI合规、密钥管理、幂等性、内容安全策略(CSP)
references/security.md - — 定价页面组件实现方案
references/pricing-page.md - — Stripe与数据库同步方案
references/database-sync.md - — 付费路由的权限校验中间件
references/middleware-guard.md