条纹(Stripe)在特定日期取消订阅

4

我正在使用 PHP 中的 Stripe API,但我不知道如何在特定日期停止客户的订阅。

我知道有一种立即取消订阅的选项:

$subscription = \Stripe\Subscription::retrieve('sub_49ty4767H20z6a');
$subscription->cancel();

或者在当前计费周期结束时取消订阅(即客户已经支付的时间段):

$subscription->cancel(['at_period_end' => true]);

使用此选项,取消将被安排。
但是我如何在特定日期取消订阅呢?在我的系统中,用户可以要求取消订阅,但必须等待特定日期(类似于至少作为订阅者留下X个月)。
您知道我该如何做吗?谢谢!

你需要在自己的代码中编写那个逻辑。Stripe没有内置的构造来实现那个功能。 - korben
3个回答

4
啊,是的。好问题。这里有一个检查/执行的例子:
$today = time();
$customer = \Stripe\Customer::retrieve('Their Customer ID');
$signUpDate = $customer->subscriptions->data[0]->created;

// This will define the date where you wish to cancel them on.
// The example here is 30 days from which they initially subscribed.
$cancelOnDate = strtotime('+30 day', $signUpDate);

// If today is passed or equal to the date you defined above, cancel them.
if ($today >= $cancelOnDate) {
    $subscription = \Stripe\Subscription::retrieve('sub_49ty4767H20z6a');
    $subscription->cancel();
}

以下是一个在用户订阅时设置取消的示例:
```html

以下是一个在用户订阅时设置取消的示例:

```
$today = time();

// Cancel them 30 days from today
$cancelAt = strtotime('+30 day', $today);

$subscription = \Stripe\Subscription::create(array(
    "customer" => 'Their Customer ID',
    "plan" => 'xxxxxxxxx',
    "cancel_at" => $cancelAt
));

4
Stripe刚刚在其API中增加了一个新功能,名为“cancel_at”的字段,可以将其设置为未来的某个日期。由于这个功能非常新,他们的文档中并没有列出这个属性。您可以在此处的响应对象中看到该值:https://stripe.com/docs/api/subscriptions/create?lang=php
我已经使用.NET进行了测试,并确认它可以将订阅设置为您提供的值。

他们有一个名为“cancel_at”的字段。看起来他们没有。 - Seph Reed
@SephReed,刚刚通过链接验证了该字段在响应中仍然存在,正如注明的那样。我们仍然在生产中使用该字段作为请求,并且没有任何问题。 - Matty
我找到了它。它根本无法通过谷歌或其API进行搜索。此外,它还会过载典型的搜索,因此您不能进行双重搜索。没有关于它的文档。但是,它可以在其中一个示例中看到。 - Seph Reed

0

可能有点晚了,但我遇到了同样的问题并查看了您的帖子以获取帮助。我们想为用户提供季票,因此如果他们取消订阅,则付款将在季节结束时结束,例如每年的六月份。

经过大量研究,我在 Laravel docs 中找到了一个解决方案,这可能会有所帮助。对于我的解决方案,不是cancel_at而是cancelAt。没有太大的区别,但对我很有帮助 :)

$subscription = $user->subscription('default')->cancelAt(
        Carbon::create(2021,6,30)
    );

最好的, Timo


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接