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

Stripe创建关联账户Checkout Session报Invalid array异常

问题成因

抛出Stripe\Exception\InvalidRequestException并提示Invalid array的核心原因是line_items参数结构不符合Stripe API规范:

  • Stripe要求line_items是由商品项对象组成的二维数组,每个商品项单独作为一个子数组存在
  • 现有代码直接将单个商品的price_data、quantity键值对写在line_items下,缺少包裹单个商品项的外层数组,API无法解析为合法的商品列表,直接触发格式错误。

代码中其余和关联账户路由相关的参数(stripe_account请求头、payment_intent_data.application_fee_amount)写法均正确,不是报错诱因。另外代码存在冗余:同时写了静态方法设置API Key和实例化StripeClient两种初始化逻辑,二者保留其一即可,不影响接口调用结果。

修复方案

调整line_items的数组层级,给单个商品项外层包裹一层数组,匹配API要求的列表格式,修复后完整代码如下:

// 保留一种初始化方式即可,无需重复配置
\Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));

$session = \Stripe\Checkout\Session::create([       
  'payment_method_types' => ['card'],
  'line_items' => [
    [
      'price_data' => [
        'unit_amount' => 25000,
        'currency' => 'usd',
        'product_data' => ['name' => 'Product8', 'active' => true],
      ],
      'quantity' => 2,
    ]
  ],
  'mode' => 'payment',
  'success_url' => 'http://devweb.drivinggradebook.com/',
  'cancel_url' => 'https://www.drivinggradebook.com/',
  'payment_intent_data' => [
    'application_fee_amount' => 10,
  ],
], ['stripe_account' => 'acct_1L7ugjSJzLhcy6eF']);
扩展提示
  • 后续需要添加多个商品时,直接在line_items的外层数组中追加新的商品子数组即可
  • 路由到关联账户的Checkout Session创建逻辑中,application_fee_amount单位为最小货币单位,示例中10代表0.1美元,注意金额计算不要出错。

内容的提问来源于stack exchange,提问作者Anup Patel

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.01 04:48:17