ShipNow LogoShipNow
支付

Stripe

学习如何在 ShipNow 应用中配置价格方案和集成 Stripe 支付。

概述

ShipNow 的 Stripe 支付集成功能包括:

  • 💰 配置订阅或一次性支付
  • 🔄 自动处理支付流程
  • 🛠️ 开发者友好,支持沙箱和 webhook

配置 Stripe

创建 Stripe 账户

  1. 注册 Stripe 账户
  2. 填写信息以激活账户
  3. 在激活前,你可以使用沙箱环境或测试模式

获取 API 密钥

  1. 前往 Stripe API 密钥页面
  2. 创建并复制 "Publishable key" 和 "Secret key"

获取 Stripe API 密钥

添加环境变量

将 Stripe 密钥添加到 .env.local

.env.local
STRIPE_PUBLIC_KEY="pk_test_..."
STRIPE_SECRET_KEY="sk_test_..."

配置 Stripe Webhooks

创建 Webhook 端点

  1. 前往 Stripe Webhooks
  2. 点击 "Add destination"

创建 Stripe webhook

选择事件

搜索并选择以下事件:

  • invoice.paid
  • checkout.session.completed
  • customer.subscription.deleted

创建 Stripe webhook

选择目标类型

使用 "Webhook endpoint" 作为目标类型,点击 "Continue"

创建 Stripe webhook

添加 Webhook URL

  1. 在 "Endpoint URL" 字段中添加你的 webhook URL
    https://your-domain.com/api/stripe/webhook
  2. 点击 "Create destination"

创建 Stripe webhook

获取 Webhook 密钥

  1. 创建 Webhook 后,点击 "Reveal" 并复制 "Signing secret"
  2. 添加到 .env.local
    STRIPE_WEBHOOK_SECRET="whsec_..."

获取 Stripe webhook 密钥

支付流程

以下是 ShipNow 中支付流程的工作方式:

用户点击价格表上的 "立即购买" 按钮

系统创建 Stripe 结账会话

用户被重定向到 Stripe 结账页面

支付后,用户被重定向回成功页面

Webhook 接收支付确认结果

本地开发和测试

设置本地开发

在本地开发时,使用 Stripe 的沙箱模式:

前往 Stripe 沙箱,进入你要使用的沙箱

API 密钥 页面获取测试 API 密钥

Stripe 工作台 设置本地 webhooks

使用以下命令将 webhooks 转发到本地服务器:

stripe listen --forward-to http://localhost:3000/api/stripe/webhook

测试支付卡

使用这些卡测试不同场景:

卡号场景
4242 4242 4242 4242支付成功
4000 0000 0000 0002支付失败

对于所有测试卡:

  • 有效期:任何未来日期,例如 12/34
  • CVC:任意 3 位数字,例如 123
  • 邮编:如果需要,任意 5 位数字,例如 12345

测试清单

✅ 创建测试订阅

  • 使用测试卡 4242 4242 4242 4242
  • 验证成功重定向
  • 检查 webhook 接收

✅ 测试支付失败

  • 使用失败测试卡 4000 0000 0000 0002
  • 验证错误消息显示

✅ 监控 webhook 事件

  • 检查 Stripe CLI 输出
  • 验证应用日志中的事件

调试提示

如果遇到问题:

  1. 检查环境变量是否正确设置
  2. 确保 webhook 转发正在运行
  3. 查看 Stripe 仪表板中的错误

专业提示

  • 不要使用正式模式测试支付流程,请使用沙箱或测试模式

需要帮助?

  • 查看 Stripe 文档
  • 加入我们的 Discord 社区
  • 在 GitHub 上提交问题
ShipNow Logo获取 ShipNow