如何在 Stripe 中正确设置订阅到期自动取消

如何在 Stripe 中正确设置订阅到期自动取消

在 stripe 中,不能通过 `cancel()` 方法设置 `cancel_at_period_end` 参数来实现订阅到期自动取消;必须改用 `update()` 方法更新订阅对象,将 `cancel_at_period_end` 设为 `true` 才能生效。

Stripe 的订阅取消机制分为两种行为:立即取消(immediate cancellation)和周期结束时取消(cancellation at period end)。关键在于:cancel() 接口仅支持立即终止订阅,并不接受 cancel_at_period_end 参数——若传入该参数,Stripe 会直接忽略或报错,无法达到预期效果。

✅ 正确做法是使用 subscriptions->update() 方法,仅需传入 cancel_at_period_end => true 即可标记订阅在当前计费周期结束后自动终止:

// ✅ 正确:设置订阅在当前周期结束时自动取消
$subscription = $stripe->subscriptions->update(
    $subscription_id,
    [
        'cancel_at_period_end' => true
    ]
);

// 可选:检查返回结果确认状态
if ($subscription->cancel_at_period_end) {
    echo "订阅已成功设置为周期结束时取消。";
}

⚠️ 注意事项:

Lumen5

Lumen5

一个在线视频创建平台,AI将博客文章转换成视频

下载

  • 设置后,subscription.cancel_at_period_end 将变为 true,且 subscription.status 仍保持为 active,直至周期自然结束;
  • 用户在周期结束前仍可正常使用服务,且不会产生额外账单(除非启用了 proration 或 invoice_now 等特殊选项);
  • 若后续需要提前立即取消,可再次调用 cancel()(此时不带任何取消时间参数),系统将立即终止并生成最终发票(如有);
  • 不要混用 invoice_now => true 与 cancel_at_period_end => true ——前者强制立即开票并可能触发扣款,与“到期取消”的语义冲突,通常不应同时启用。

? 小贴士:可通过 Stripe Dashboard 查看订阅详情页的 “Cancel subscription” 按钮下方提示(如 “Cancel at the end of the billing period”)验证设置是否生效;也可在代码中检查 $subscription->cancel_at_period_end 和 $subscription->current_period_end 字段进行逻辑判断。

总之,牢记一条原则:延迟取消 = 更新(update),非取消(cancel)。这是 Stripe API 设计的关键约定,也是避免业务逻辑出错的核心要点。

https://www.php.cn/faq/2006382.html

发表回复

Your email address will not be published. Required fields are marked *