Yasmin French
Yasmin French

Reputation: 1284

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 [email protected].

Upvotes: 0

Views: 1168

Answers (1)

Yasmin French
Yasmin French

Reputation: 1284

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 [email protected].

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.

Upvotes: 2

Related Questions