【问题标题】:Laravel: Stripe No API key provided. (HINT: set your API key using Stripe::setApiKey()Laravel:Stripe 未提供 API 密钥。 (提示:使用 Stripe::setApiKey() 设置您的 API 密钥
【发布时间】:2015-12-09 17:39:30
【问题描述】:

是的,我知道,那里有一个完全相同的问题,但“解决方案”没有得到批准,也没有按应有的规定指定。

所以: 1)我通过php composer.phar安装了stripe library v.3.0 require stripe/etc

它安装正常,(否则我实际上不会收到该错误)

2) 我在 Head 部分的刀片表单中拥有公共测试密钥

3)然后在控制器中,我在从表单接收数据的公共函数中包含以下内容:(没问题,不是我的真正密钥)

$token = Input::get('stripeToken');
Stripe::setApiKey("sk_test_1VJeJsdfsdgdgVbJODDDD");

3)我也把它放在 .env 文件中作为

STRIPE_API_SECRET='sk_test_1VJeJsvj7l2ft2eXXsevDD'

并从 config/services.php 调用

'stripe' => [
        'model'  => App\User::class,
        'key'    => '',
        'secret' => env('STRIPE_API_SECRET'),
    ],

但我不断收到该错误。

SO 的另一个相同问题说它已经通过以下方式“解决”了它:

the solution was to put the stripe api key into AppServiceProvider, into register() class.

那完全是模糊的、不准确的,也不知道他在说什么。

有人知道吗?非常感谢

【问题讨论】:

    标签: laravel-5 stripe-payments


    【解决方案1】:

    如果您查看Billable 代码:

    /**
     * Get the Stripe API key.
     *
     * @return string
     */
    public static function getStripeKey()
    {
        return static::$stripeKey ?: getenv('STRIPE_SECRET');
    }
    
    /**
     * Set the Stripe API key.
     *
     * @param  string  $key
     * @return void
     */
    public static function setStripeKey($key)
    {
        static::$stripeKey = $key;
    }
    

    它想要定义静态变量或STRIPE_SECRET 环境变量。

    other answer 有点模糊,但提供了一个解决方案。在你的AppServiceProvider:

    public function register()
    {
        \App\Models\User::setStripeKey(\Config::get('services.stripe.secret'));
    }
    

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,并通过在我的控制器构造函数中手动设置密钥来解决它,如下所示:

      class SubscriptionController extends Controller {
      
          public function __construct() {
              Stripe::setApiKey(env('STRIPE_SECRET'));
          }
      
          public function getPlans() {
              dd(Plan::all());
          }
      }
      

      【讨论】:

      • 这有效,但仅适用于此处提到的特定控制器。如果您想确保无论在何处进行 Stripe API 调用都不会发生错误,请不要使用它(除非您想将其添加到应用程序中的每个控制器中)。
      【解决方案3】:

      尝试清除缓存

      php artisan config:cache
      

      https://laravel.com/docs/7.x/configuration#configuration-caching

      【讨论】:

        【解决方案4】:

        我遇到了类似的问题,这是因为“条纹”在 config/services/php.ini 中配置了两次。你确定 config/services.php 里面只有一个 'stripe' 实例吗?

        这是我使用 Stripe 的示例 config/services.php,供参考:

        <?php
        return [
        'mandrill' => [
            'secret' => env('MANDRILL_SECRET'),
        ],
        
        'ses' => [
            'key'    => env('SES_KEY'),
            'secret' => env('SES_SECRET'),
            'region' => 'us-east-1',
        ],
        
        'stripe' => [
            'model'  => 'App\Models\User',
            'key'    => env('STRIPE_API_PUBLIC'),
            'secret' => env('STRIPE_API_SECRET'),
        ],
        ];
        

        【讨论】:

          【解决方案5】:

          设置公共静态 $apiKey='sk_test_your api key';来自stripe.php

          【讨论】:

            【解决方案6】:

            综合答案,为整个应用解决它:

            .env文件:

            STRIPE_KEY=pk_live_exampleexampleexampleexampleexampleexampleexample
            STRIPE_SECRET=sk_live_exampleexampleexampleexampleexampleexampleexample
            

            config\services.php文件:

            'stripe' => [
                [...]
                'key' => env('STRIPE_KEY'),
                'secret' => env('STRIPE_SECRET'),
            ],
            

            app\Providers\AppServiceProvider.php文件:

            public function boot()
            {
                [...]
                Stripe::setApiKey(config('services.stripe.secret'));
                [...]
            }
            

            之后,您也可以在命令行上运行:

            php artisan config:clear
            

            解释:

            • 我使用boot() 方法而不是其他答案中建议的register(),原因解释为here
            • php artisan config:clear 并不总是必要的,但请确保您没有使用缓存配置,以防止应用获取上面添加的更改。

            【讨论】:

              猜你喜欢
              • 2017-05-31
              • 2020-10-22
              • 2021-05-25
              • 2015-11-23
              • 1970-01-01
              • 2015-11-16
              • 2014-06-23
              • 2017-01-18
              • 2023-02-22
              相关资源
              最近更新 更多