'Laravel Cashier using Stripe - No API key provided. Set your API key when constructing the StripeClient instance
I seem to be getting the following error when I run $user->createSetupIntent()
No API key provided. Set your API key when constructing the StripeClient instance, or provide it on a per-request basis using the `api_key` key in the $opts argument.
I have added Billable to my User model
use Laravel\Cashier\Billable;
class User extends Authenticatable
{
use Billable;
use Notifiable;
use SoftDeletes;
use HasFactory;
And have added both my STRIPE_KEY and STRIPE_SECRET to my .env file.
I have also added to my services.php file
'stripe' => [
'model' => 'App\Models\User',
'key' => env('STRIPE_KEY'),
'secret' => env('STRIPE_SECRET'),
],
This was working earlier today but for some reason seems to be failing now. If I run ddd(\Stripe\Stripe::getApiKey()); the API key is returned correctly. However, if I go to the BaseStripeClient.php in the Vendor directory and add ddd($this->config) to the getApiKey method I get the following returned.
"api_key" => null
"client_id" => null
"stripe_account" => null
I have also tried following other solutions such as setting the api key in the register function.
public function register()
{
Stripe::setApiKey(config('services.stripe.secret'));
}
And tried in the boot function
public function boot()
{
Stripe::setApiKey(config('services.stripe.secret'));
}
Has anyone else come across this issue before? Why is my api_key not set in the stripe vendor files and how can I get it set?
Update
After looking in the Stripe BaseStripeClient.php file I found that commenting out the new error allowed me to successfully add a payment, which pulled through to Stripe.
private function apiKeyForRequest($opts)
{
$apiKey = $opts->apiKey ?: $this->getApiKey();
if (null === $apiKey) {
$msg = 'No API key provided. Set your API key when constructing the '
. 'StripeClient instance, or provide it on a per-request basis '
. 'using the `api_key` key in the $opts argument.';
// throw new \Stripe\Exception\AuthenticationException($msg);
}
return $apiKey;
}
I have also tried adding the api key via the Cashier class in the boot function
Cashier::stripe(['api_key' => 'sk_test_***']);
If I then open the Cashier file in the vendor directory and ddd the result
public static function stripe(array $options = [])
{
ddd(new StripeClient(array_merge([
'api_key' => $options['api_key'] ?? config('cashier.secret'),
'stripe_version' => static::STRIPE_VERSION,
'api_base' => static::$apiBaseUrl,
], $options)));
return new StripeClient(array_merge([
'api_key' => $options['api_key'] ?? config('cashier.secret'),
'stripe_version' => static::STRIPE_VERSION,
'api_base' => static::$apiBaseUrl,
], $options));
}
Then I get the correct api key value returned. However when I comment this out and simply return the new StripeClient, I get null again. How is it that the results are different when I'm essentially running the same code? So I'm really not sure how to correct this
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
