0
votes

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].

  • 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.
1
Are you setting your Stripe secrets in config/services.php? I see you have manually set them in your controller but I think you need to follow these directions for your app to authenticate correctly - laravel.com/docs/5.7/billing#stripe-configurationDylan Pierce
Ive set them correctly, if they werent set i would get an error stating the API keys are not setYasmin French
When you look in the logs of the Stripe Dashboard, what do you see for an error. That may be more enlightening. Also, it seems that you are passing raw card details here, which is not something i would recommend, as that puts you in scope for some serious PCI compliance hasslesduck
@duck Thanks for the input, I managed to fix the error if you check my answer. Before however i contacted stripe support and they checked through my logs and confirmed nothing was wrong, which is weird considering the error is literally telling me to message stripe support. I usually only use this method of gaining the token cause its simpler for me testing wise, not a huge fan of JS but ill implement it before production, thanks!Yasmin French

1 Answers

2
votes

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.