You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Stripe PHP Webhook疑问:订阅取消事件捕获与数据获取

关于Stripe Webhook订阅相关疑问的解答

问题1:invoice.payment_failed能否捕获订阅取消?

invoice.payment_failed事件无法捕获客户主动设置“订阅周期结束时取消”的场景:

  • 当客户登录Stripe客户门户选择到期取消订阅,Stripe仅会将该订阅的cancel_at_period_end字段设为true,此时不会触发invoice.payment_failed,只有当订阅真正到期并被删除时,才会触发customer.subscription.deleted事件。
  • 若订阅是因为多次付款失败被系统自动取消,会同时触发invoice.payment_failed和customer.subscription.deleted,但前者仅标记付款失败状态,后者才是订阅取消的核心触发事件。

综上,你需要额外监听customer.subscription.deleted事件来覆盖所有订阅取消场景,还可通过该事件对象的cancel_at_period_end字段区分是“立即取消”还是“到期取消”。

问题2:通过Session对象获取订阅/客户详情是否合理?

这种做法不合理,因为invoice.paid事件的data->object是Invoice对象,而非Checkout Session对象,用Invoice的ID去查询Session必然会出错。

正确的处理方式是直接从Invoice对象本身获取关联数据:

// 从事件中获取Invoice对象
$invoice = $event->data->object;
// 展开关联的customer和subscription
$invoice->expand(['customer', 'subscription']);

// 直接提取所需信息
$customer_email = $invoice->customer->email;
$subscription_id = $invoice->subscription->id;
$session_id = $invoice->checkout_session; // 若订阅由Checkout创建,Invoice会关联对应的Session ID

如果确实需要通过Session获取数据,也应使用Invoice对象中的checkout_session字段(而非Invoice自身ID)去查询,但这会多一次API调用,效率远不如直接从Invoice展开关联字段。


内容的提问来源于stack exchange,提问作者Matt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 23:35:01