Shealan
Shealan

Reputation: 1346

Starting a no card up front subscription using Laravel Cashier & Stripe

I'm confused about how Cashier/Stripe subscriptions work with no card up front trials. I have set the trial period on Stripe and created the plans. I have set the no card up front model trait to false.

$user = User::find(1);

// This doesn't seem to do anything. No sub created in Stripe.

$user->subscription('yearly_uk');

// This throws an error as I am not passing through a card token.

$user->subscription('yearly_uk')->create();

// This throws an nasty api error but strangely does create a customer in Stripe.

$user->subscription('yearly_uk')->create(null);

Any ideas?

Upvotes: 2

Views: 3618

Answers (2)

Bede
Bede

Reputation: 302

By your comment, it looks like you've already answered this, but for future readers, I encountered a similar problem where the user can choose from a few plans, one of which is free.

To resolve, add this to your User Model:

protected $cardUpFront = false;
protected $trial_ends_at = null;

then you can subscribe the user using a null token:

$user->subscription('yearly_uk')->create(null);

I haven't checked, but I assume it won't throw the same kind of errors if / when you move to a paid plan - i.e. they'll be able to change, but I would guess Stripe will return an error as it'll bill the user with no credit card details.

Upvotes: 3

Hardik Gadhiya
Hardik Gadhiya

Reputation: 11

        \Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));

    $user_id = Auth::user()->id;
    $AuthUser = Auth::user();

    $stripe_plan = 'plan_CMFx07YJ96oL6h'; //Free stripe plan

    $user = User::find($user_id);
    $user->newSubscription('main',$stripe_plan)->create(null);

    if($AuthUser->subscribed('main')){
        return redirect('subscription')->with('status','You are subscribed successfully.');
    }

Upvotes: 1

Related Questions