如何修复Stripe连接时出现的No API key provided错误?
修复Stripe API密钥未提供的错误
尝试连接并创建Stripe账户时遇到如下错误:
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.
以下是相关代码:
SellerController代码
<?php namespace App\Http\Controllers; use App\Models\User; use Illuminate\Database\DatabaseManager; use Illuminate\Http\Request; use Stripe\StripeClient; use Illuminate\Support\Str; use Stripe\Exception\ApiErrorException; use Stripe\Stripe; class SellerController extends Controller { protected StripeClient $stripeClient; protected DatabaseManager $databaseManager; public function __construct(StripeClient $stripeClient, DatabaseManager $databaseManager) { $this->stripeClient = $stripeClient; $this->databaseManager = $databaseManager; } public function showProfile($id){ $seller = User::find($id); if(is_null($seller)){ abort(404); } return view('seller', [ 'seller' => $seller, 'balande' => null ]); } public function redirectToStripe($id){ $seller = User::find($id); if(is_null($seller)){ abort(404); } if(!$seller->completed_stripe_onboarding){ $token = Str::random(); $this->databaseManager->table('stripe_state_tokens')->insert([ 'created_at' => now(), 'updated_at' => now(), 'seller_id' => $seller->id, 'token' => $token ]); // account id if (is_null($seller->stripe_connect_id)) { // Create account $account = $this->stripeClient->accounts->create([ 'country' => 'FR', 'type' => 'express', 'email' => $seller->email, ]); $seller->update(['stripe_connect_id' => $account->id]); $seller->fresh(); } $onboardLink =$this->stripeClient->accountLinks->create([ 'account' => $seller->stripe_connect_id, 'refresh_url' => route('redirect.stripe', ['id' =>$seller->id]), 'return_utl' => route('save.stripe', ['token' => $token]), 'type' => 'acccount_onboarding' ]); return redirect($onboardLink->url); } $loginLink = $this->stripeClient->accounts->createloginLink($seller->stripe_connect_id); return redirect($loginLink->url); } public function saveStripeAccount($token){ $stripeToken = $this->databaseManager->table('stripe_state_tokens') ->where('token', '=', $token) ->first(); if(is_null($stripeToken)){ abort(404); } $seller = User::find($stripeToken->seller_id); $seller->update([ 'completed_stripe_unboarding' => true ]); return redirect()->route('seller.profile', ['id' => $seller->id]); } }
services.php代码
<?php return [ /* |-------------------------------------------------------------------------- | Third Party Services |-------------------------------------------------------------------------- | | This file is for storing the credentials for third party services such | as Mailgun, Postmark, AWS and more. This file provides the de facto | location for this type of information, allowing packages to have | a conventional file to locate the various service credentials. | */ 'mailgun' => [ 'domain' => env('MAILGUN_DOMAIN'), 'secret' => env('MAILGUN_SECRET'), 'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'), 'scheme' => 'https', ], 'postmark' => [ 'token' => env('POSTMARK_TOKEN'), ], 'ses' => [ 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), ], 'stripe' => [ 'key' => env('STRIPE_KEY'), 'secret' => env('STRIPE_SECRET'), ], ];
AppServiceProvider代码
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Schema; use Carbon; use Stripe\StripeCLient; class AppServiceProvider extends ServiceProvider { /** * Register any application services. */ public function register(): void { $this->app->singleton(StripeClient::class, function(){ return new StripeClient(config('stripe.secret')); }); } /** * Bootstrap any application services. */ public function boot(): void { $this->app->singleton(StripeClient::class, function(){ return new StripeClient(config('stripe.secret')); }); } }
修复方案
1. 修正AppServiceProvider中的类名拼写错误
代码里use Stripe\StripeCLient;存在拼写错误,应该是Stripe\StripeClient(注意是小写i)。这个错误会导致Laravel容器无法正确解析StripeClient类,最终注入的实例没有携带API密钥。
修改后的AppServiceProvider:
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Schema; use Carbon; use Stripe\StripeClient; // 修正拼写错误 class AppServiceProvider extends ServiceProvider { /** * Register any application services. */ public function register(): void { $this->app->singleton(StripeClient::class, function(){ return new StripeClient(config('stripe.secret')); }); } /** * Bootstrap any application services. */ public function boot(): void { // 移除boot方法中的重复单例注册,只保留register方法内的定义即可 Schema::defaultStringLength(191); // 可选:解决数据库字符串长度限制问题 } }
2. 移除重复的单例注册
不需要在boot方法中再次注册StripeClient单例,仅在register方法中注册一次即可,避免重复定义引发冲突。
3. 验证环境变量配置
确保.env文件中正确配置了Stripe密钥:
STRIPE_KEY=你的Stripe公钥 STRIPE_SECRET=你的Stripe密钥
执行php artisan config:clear清除配置缓存,确保新配置生效。
4. 修复代码中的其他拼写错误
redirectToStripe方法中,return_utl改为return_url,type字段的acccount_onboarding改为account_onboarding(多写了一个c);saveStripeAccount方法中,completed_stripe_unboarding改为completed_stripe_onboarding(拼写错误)。
这些错误虽不直接导致API密钥问题,但会影响后续流程正常运行。
内容的提问来源于stack exchange,提问作者user21942112
相关产品推荐
相关产品推荐

