I am using Cashier in Laravel 9 and trying to redirect the user to the stripe gateway with 14 days trial, but on the gateway, it shows 13 days. What can be the problem?
public function paymentForm(Plan $plan)
{
$user = Auth::user();
$subscription = $user
->newSubscription($plan->name, $plan->price_id);
if($plan->trial) {
$subscription->trialDays($plan->trial);
}
return $subscription->checkout([
'success_url' => route('success_page').'?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => route('failed_page'),
]);
}
I tested this code by changing trial days, and here are my responses for the trial:
$subscription->trialDays(0); => returns 1
$subscription->trialDays(1); => returns 1
$subscription->trialDays(2); => returns 1
$subscription->trialDays(20); => returns 19
The issue is because of the difference between Stripe and Laravel Cashier trial periods. Stripe uses seconds to count the trial period and Laravel Cashier uses days.
Use trialUntil()
if ($plan->trial) {
$trialEnd = now()->addDays($plan->trial);
$subscription->trialUntil($trialEnd);
}