Get next billing date from Laravel Cashier
Asked Answered
H

3

19

I have subscribed a user to a subscription plan through Laravel's Cashier package. I now want to display the date the user will next be billed, however this doesn't appear to be an available through the Billable trait.

How do I get the next billing date? Thanks!

Hyla answered 10/1, 2017 at 19:11 Comment(0)
H
20

The solution is to use the asStripeCustomer method:

// 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();

Note that I've only tested this with a user who has a single subscription - data[0].

You may need to alter this code for multiple subscriptions or if the user has cancelled and started another subscription.

Hyla answered 11/1, 2017 at 9:37 Comment(2)
This solution worked for me. @John1984, it's OK to accept your own answer.Lunik
may I ask you to take a look at a l Stripe rekated question here : #62239051 ?Tetrasyllable
B
15

Building on previous answers, here's what's working for me:

private function getSubscriptionRenewDate($plan)
{
    $sub = Auth::user()->subscription($plan)->asStripeSubscription();
    return Carbon::createFromTimeStamp($sub->current_period_end)->format('F jS, Y');
}
Brewer answered 26/1, 2018 at 21:17 Comment(0)
T
11

Subscriptions also have a ->asStripeSubscription() method that gives you access to the values just for that subscription. So you could do:

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

// Cast to Carbon instance and return
return \Carbon\Carbon::createFromTimeStamp($timestamp)->toFormattedDateString();
Television answered 24/12, 2017 at 18:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.