如何处理订阅续订与升级之间的 Stripe 失败付款?

How to handle Stripe failed payments between subscription renewal vs upgrade?

升级工作流程

一位客户有一个订阅(单人团队),并且能够添加其他团队成员(更新订阅的数量)。

如果客户目前只有一个计划,他们可以升级到团队计划。如果团队计划中的任何人需要额外的席位,他们只需更改他们的订阅数量。

我目前设置了 webhooks:customer.subscription.updatedcustomer.subscription.deletedinvoice.paidinvoice.payment_failed

目标

问题

目前,在控制面板中,我将订阅状态(在管理失败的付款中找到)设置为如果所有重试都失败则取消订阅。

这是否会取消所有已达到重试限制的发票的订阅,例如续订(发票对象的 billing_reason: "subscription_cycle")和升级(billing_reason: "subscription_update")?

如果是,我需要将其关闭,但是通过我的 webhook 处理这种情况的标准方法是什么?

我认为您的问题已经很接近解决了。 invoice.payment_failed 事件就是您要找的。所以这就是我的处理方式:

Dashboard Settings

两者都

  • 如果发票支付失败,取消订阅取消设置
  • 或者保留它,但设置为最大天数 (15) 作为故障保险,以防万一您的其他逻辑未能捕捉到您应该 取消订阅的情况

Webhook 处理

如果事件类型是 invoice.payment_failed,请检查 invoice.billing_reason 以确定它是响应续订还是更新。对于任一进程,您都可以在 invoice.subscription.

中访问订阅 ID
if invoice.billing_reason == "subscription_cycle":
    # Renewal Payment Failure - Cancel the subscription for this customer
    stripe.Subscription.delete(invoice.subscription)
    # Take other measures (like emailing the customer)
    ...

elif invoice.billing_reason == "subscription_update":
    # Upgrade Payment Failure - downgrade subscription
    ...  

为 Python 道歉,这是我最熟悉的语言