没有提供 API 密钥

huangapple go评论52阅读模式
英文:

No API key provided

问题

我尝试连接并在Stripe侧创建帐户,但出现错误:

未提供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;
    }

    // 其他控制器方法...

}

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

如何修复此错误?我想我需要注入一些关于StripeClient的修改吗?

英文:

I try to connect and create account on stripe side and got error:

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

My controller

&lt;?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-&gt;stripeClient = $stripeClient;
        $this-&gt;databaseManager = $databaseManager;
    }

    public function showProfile($id){
        $seller = User::find($id);

        if(is_null($seller)){
            abort(404);
        }

        return view(&#39;seller&#39;, [
            &#39;seller&#39; =&gt; $seller,
            &#39;balande&#39; =&gt; null
        ]);
    }

    public function redirectToStripe($id){
        $seller = User::find($id);

        if(is_null($seller)){
            abort(404);
        } 
        
        if(!$seller-&gt;completed_stripe_onboarding){

            $token = Str::random();
            $this-&gt;databaseManager-&gt;table(&#39;stripe_state_tokens&#39;)-&gt;insert([
                &#39;created_at&#39; =&gt; now(),
                &#39;updated_at&#39; =&gt; now(),
                &#39;seller_id&#39; =&gt; $seller-&gt;id,
                &#39;token&#39; =&gt; $token
            ]);

            // account id

            if (is_null($seller-&gt;stripe_connect_id)) {
    
                // Create account
                $account = $this-&gt;stripeClient-&gt;accounts-&gt;create([
                    &#39;country&#39; =&gt; &#39;FR&#39;,
                    &#39;type&#39;    =&gt; &#39;express&#39;,
                    &#39;email&#39;   =&gt; $seller-&gt;email,
                                   
                ]);

                $seller-&gt;update([&#39;stripe_connect_id&#39; =&gt; $account-&gt;id]);
                $seller-&gt;fresh();
            }
            $onboardLink =$this-&gt;stripeClient-&gt;accountLinks-&gt;create([
                &#39;account&#39; =&gt; $seller-&gt;stripe_connect_id,
                &#39;refresh_url&#39; =&gt; route(&#39;redirect.stripe&#39;, [&#39;id&#39; =&gt;$seller-&gt;id]),
                &#39;return_utl&#39; =&gt; route(&#39;save.stripe&#39;, [&#39;token&#39; =&gt; $token]),
                &#39;type&#39; =&gt; &#39;acccount_onboarding&#39;
            ]);

            return redirect($onboardLink-&gt;url);
        }

        $loginLink = $this-&gt;stripeClient-&gt;accounts-&gt;createloginLink($seller-&gt;stripe_connect_id);
        return redirect($loginLink-&gt;url);
    }

    public function saveStripeAccount($token){
        $stripeToken = $this-&gt;databaseManager-&gt;table(&#39;stripe_state_tokens&#39;)
        -&gt;where(&#39;token&#39;, &#39;=&#39;, $token)
        -&gt;first();

        if(is_null($stripeToken)){
            abort(404);
        }

        $seller = User::find($stripeToken-&gt;seller_id);

        $seller-&gt;update([
            &#39;completed_stripe_unboarding&#39; =&gt; true
        ]);

        return redirect()-&gt;route(&#39;seller.profile&#39;, [&#39;id&#39; =&gt; $seller-&gt;id]);
    }
}

services.php

&lt;?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.
    |
    */

    &#39;mailgun&#39; =&gt; [
        &#39;domain&#39; =&gt; env(&#39;MAILGUN_DOMAIN&#39;),
        &#39;secret&#39; =&gt; env(&#39;MAILGUN_SECRET&#39;),
        &#39;endpoint&#39; =&gt; env(&#39;MAILGUN_ENDPOINT&#39;, &#39;api.mailgun.net&#39;),
        &#39;scheme&#39; =&gt; &#39;https&#39;,
    ],

    &#39;postmark&#39; =&gt; [
        &#39;token&#39; =&gt; env(&#39;POSTMARK_TOKEN&#39;),
    ],

    &#39;ses&#39; =&gt; [
        &#39;key&#39; =&gt; env(&#39;AWS_ACCESS_KEY_ID&#39;),
        &#39;secret&#39; =&gt; env(&#39;AWS_SECRET_ACCESS_KEY&#39;),
        &#39;region&#39; =&gt; env(&#39;AWS_DEFAULT_REGION&#39;, &#39;us-east-1&#39;),
    ],
    &#39;stripe&#39; =&gt; [
        &#39;key&#39; =&gt; env(&#39;STRIPE_KEY&#39;),
        &#39;secret&#39; =&gt; env(&#39;STRIPE_SECRET&#39;),
    ],
];

appserviceprovider

&lt;?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-&gt;app-&gt;singleton(StripeClient::class, function(){
            return new StripeClient(config(&#39;stripe.secret&#39;));
           });
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
       $this-&gt;app-&gt;singleton(StripeClient::class, function(){
        return new StripeClient(config(&#39;stripe.secret&#39;));
       });
    }
}

How to fix this error ? i suppose i have to inject to modify somethings about StripeClient no?

答案1

得分: 1

在你的 AppServiceProvider 文件中,你缺少了用于查找 Stripe 详细信息的配置文件。而应该像这样:

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

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

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

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

编辑:
你只需要在 AppServiceProviderregister 函数中定义单例,不需要在 boot 函数中定义。

最后,你可能需要在使用 sk_live_xxxxxxx API 密钥之前,通过使用 sk_test_xxxxxxx API 密钥来退出 Stripe 沙盒模式。

英文:

In your AppServiceProvider file, you are missing the config file to look for the stripe details. Instead it needs to look like this:

// Option 1:
return new StripeClient(config(&#39;services.stripe.secret&#39;));

// Option 2:
return new StripeClient([&#39;api_key&#39; =&gt; config(&#39;services.stripe.secret&#39;)]);

You can test the return value of config by using the dd function (before the return statement), e.g.:

dd(config(&#39;services.stripe.secret&#39;));

EDIT:
You only need the singleton definition in the AppServiceProvider register function, not in the boot function.

Finally, you may need to get out of Stripe sandbox mode by using the sk_test_xxxxxxx api key before you can use the sk_live_xxxxxxx api key.

答案2

得分: 0

确保在您的.env文件中设置了Stripe API密钥:

STRIPE_KEY=your_stripe_api_key
STRIPE_SECRET=your_stripe_api_secret

还要清除您的配置缓存:

php artisan config:clear

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

php artisan cache:clear

英文:

Make sure that the Stripe API key is set in your .env file

STRIPE_KEY=your_stripe_api_key
STRIPE_SECRET=your_stripe_api_secret

Also clear your config cache:

php artisan config:clear

If you're using a caching mechanism, such as OPcache or APC, make sure to clear the cache after updating the configuration files.

php artisan cache:clear

huangapple
  • 本文由 发表于 2023年6月15日 17:53:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481304.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定