17

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!

John1984
  • 917
  • 2
  • 13
  • 23

3 Answers3

19

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.

tptcat
  • 3,894
  • 2
  • 32
  • 51
John1984
  • 917
  • 2
  • 13
  • 23
12

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');
}
split19
  • 597
  • 7
  • 18
10

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();
hotmeteor
  • 495
  • 6
  • 7