Get next billing date from Laravel Cashier
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');
}
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();
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.