无法在Laravel Cashier 4.2中设置订阅试用版,以便客户不会立即收取费用

I'm currently using Laravel Cashier (4.2) with Stripe. The current Stripe API version I'm using is 2014-06-17. I have tried in various ways to set the trial end date when creating a subscription. I have tried all kinds of variations of this code:

$company->trial_ends_at = Carbon::now()->addDays(14);
// Also tried this variation of the trial date
// $trialEndDateUnixTimestamp = strtotime(Carbon::parse($company->trial_ends_at));

$company->subscription('annual')
        ->create($creditCardToken, ['description'=>$company->company_name, 'email'=>Auth::user()->email], null);

$company->trial_ends_at = Carbon::now()->addDays(14);;
$company->save(); 

When I look at the request sent to Stripe, trial_end is always null. Any ideas are appreciated.

The official Laravel Cashier documentation states the following:

$user->subscription('monthly') ->withCoupon('code') ->create($creditCardToken);

The subscription method will automatically create the Stripe subscription, as well as update your database with Stripe customer ID and other relevant billing information. If your plan has a trial configured in Stripe, the trial end date will also automatically be set on the user record.

If your plan has a trial period that is not configured in Stripe, you must set the trial end date manually after subscribing:

$user->trial_ends_at = Carbon::now()->addDays(14);

$user->save();

The manual setting of the trial date is what seems to not be working. I tried the method outlined by the official docs, and the user is always charged right away because for some reason the trial period is not being applied.

The more I look at the documentation, the more I think it doesn't even support trial periods if it's not on the Stripe side. The key lines are: "If your plan has a trial period that is not configured in Stripe, you must set the trial end date manually after subscribing", but by that point, the subscription has already been created, and thus the charge has already been made....

By pure chance, I came across this issue posted in the Laravel Cashier repository.

The answerer of the question said this:

Not in the docs for some reason, but you can do:

$user->subscription($plan) ->trialFor($user->trial_ends_at) ->create($creditCardToken, ['email' => $user->email, 'description' => $user->name ]);

This solves my question as it creates the subscription but does not charge the user right away if they have a free trial. I can verify this by looking on the Stripe dashboard and seeing that the trial field has been applied to the subscription.

From what I can tell, You have to set the trial end time after you subscribe the customer.

Like so:

$trialEndDateUnixTimestamp = strtotime(Carbon::parse($company->trial_ends_at));

$customer->subscription('annual')
         ->create(NULL, $customer);

$customer->trial_ends_at = $trialEndDateUnixTimestamp;

$cusomer->save();

Laravel Cashier Docs:

To create a subscription, first retrieve an instance of your billable model, which typically will be an instance of App\User. Once you have retrieved the model instance, you may use the subscription method to manage the model's subscription:

$user = User::find(1);

$user->subscription('monthly')->create($creditCardToken);

The create method will automatically create the Stripe subscription, as well as update your database with Stripe customer ID and other relevant billing information. If your plan has a trial configured in Stripe, the trial end date will also automatically be set on the user record.

If you want to implement trial periods, but are managing the trials entirely within your application instead of defining them within Stripe, you must manually set the trial end date:

$user->trial_ends_at = Carbon::now()->addDays(14);

$user->save();

$company->trial_ends_at = date('Y-m-d', strtotime("+14 days"));