php 未提供API密钥

vfh0ocws  于 2023-06-20  发布在  PHP
关注(0)|答案(2)|浏览(144)

我尝试在条带端连接并创建帐户,但出现错误:
未提供API密钥。在构造StripeClient示例时设置API密钥,或者使用$opts参数中的api_key密钥在每个请求的基础上提供它。
我的控制器

<?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'),
    ],
];

应用服务提供者

<?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'));
       });
    }
}

如何修复此错误?我想我必须注入修改一些关于StripeClient没有?

kb5ga3dv

kb5ga3dv1#

在您的AppServiceProvider文件中,缺少用于查找条带详细信息的配置文件。相反,它需要看起来像这样:

// Option 1:
return new StripeClient(config('services.stripe.secret'));

// Option 2:
return new StripeClient(['api_key' => config('services.stripe.secret')]);

您可以使用dd函数(在return语句之前)测试config的返回值,例如:

dd(config('services.stripe.secret'));

编辑:您只需要AppServiceProvider register函数中的单例定义,而不需要boot函数中的单例定义。
最后,您可能需要使用sk_test_xxxxxxx API密钥退出Stripe沙箱模式,然后才能使用sk_live_xxxxxxx api密钥。

bq3bfh9z

bq3bfh9z2#

确保在.env文件中设置了API密钥

STRIPE_KEY=your_stripe_api_key
STRIPE_SECRET=your_stripe_api_secret

同时清除配置缓存:

php artisan config:clear

如果您使用的是缓存机制,如OPcache或APC,请确保在更新配置文件后清除该高速缓存。

php artisan cache:clear

相关问题