【问题标题】:Stripe cancel subscription at specific dateStripe 在特定日期取消订阅
【发布时间】:2018-09-01 00:44:21
【问题描述】:

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

我知道有一个选项可以立即取消订阅:

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

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

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

使用此选项,将安排取消。

但是如何在特定日期取消订阅?在我的系统中,用户可以要求取消,但他必须等待一个特定的日期(比如作为订阅者至少停留 X 个月)

你知道我该怎么做吗?谢谢!

【问题讨论】:

  • 您需要自己编写该逻辑。 Stripe 没有内置的结构。

标签: stripe-payments


【解决方案1】:

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

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

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

最好, 蒂莫

【讨论】:

    【解决方案2】:

    啊,是的。好问题。这是一个检查/执行示例:

    $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();
    }
    

    这是一个在订阅时设置取消的示例:

    $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
    ));
    

    【讨论】:

      【解决方案3】:

      Stripe 刚刚将它添加到他们的 API 中,我偶然发现了它。他们有一个名为“cancel_at”的字段,可以设置为将来的日期。他们的文档中没有列出此属性,因为它太新了。您可以在此处查看响应对象中的值:

      https://stripe.com/docs/api/subscriptions/create?lang=php

      我已经使用 .NET 对此进行了测试,并且可以确认它将订阅设置为在​​您提供的值时到期。

      【讨论】:

      • They have a field called "cancel_at" 。看来他们没有。
      • @SephReed,刚刚通过链接验证该字段仍然存在于响应中,如前所述。我们仍在生产中使用该字段作为请求,没有问题。
      • 我找到了。它根本不可搜索(通过 google 或他们的 api)。此外,它们使典型搜索过载,因此您无法搜索它。没有它的文档。但是,可以在其中一个示例中看到。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-16
      • 2019-02-28
      • 2017-08-20
      • 2014-09-07
      • 1970-01-01
      相关资源
      最近更新 更多