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

Ruby on Rails集成Stripe支付报错:line_items部分属性不可用

解决Stripe Checkout集成的InvalidRequestError错误

错误原因

你遇到的错误是因为当前使用的Stripe API版本不再允许在line_items中直接指定description、name这类字段——当你通过price参数关联已创建的Stripe Price对象时,这些信息已经存储在Stripe的Price/Product对象中,无需重复传递。

修改代码

只需要移除line_items中的description字段即可,修改后的CheckoutController#create代码如下:

class CheckoutController < ApplicationController
  def create
    order = Order.find(params[:order_id])
    prices = order.articles.map do |article|
      { price: article.stripe_pricing_id, quantity: 1 }
    end

    session = Stripe::Checkout::Session.create({
      cancel_url: order_url(order),
      success_url: root_url,
      mode: 'payment',
      payment_method_types: ['card'],
      line_items: prices
    })

    redirect_to session.url
  end
end

补充说明

如果需要自定义显示的商品描述信息,你应该在Stripe后台编辑对应的Product或Price对象的描述字段,Checkout页面会自动加载Stripe端存储的商品信息。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 16:26:25