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

Stripe Plan更新报URI::InvalidURIError问题求助

问题:Stripe Plan更新时出现URI::InvalidURIError错误

基于Stripe Plan的创建逻辑编写更新方法,仅将Stripe调用从create改为update,但出现URI::InvalidURIError,创建功能可正常运行。


相关代码

update_feature_plans_job.rb

class UpdateFeaturePlansJob < ApplicationJob
  queue_as :default

  def perform(service)
    key = service.user.access_code
    Stripe.api_key = key

    service.features.each do |feature|
      Stripe::Plan.update({
                            id: "#{feature.title.parameterize}-feature_#{feature.id}",
                            amount: (feature.amount.to_r * 100).to_i,
                            currency: 'eur',
                            interval: 'month',
                            product: { name: feature.title },
                            nickname: feature.title.parameterize
                          })
    end
  end
end

service_controller.rb 相关代码

def update
    respond_to do |format|
      if @service.update(service_params)
        UpdateFeaturePlansJob.perform_now(@service)
        format.html { redirect_to @service, notice: 'service was successfully updated.' }
        format.json { render :show, status: :ok, location: @service }
      else
        format.html { render :edit }
        format.json { render json: @service.errors, status: :unprocessable_entity }
      end
    end
  end

可正常运行的创建功能代码

class CreateFeaturePlansJob < ApplicationJob
  queue_as :default

  def perform(service)
    key = service.user.access_code
    Stripe.api_key = key

    service.features.each do |feature|
      Stripe::Plan.create({
        id: "#{feature.title.parameterize}-feature_#{feature.id}",
        amount: (feature.amount.to_r * 100).to_i,
        currency: 'eur',
        interval: 'month',
        product: { name: feature.title },
        nickname: feature.title.parameterize
      })
    end
  end
end

错误信息

URI::InvalidURIError in ServicesController#update
bad URI(is not URI?): "/v1/plans/{:id=>\"feature-title-feature_14\", :amount=>1000, :currency=>\"eur\", :interval=>\"month\", :product=>{:name=>\"Feature Title\"}, :nickname=>\"feature-title\"}"

请求参数

{"_method"=>"patch",
 "authenticity_token"=>"aErX41FPUztfEjbILjgHGojdaCJsDp3jwlwAhnIoab2_JEZjRffBIEv0ZvH7RpNwSAZzUOmMBYzxX__qvTElgQ",
 "service"=>
  {"title"=>"Test Title",
   "goal"=>"100",
   "color"=>"#FFFFFF",
   "location"=>"Berlin",
   "description"=>"<div>Description main</div>",
   "features_attributes"=>{"0"=>{"_destroy"=>"false", "title"=>"Feature Title", "description"=>"desc feature", "amount"=>"10.0", "quantity"=>"1", "id"=>"14"}}},
 "commit"=>"Update Service",
 "id"=>"5"}

修复方案

问题核心是Stripe API的调用语法错误:Stripe::Plan.update要求第一个参数是要更新的Plan的ID,第二个参数才是更新的字段哈希。你现在把包含ID的完整哈希直接传给update,导致Stripe把整个哈希当作ID拼接到请求URI里,触发无效URI错误。

修改update_feature_plans_job.rb中的调用代码:

service.features.each do |feature|
  plan_id = "#{feature.title.parameterize}-feature_#{feature.id}"
  Stripe::Plan.update(
    plan_id,
    {
      amount: (feature.amount.to_r * 100).to_i,
      nickname: feature.title.parameterize,
      product: { name: feature.title }
    }
  )
end

额外注意:Stripe的Plan有部分字段不可更新(比如currency、interval),如果强行更新会返回API错误,建议只保留官方允许更新的字段。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 11:22:36