【问题标题】:PHP Laravel 5.6 -The Stripe API key is not defined! error messagePHP Laravel 5.6 - Stripe API 密钥未定义!错误信息
【发布时间】:2020-07-19 09:26:29
【问题描述】:

任何帮助或建议将不胜感激! 我正在尝试在启用 Stripe Sandbox 的 PHP Laravel 5.6 中退款。 我正在使用一个表,在该表中我有两个文本字段来定义 Stripe 测试和实时键。

这是我在 PHP Laravel 中的服务代码:


return [

    /*
    |--------------------------------------------------------------------------
    | Third Party Services
    |--------------------------------------------------------------------------
    |
    | This file is for storing the credentials for third party services such
    | as Stripe, Mailgun, SparkPost and others. This file provides a sane
    | default location for this type of information, allowing packages
    | to have a conventional place to find your various credentials.
    |
    */

    'mailgun' => [
        'domain' => env('MAILGUN_DOMAIN'),
        'secret' => env('MAILGUN_SECRET'),
    ],

    'ses' => [
        'key' => env('SES_KEY'),
        'secret' => env('SES_SECRET'),
        'region' => 'us-east-1',
    ],

    'sparkpost' => [
        'secret' => env('SPARKPOST_SECRET'),
    ],

    'stripe' => [
        'model' => App\User::class,
        'key' => config('settings.stripe_sandbox_enabled') ? config('settings.stripe_test_key_pk') : config('settings.stripe_live_key_pk'),
        'secret' => config('settings.stripe_sandbox_enabled') ? config('settings.stripe_test_key_sk') : config('stripe_live_key_sk'),        
    ],

];

这是我的刀片文件中通过 Stripe 发出退款的代码行:


            //issue refund
            $invoice = $booking->invoice()->first();

            if($invoice['payment_method'] == __('app.credit_card'))
            {
                try {
                    //refund via stripe
                    if($booking->invoice->is_partial == 1){
                        if($booking->invoice->amount_left != 0){
                        //     print_r($booking->invoice->transaction_id);
                        // exit();
                            Stripe::refunds()->create($booking->invoice->transaction_id, $booking->invoice->first_payment, [
                                'reason' => 'requested_by_customer'
                            ]);
                        }
                        else{
                            $transaction_ids = explode(',', $booking->invoice->transaction_id);
                            Stripe::refunds()->create($transaction_ids[0], $booking->invoice->first_payment, [
                                    'reason' => 'requested_by_customer'
                                ]);
                            Stripe::refunds()->create($transaction_ids[1], $booking->invoice->second_payment, [
                                    'reason' => 'requested_by_customer'
                                ]);
                        }
                    }
                    else{
                        Stripe::refunds()->create($booking->invoice->transaction_id, $booking->invoice->amount , [
                            'reason' => 'requested_by_customer'
                        ]);
                    }

【问题讨论】:

    标签: php laravel laravel-5 stripe-payments


    【解决方案1】:

    我建议在每个环境中设置正确的 Stripe 键,然后在你的配置文件中读取这些配置值:

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

    在您的情况下,这意味着您必须设置这些环境值:

    STRIPE_KEY=your-stripe-key
    STRIPE_SECRET=your-stripe-secret
    

    您不再需要 stripe_sandbox_enabled 变量,当您使用沙盒(测试)密钥时,将自动使用沙盒环境。

    【讨论】:

    • 感谢您的建议!我实际上发现了这个问题并在下面发布了我的修复。我希望这可以帮助有类似问题的功能开发人员:)。
    【解决方案2】:

    大家好,我实际上在“vendor/cartalyst/stripe/src/Config.php”中发现了问题,我更改了以下代码:

        {
            $this->setVersion($version);
    
            $this->setApiKey($apiKey ?: getenv('STRIPE_API_KEY'));
    
            $this->setApiVersion($apiVersion ?: getenv('STRIPE_API_VERSION') ?: '2017-06-05');
    
            if (! $this->apiKey) {
                throw new \RuntimeException('The Stripe API key is not defined!');
            }
        }
    

    收件人:

        {
            $this->setVersion($version);
    
            $this->setApiKey(config('settings.stripe_sandbox_enabled') ? config('settings.stripe_test_key_sk') : config('settings.stripe_live_key_sk'));
            $this->setApiVersion($apiVersion ?: getenv('STRIPE_API_VERSION') ?: '2017-06-05');
    
            if (! $this->apiKey) {
                throw new \RuntimeException('The Stripe API key is not defined!');
            }
        }
    

    并且现在可以 100% 工作我现在可以根据使用的密钥类型退款。谢谢大家,我希望这可以帮助遇到类似问题的人。

    【讨论】:

      猜你喜欢
      • 2020-04-24
      • 2015-06-29
      • 2017-01-18
      • 2015-11-23
      • 2021-05-25
      • 1970-01-01
      • 2020-08-08
      • 2018-12-18
      • 1970-01-01
      相关资源
      最近更新 更多