itsliamoco
itsliamoco

Reputation: 1048

Laravel Cashier Create Subscription

It's very easy to create a stripe customer, associate a card with that customer and charge them at anytime after. However, using Laravel cashier it's not so easy to subscribe a user to a plan at any time after.

Auth::user()->subscription(Input::get($new_plan_id))->create($card_token, array(
    'email' => Auth::user()->email
));

The card tokens are generated when the user adds a card in my settings. I have tried storing the token at this point and using it when a user wants to update their subscription but I get the error:

You cannot use a Stripe token more than once

Upvotes: 3

Views: 3668

Answers (3)

Yuvraj Chauhan
Yuvraj Chauhan

Reputation: 239

Below code is work for me in Laravel 5.1 :

For Create Subscription :

$creditcardToken = Input::get( 'stripeToken' );
$user->subscription(Input::get( 'subscription' ))->create($creditcardToken,['email' => $user->email,'description' => 'Customer from DevLaravel']);

For Update Subscription you have to follow some steps:

1. // Get Subscription
$subscription = $user->subscriptions()->where('stripe_id', 'sub_5vp6DX7N6yVJqY')->first();
2. // Update the subscription plan
$subscription->setStripePlan('new-plan-name');
3. // Apply the coupon
$subscription->applyCoupon($coupon);

Upvotes: 1

vcardillo
vcardillo

Reputation: 1696

You may simply call it normally, without the cc token:

$user->newSubscription('main', $plan)->create();

Upvotes: 1

Andreyco
Andreyco

Reputation: 22862

You can pass customer as 3rd parameter to create method.

$customer = $user->stripe_id ?
    \Laravel\Cashier\Customer::retrieve($user->stripe_id) : 
    null;

$user->subscription($plan)->create($token, $meta, $customer);

Upvotes: 0

Related Questions