Laravel 收银员交换立即生效

发布于 2025-01-16 21:07:14 字数 721 浏览 1 评论 0 原文

根据文档 swap() 方法,在下一个计费日期(即当前计划结束后)将当前订阅与新订阅交换。并且swapAndInvoice()会立即生效,无需等待当前计划结束。

但是 swap() 方法没有按照描述的那样工作。它立即生效。

$subscription_result = $user->subscription('primary')->swap('new_price_id');

这就是我用来交换订阅的东西。

示例

Plan A :- 3 days ($3)
Plan B :- 7 days ($7)

用户于3月25日订阅了计划A,下一个计费周期为3月28日。 用户于 3 月 25 日更改订阅B 计划,应于 28 日生效(使用 swap() 方法)。但它立即生效,并显示下一个计费周期是4 月 1 日

我尝试使用 swapAndInvoice() 而不是 swap(),但这两种方法都给出相同的输出,没有区别。

As per the documentation swap() method swap the current subscription with the new one on the next billing date, means after end of current plan. And swapAndInvoice() will take immediate effect without waiting for the current plan to end.

But the swap() method is not working as described. It is taking immediate effect.

$subscription_result = $user->subscription('primary')->swap('new_price_id');

This is what I am using to swap subscription.

example

,

Plan A :- 3 days ($3)
Plan B :- 7 days ($7)

User subscribed to Plan A on 25th March, next billing cycle is on 28th March.
User changed subscription to Plan B on 25th March, it should take effect on 28th (using swap() method). But it is taking effect immediately and showing next billing cycle is on 1st April.

I have tried using swapAndInvoice() instead of swap(), but both the methods are giving same output, no difference.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

漫雪独思 2025-01-23 21:07:14

此行为的原因与 swap() 方法具体无关,而是与 订阅升级和降级行为。当您将订阅更新为与先前价格不同的间隔的价格时(例如您的示例为 3 天间隔与 7 天间隔),则 billing_cycle_anchor 将立即重置,并生成新发票。无论价格更新的间隔是否保持不变,swapAndInvoice() 方法都会在订阅更新时截取新发票。而在两个价格的间隔相同的情况下,仅使用 swap() 会导致在下一个自然计费周期之前无法创建发票。

此处完成用例的 Stripe API 方法是利用 订阅计划。然而,Cashier 可能不直接支持此功能。

The reason for this behavior is not related to the swap() method specifically, but instead to how Subscription upgrades and downgrades behave. When you update a subscription to a price with a different interval than the previous price (like with your example a 3 day interval vs. 7 day interval), then the billing_cycle_anchor will be reset immediately and a new invoice will be generated. The swapAndInvoice() method will cut a new invoice at the time of the Subscription update regardless of whether the intervals of the price update stay the same. Whereas just using swap() in the case where the interval of the two prices are the same would result in an invoice not being created until the next natural billing cycle.

The Stripe API way to accomplish your use-case here would be to utilize Subscription Schedules. However, Cashier may not support this feature directly.

演多会厌 2025-01-23 21:07:14

我遇到了同样的问题,但可能找到了解决方法。我仍在亲自测试,因此请记住这一点。

简而言之,这不是出纳员的问题,当订阅的计费周期发生变化时,Stripe 的默认行为是立即收费。 (但是,如果是相同的计费周期,则将来会收费。不幸的是,这不是我们需要的。)
https://stripe.com/docs/billing/subscriptions/upgrade- downgrade#immediate- payment

然而,Stripe 的设计目的是处理每个用户的多个订阅! Cashier auth()->user()->subscription() 将始终提取他们最近的订阅,因此效果很好。

  1. 取消他们现有的订阅(Stripe 将附加一个“宽限期”,直到其预定的到期日期)。
  2. 获取默认付款方式
  3. 创建新订阅(试用期为上次订阅剩余的时间。您需要将此日期保存在某处)。
  4. 我添加了 'proration_behavior' => 'none' 作为 $subscriptionOptions (在代码中挖掘发现 create() 接受多个参数)。不确定这是否是造成差异的原因,但尚未在没有它的情况下进行测试。
  5. 您必须自己在某个地方管理实际的“到期日期”,这很好,因为无论如何您都需要在某个地方使用这个日期。只需确保您的日期更新准确即可。

代码示例。

auth()->user()->subscription('default')->cancel();

$payment_method = auth()->user()->defaultPaymentMethod()->id;

auth()->user()->newSubscription('default',  $plan->stripe_plan_id)
      ->trialDays(45)
      ->create($payment_method, [], ['proration_behavior' => 'none']);

return redirect()->route('billing')->withMessage('Subscription successfully changed');

注意:订阅表下的“试用结束时间”日期将不准确。我建议在用户表下使用“Trial_ends_at”,该表在用户注册时与付款无关。

I ran into this same issue, but might have found a workaround. I'm still testing it myself so bear that in mind.

In short, it's not Cashier that's the issue, Stripe's default behaviour when billing period changes in a subscription is to charge immediately. (If it was the same billing period however, it would charge in the future. Unfortunately not what we need here.)
https://stripe.com/docs/billing/subscriptions/upgrade-downgrade#immediate-payment

However, Stripe is designed to handle multiple subscriptions per user! Cashier auth()->user()->subscription() will always pull their most recent subscription so it works nicely.

  1. Cancel their existing subscription (stripe will attach a "grace period" until their scheduled expiry date).
  2. Get default payment method
  3. Create a new subscription (with a trial period of however long was left on the last subscription. You'll need this date saved somewhere).
  4. I added 'proration_behavior' => 'none' as $subscriptionOptions (dug in the code to find create() accepts more than one parameter). Unsure if this is what made the difference, but haven't tested yet without it.
  5. You will have to manage the actual "expiration date" yourself somewhere, which is fine because you will need this date somewhere for #3 anyways. Just ensure your date updating is accurate.

Code Ex.

auth()->user()->subscription('default')->cancel();

$payment_method = auth()->user()->defaultPaymentMethod()->id;

auth()->user()->newSubscription('default',  $plan->stripe_plan_id)
      ->trialDays(45)
      ->create($payment_method, [], ['proration_behavior' => 'none']);

return redirect()->route('billing')->withMessage('Subscription successfully changed');

Note: Your "trial_ends_at" date under subscriptions table will be inaccurate. I'm recommend using "trial_ends_at" under users table which isn't tied to payments when a user registers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文