订阅续订时会引发 customer.subscription.updated 事件吗? [英] Is the customer.subscription.updated event raised when a subscription is renewed?

查看:31
本文介绍了订阅续订时会引发 customer.subscription.updated 事件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑为什么 Stripe 的文档建议 customer.subscription.updated 事件在我认为应该触发时不会触发:

  • Stripe subscription 对象具有属性 current_period_startcurrent_period_end,只要客户成功支付订阅的发票(https://stripe.com/docs/api#subscriptions )
  • customer.subscription.updated 事件的文档指出它...

    <块引用>

    每当订阅更改时发生.示例包括从一个计划切换到另一个计划,或将状态从试用切换到活动状态.

    ...这意味着如果 current_period_startcurrent_period_end 值改变,事件将被引发,但它不会确认是否改变在这种情况下.

    但是,此第三方网页指出,在成功续订时不会引发此问题 ( https://www.masteringmodernpayments.com/stripe-webhook-event-cheatsheet#8 ).

  • 但是发起这个活动才有意义...

  • 当然,如果应用程序需要监视单个事件类型(即customer.subscription.updated),那么它将大大简化程序代码,而无需同时监视invoice.payment_succeededinvoice.createdcharge.succeeded.
  • 但是订阅生命周期的文档 ( https://stripe.com/docs/subscriptions/lifecycle ) 完全没有提及 customer.subscription.updated 事件.

这种适当的事件没有在应该发生的时候引发,这似乎很奇怪.该文档也确实没有说明 current_period_endcurrent_period_start 值何时更新,这限制了它们的实用性.

因此,在我的应用程序中,在收到 invoice.payment_succeeded 事件后,我的程序代码如何确定客户的订阅期何时结束?

解决方案

我已经验证 customer.subscription.updated 在计费周期结束时被调用.

为此,我拦截了在该期间结束时发生的所有 webhook(仅供参考:我使用了一个 AWS Lambda 函数,该函数从 AWS API 网关接收事件,然后将事件放在 SQS 队列中:))

我同意customer.subscription.updated的Stripe 文档代码> 事件可能会更清楚,并且可以通过说...来涵盖这个用例.

<块引用>

每当订阅更改时发生.例子包括当计费周期结束并且新的计费周期开始时,当从一个计划切换到另一个计划,或从试用状态切换到活跃.

(仅供参考:我会完全忽略备忘单网站.他们只对 customer.subscription.updated 进行了短暂的引用 - 在第 8 步中,他们描述了(糟糕的)创建客户"的用例没有试用的计划"不会创建 customer.subscription.updated 事件,因为此事件仅在订阅更新时发生,而不是在创建时发生.他们引用 的地方customer.subscription.updated 在第 12 步发票收费尝试失败"的上下文中)

在为有关订阅生命周期的 Stripes 文档辩护时,它确实说下图显示了发生的最重要事件的生命周期",我会说客户.subscription.updated 在创建发票和付款方面不是重要事件.

有关 Stripe 如何处理句点结束的一些详细信息:

  • 在我的测试中,Stripe 在订阅的 current_period_end 属性中的时间戳之后大约 2 分钟引发了 customer.subscription.updated 事件.该事件有两个与之关联的数据对象.第一个是带有新值的订阅.第二个是带有前两个 current_period_startcurrent_period_end 值的 previousAttributes 对象.

  • 同时生成了两个事件:customer.subscription.updatedinvoice.created(这是刚刚过去期间的发票).

  • 在发票创建大约一个小时后,同时生成了三个事件:invoice.payment_succeededcharge.succeededinvoice.updated.

您如何处理结算周期的展期与发票的付款状态完全取决于您,这在很大程度上取决于您的应用程序类型.这就是 Stripe API 的美妙之处(这是一件很美妙的事情).

就我而言,我将结算周期的转期与发票的付款状态分开处理.我的应用程序关心计费周期何时结束并基于此更新使用情况,但任何付款失败都会生成警报离线处理.

总而言之,您可以使用customer.subscription.updated来了解计费周期何时发生变化.

I'm confused about why Stripe's documentation suggests the customer.subscription.updated event is not fired when I believe it should:

  • A Stripe subscription object has the properties current_period_start and current_period_end which would be updated whenever the customer successfully pays a subscription's invoices ( https://stripe.com/docs/api#subscriptions )
  • The documentation for the customer.subscription.updated event states that it...

    Occurs whenever a subscription changes. Examples would include switching from one plan to another, or switching status from trial to active.

    ...which would imply that the event would be raised if the current_period_start and current_period_end values change, but it doesn't affirm if it does or it does not in this case.

    However this third-party webpage states that it is not raised when a successful renewal is performed ( https://www.masteringmodernpayments.com/stripe-webhook-event-cheatsheet#8 ).

  • But raising the event just makes sense...

  • And certainly if applications only needed to monitor a single event type (i.e. customer.subscription.updated) then it would greatly simplify program code without needing to also monitor invoice.payment_succeeded, invoice.created and charge.succeeded.
  • However the documentation for the Subscription Lifecycle ( https://stripe.com/docs/subscriptions/lifecycle ) makes absolutely no mention of the customer.subscription.updated event at all.

It just seems odd that such an appropriate event is not raised when it should be. The documentation also really doesn't say when the current_period_end and current_period_start values are updated either, which limits their utility.

So in my application, after receiving an invoice.payment_succeeded event, how can my program code determine when the customer's subscription period will end next?

解决方案

I've verified that customer.subscription.updated is called when the billing period ends.

To do this I intercepted all webhooks that occur at the end of the period (FYI: I used an AWS Lambda function that receives the events from an AWS API Gateway, then puts the events on an SQS queue :))

I agree that the Stripe documentation for the customer.subscription.updated event could be clearer and could cover this use case by saying....

Occurs whenever a subscription changes. Examples would include when the billing period ends and a new billing period begins, when switching from one plan to another, or switching status from trial to active.

(FYI: I would ignore the cheatsheet website altogether. They make only fleeting reference to customer.subscription.updated - In step 8 they are describing (poorly) the use case of "Create a customer with a plan without a trial" which wouldn't create a customer.subscription.updated event because this event only occurs when the subscription is updated, not when it is created. Where they do reference customer.subscription.updated is in context of Step 12 "Invoice charge attempt fails")

In defense of Stripes documentation regarding the lifecycle of a subscription, it does say "the following image shows the lifecycle of the most important events that occur" and I'd say customer.subscription.updated is not an important event in context of creating invoices and making payments.

Some details about how Stripe handles the end of a period:

  • In my test, Stripe raised the customer.subscription.updated event approximately 2 minutes after the timestamp in the current_period_end property on the subscription. The event has two data objects associated with it. The first is the subscription with the new values on it. The second is a previousAttributes object with the two previous current_period_start and current_period_end values.

  • Two events were generated simultaneously: customer.subscription.updated and invoice.created (this was the invoice for the period that had just elapsed).

  • Around an hour after the invoice was created, three events were generated simultaneously: invoice.payment_succeeded, charge.succeeded and invoice.updated.

How you treat the rollover of a billing period vs. the payment status of an invoice is really up to you, and very much depends on your application type. That's the beauty of the Stripe API (and it is a thing of beauty).

In my case, I treat the rollover of a billing period separately to the payment status of an invoice. My application cares about when the billing period rolls over and makes updates to usage based on this, but any payment failures generate alerts & are handled offline.

In summary, you can use customer.subscription.updated to know when the billing period has changed.

这篇关于订阅续订时会引发 customer.subscription.updated 事件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆