Search code examples
phpstripe-paymentslaravel-cashierlaravel-5.7

Invalid request: Stripe 7.0 + Laravel 5.7


Description

I'm getting an error when attempting to create a stripe subscription using Laravel + the API.

Before you create the subscription you must get the token by requesting it, I have successfully created this token and I'm now using the "createSubscription()" method from the API (referenced in my code), but this is where the error is occurring.

Code

public function create()
    {
        $user = Auth::user();
        $plan = 'prod_**********';

    // Do some checks
    if ($user->subscribed('main')){
        return [
            'status' => 'failed',
            'message' => 'You are already subscribed!',
        ];
    }

    // Set the stripe Key
    Stripe::setApiKey(env('STRIPE_SECRET'));

    // Create the stripe token
    try {
        $stripeToken = Token::create([
            'card' => [
                'number' => str_replace(' ', '', Input::get('number')),
                'exp_month' => Input::get('exp_month'),
                'exp_year' => Input::get('exp_year'),
                'cvc' => Input::get('cvc')
            ]
        ]);
    }
    catch (\Stripe\Error\InvalidRequest $e)
    {
        return [
            'status' => 'failed',
            'message' => $e->getMessage(),
        ];
    }

    try{
        // This is the line thats failing
        $user->newSubscription('main', $plan)->create($stripeToken);
    } catch (\Stripe\Error\InvalidRequest $e) {
        dd($e->getMessage());
    }

    return [
        'status' => 'success',
        'message' => 'Subscription was successful!',
    ];
}

The Error

The error in full is:

Invalid request. Hint: check the encoding for your request parameters and URL (http://en.wikipedia.org/wiki/percent-encoding). For assistance, email support@stripe.com.

  • I've spoken to stripe support and they're saying they think the error is on my end, everything seems good on the stripe end.
  • I've checked the card detailed and customer details are correct (This is why the token being created).
  • I've searched this error but nothing seems to come up for this, there are other questions somewhat similar but no answers.
  • I've uninstalled / reinstalled laravel-cashier package to no success.

Solution

  • Whats strange is how i've managed to solve this problem. It would seem that passing the entire stripe token does not work and instead I only needed to pass the token ID.

    Simply changing

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

    to this

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

    Solved this error

    Invalid request. Hint: check the encoding for your request parameters and URL (http://en.wikipedia.org/wiki/percent-encoding). For assistance, email support@stripe.com.

    I'm sure that nowhere in either documentation is states that this is the solution? Or maybe I overlooked this somewhere... but this has solved it for me.