2
votes

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

3
With that, you're creating a new Customer. You should change the subscription of the existing customer.Simon Fredsted

3 Answers

1
votes

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

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

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);
0
votes

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