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

React中Stripe line_items添加多商品问题求助

解决Stripe line_items添加多商品问题

Stripe创建结账会话时,line_items要求是包含多个商品对象的数组,每个对象必须包含price(Stripe价格ID)和quantity(正整数)字段。以下是正确实现方式和常见错误排查:

正确代码示例(Node.js)

const stripe = require('stripe')('你的Stripe API密钥');

async function createCheckoutSession(req, res) {
  // 假设从前端接收的商品列表,格式:[{priceId: 'price_xxxx', quantity: 1}, ...]
  const userProducts = req.body.products;

  // 构造符合要求的line_items数组
  const lineItems = userProducts.map(product => ({
    price: product.priceId,
    quantity: product.quantity
  }));

  const session = await stripe.checkout.sessions.create({
    payment_method_types: ['card'],
    line_items: lineItems,
    mode: 'payment',
    success_url: 'https://你的域名.com/success',
    cancel_url: 'https://你的域名.com/cancel'
  });

  res.json({ checkoutUrl: session.url });
}

常见错误排查点

  • 确认line_items是数组类型,而非单个对象或非数组结构
  • 检查每个数组项的price值是Stripe后台生成的价格ID(格式如price_xxxxxxxxxx),不能直接传入金额数值
  • 确保quantity是正整数,不能为0、负数或非数字类型
  • 核对Stripe SDK版本:v10+版本支持直接传入price字段,旧版本可能需要通过price_data定义价格,但多商品场景同样需要数组结构

如果问题仍未解决,建议把截图中的具体代码文本(比如数组构造逻辑、错误提示内容)贴出来,方便精准定位问题。

内容的提问来源于stack exchange,提问作者Gabriel Ribeiro

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 00:47:06