
本文详解如何通过 stripe checkout 的 setup 模式预先收集客户支付方式并设置为默认付款方式,再结合 subscription schedule 实现“首月付费→次月续费→一年后切换价格”的灵活订阅流程,避免手动建卡、重复跳转与无限加载问题。
本文详解如何通过 stripe checkout 的 setup 模式预先收集客户支付方式并设置为默认付款方式,再结合 subscription schedule 实现“首月付费→次月续费→一年后切换价格”的灵活订阅流程,避免手动建卡、重复跳转与无限加载问题。
在 Stripe 生态中,Subscription Schedule 本身不支持直接扣款,它仅定义未来订阅阶段(phases)的时间与价格逻辑,实际扣款依赖于关联 Customer 的有效支付方式及后续生成的 Invoice 或 Subscription。因此,你遇到的“页面无限加载、无扣款”问题,根源在于:当前代码中仅创建了 Customer(无支付方式)和 Subscription Schedule(无激活的订阅实例),Stripe 无法触发任何账单周期。
要实现你所需的「首期即刻扣款 + 后续按 Schedule 自动切换」,必须分两步走:先安全获取并绑定支付方式,再创建带生效逻辑的 Schedule。推荐采用 Stripe 官方推荐的 Checkout Setup Mode(而非手动集成 Elements 或自建表单),既符合 PCI 合规,又保持用户体验一致性。
✅ 正确流程(四步闭环)
-
发起 Setup Checkout Session
使用mode: 'setup'创建会话,引导用户授权保存卡片(无需立即扣款,但获得 PaymentMethod ID):\Stripe\Stripe::setApiKey('sk_test_...'); $session = \Stripe\Checkout\Session::create([ 'customer' => null, // 允许新客户自动创建 'mode' => 'setup', 'success_url' => 'https://yoursite.com/success?session_id={CHECKOUT_SESSION_ID}', 'cancel_url' => 'https://yoursite.com/cancel', 'payment_method_types' => ['card'], 'setup_intent_data' => [ 'metadata' => ['origin' => 'subscription_schedule_setup'] ], ]); echo json_encode(['id' => $session->id]); -
服务端监听
checkout.session.completed事件
在 Webhook 中捕获成功会话,提取 Customer ID 和 SetupIntent 关联的 PaymentMethod:// Webhook handler (e.g., /wp-json/api/webhook) $payload = @file_get_contents('php://input'); $sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE']; $event = \Stripe\Webhook::constructEvent($payload, $sig_header, 'your_webhook_secret'); if ($event->type === 'checkout.session.completed') { $session = $event->data->object; $customerId = $session->customer; // Retrieve the SetupIntent to get the attached PaymentMethod $setupIntent = \Stripe\SetupIntent::retrieve($session->setup_intent); $paymentMethodId = $setupIntent->payment_method; // ⚠️ 关键:将该 PaymentMethod 设为 Customer 默认付款方式 \Stripe\Customer::update($customerId, [ 'invoice_settings' => [ 'default_payment_method' => $paymentMethodId ] ]); } -
创建 Subscription Schedule(使用已绑定支付方式的 Customer)
此时 Customer 已具备可扣款的default_payment_method,Schedule 可立即触发首期账单:$schedule = \Stripe\SubscriptionSchedule::create([ 'customer' => $customerId, 'start_date' => 'now', // 立即开始首个 phase 'end_behavior' => 'release', 'phases' => [ // Phase 1: 首月(立即计费) [ 'items' => [['price' => 'price_1LRF5CIne7DRPLRnwuLVE2pu', 'quantity' => 1]], 'iterations' => 1, ], // Phase 2: 次月(自动进入下一 phase) [ 'items' => [['price' => 'price_1LRF5cIne7DRPLRngevvIZiw', 'quantity' => 1]], 'iterations' => 1, ], // Phase 3: 一年后切换为年度价(无需 end_date,靠 iterations 控制) [ 'items' => [['price' => 'price_1LPujQIne7DRPLRnj3EOweJN', 'quantity' => 1]], ], ], ]); -
前端重定向与错误处理
将 Checkout Session URL 返回给前端跳转,并确保 Webhook 端有幂等性校验(使用idempotency_key或事件id去重):// 前端调用示例 fetch('/wp-json/api/checkout-setup', { method: 'POST' }) .then(res => res.json()) .then(data => { window.location.href = `https://checkout.stripe.com/c/pay/${data.id}`; });
⚠️ 注意事项与最佳实践
-
不要手动创建 Customer 再调用 Schedule:你原代码中
$customer = \Stripe\Customer::create(...)生成的是空客户,无支付方式,Schedule 无法触发首次扣款。 -
避免混合模式:不要在同一个流程中混用
payment模式(扣款)和setup模式(存卡)。本场景必须用setup获取 PaymentMethod 后显式绑定。 -
Webhook 必须启用:
checkout.session.completed事件是获取 Customer ID 和 PaymentMethod 的唯一可靠途径;前端重定向后无法直接获取这些敏感数据。 -
价格对象需兼容:所有
price_*必须是 recurring 类型(recurring.interval为month或year),且 currency 一致。 -
测试环境验证:使用 Stripe 测试卡(如
4242 4242 4242 4242)配合test-clock模拟时间推进,验证 phase 切换是否准确。
通过以上结构化实现,你既能复用 Stripe Checkout 的高转化率界面,又能精准控制多阶段定价逻辑——真正实现“一次授权、全程自动、合规无忧”。










