You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Stripe支付网关报错:$config需为字符串或数组,API密钥读取失败求助

Stripe API密钥获取失败,报错$config must be a string or array

问题详情

尝试从.env文件获取Stripe API密钥时出错,试过在services.php中添加配置、执行php artisan config:cache命令,都没解决问题。

相关配置代码

config/services.php配置

'stripe' => [
        'stripe_key' => env('STRIPE_KEY'),
        'secret_key' => env('STRIPE_SECRET'),
        ]
    ],

StripePaymentController.php代码

<?php

namespace App\Http\Controllers;

use Stripe;
use Illuminate\View\View;
use Illuminate\Http\Request;

class StripePaymentController extends Controller
{
    public function stripe():View {
        return view('stripe');
    }

    public function stripeCheckout(Request $request){
        $stripe = new \Stripe\StripeClient(env('STRIPE_SECRET'));

        $redirectUrl = route('stripe.checkout.success').'?session_id={CHECKOUT_SESSION_ID}';

        $response = $stripe->checkout->sessions->create([
            'success_url' => $redirectUrl,
            'customer_email' => 'demo@quickairlink.com',
            'payment_method_type' => ['link','card'],
            'lne_items' => [
                [
                    'price_data' => [
                        'product_data' => [
                            'name' => $request->product,
                        ],
                        'unit_price' => 100 * $request->price,
                        'currency' => 'NZD',
                    ],
                    'quantity' => 1,
                ],
            ],
            'mode' => 'payment',
            'allow_promotion_codes' => true,
        ]);
        return redirect($response['url']);
    }

    public function stripeCheckoutSuccess(Request $request){
        $stripe = new \Stripe\StripeClient(env('STRIPE_SECRET'));

        $response = $stripe->checkout->sessions->retrieve($request->session_id);

        return redirect()->route('stripe.index')->with('success','Payment Successful.');
    }
}

报错信息

Stripe支付网关错误:$config must be a string or array


解决办法

  1. 确认.env文件的密钥配置
    打开.env文件,确保已正确填入Stripe的公钥和密钥,格式如下:

    STRIPE_KEY=你的Stripe公钥
    STRIPE_SECRET=你的Stripe密钥
    

    密钥前后不要加空格或引号,保存后执行php artisan config:clear(开发环境优先用clear,避免缓存导致的配置不更新)。

  2. 修复services.php的语法错误
    你提供的services.php代码末尾多了一个闭合括号,正确的配置应该是:

    'stripe' => [
        'stripe_key' => env('STRIPE_KEY'),
        'secret_key' => env('STRIPE_SECRET'),
    ],
    

    多余的括号会导致配置加载异常,进而无法正确读取密钥。

  3. 排查密钥是否被正确读取
    报错提示$config must be a string or array,本质是env('STRIPE_SECRET')返回了无效值(大概率是null)。可以在控制器中添加调试代码验证:

    dd(env('STRIPE_SECRET'));
    

    如果输出null,要么是.env文件未被正确加载,要么是密钥名称拼写错误(注意大小写,代码中的STRIPE_SECRET要和.env文件中的完全一致)。

  4. 修正控制器中的拼写错误
    代码中有两处明显的拼写错误,会导致Stripe接口调用失败:

    • payment_method_type 应改为 payment_method_types(复数形式,缺少末尾的s)
    • lne_items 应改为 line_items(单词拼写错误,将line写成了lne)

内容的提问来源于stack exchange,提问作者Nihar Shah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 04:18:16