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.
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-configuration – Dylan Pierce