从Laravel Cashier获取下一个结算日期

时间:2017-01-10 19:11:50

标签: laravel laravel-5.3 laravel-cashier

我已通过Laravel的Cashier软件包订阅了用户订阅计划。我现在想要显示用户下次开帐单的日期,但是这似乎不是通过Billable特征可用的日期。

如何获得下一个结算日期? 谢谢!

3 个答案:

答案 0 :(得分:10)

解决方案是使用asStripeCustomer方法:

// Retrieve the timestamp from Stripe
$timestamp = $user -> asStripeCustomer()["subscriptions"] -> data[0]["current_period_end"];

// Cast to Carbon instance and return
return \Carbon\Carbon::createFromTimeStamp($timestamp) -> toFormattedDateString();

请注意,我只对拥有一次订阅的用户data[0]进行了测试。

您可能需要为多个订阅更改此代码,或者如果用户已取消并开始其他订阅。

答案 1 :(得分:1)

订阅还有一个->asStripeSubscription()方法,可让您访问该订阅的值。所以你可以这样做:

// Retrieve the timestamp from Stripe
$timestamp = $subscription->current_period_end;

// Cast to Carbon instance and return
return \Carbon\Carbon::createFromTimeStamp($timestamp)->toFormattedDateString();

答案 2 :(得分:1)

以前的答案为基础,这里有什么对我有用:

private function getSubscriptionRenewDate($plan)
{
    $sub = Auth::user()->subscription($plan)->asStripeSubscription();
    return Carbon::createFromTimeStamp($sub->current_period_end)->format('F jS, Y');
}