3

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....

Marcel Gruber
  • 6,668
  • 6
  • 34
  • 60

3 Answers3

3

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.

Marcel Gruber
  • 6,668
  • 6
  • 34
  • 60
2

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

Community
  • 1
  • 1
meun5
  • 322
  • 4
  • 15
  • Are you sure it accepts unix timestamps? In the example it is in `YYYY-MM-DD hh:mm:ss` – meun5 Dec 07 '15 at 01:18
  • Positive. I have also tried using the exact `Carbon::now()->addDays(14)` as the trial end date. – Marcel Gruber Dec 07 '15 at 01:22
  • I think there might be some confusion as you're looking at the 5.1 documentation. Unfortunately I am stuck working with 4.2, but my answer has been edited to show more code – Marcel Gruber Dec 07 '15 at 05:16
  • My DB record for the billable entity (company) does have that field set correctly, but I don't think it's having any effect whatsoever on when the user actually gets charged when the subscription is created. – Marcel Gruber Dec 07 '15 at 05:54
1
$company->trial_ends_at = date('Y-m-d', strtotime("+14 days"));
Yogus
  • 2,307
  • 5
  • 20
  • 38