gpt4 book ai didi

ruby-on-rails - Rails Stripe 检索 ID 错误 "No such plan"

转载 作者:行者123 更新时间:2023-12-02 13:54:55 25 4
gpt4 key购买 nike

所以我的计划显示 Stripe::InvalidRequestError at/orders/26/payments
没有这样的计划:我的计划的标题。
此代码应检查该计划是否已存在,如果不存在,则创建它并向用户订阅它。我认为这有效,因为它适用于我已经有一个具有相同 ID 的计划并且它说“计划已存在”的情况。如何防止此错误发生?
这是我的代码:

class PaymentsController < ApplicationController
before_action :set_order

def new
end

def create
@user = current_user

customer = Stripe::Customer.create(
source: params[:stripeToken],
email: params[:stripeEmail],
)

# Storing the customer.id in the customer_id field of user
@user.customer_id = customer.id

@plan = Stripe::Plan.retrieve(@order.service.title)
unless @plan
plan = Stripe::Plan.create(
:name => @order.service.title,
:id => @order.service.title,
:interval => "month",
:currency => @order.amount.currency,
:amount => @order.amount_pennies,
)
else
subscription = Stripe::Subscription.create(
:customer => @user.customer_id,
:plan => @order.service.title
)
end

@order.update(payment: plan.to_json, state: 'paid')
redirect_to order_path(@order)

rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_order_payment_path(@order)

end

private

def set_order
@order = Order.where(state: 'pending').find(params[:order_id])
end

end

最佳答案

documentation表示如果您尝试检索不存在的计划,则会引发错误。所以你只需要捕获错误:

begin
@plan = Stripe::Plan.retrieve(@order.service.title)
rescue
@plan = Stripe::Plan.create(...)
end

关于ruby-on-rails - Rails Stripe 检索 ID 错误 "No such plan",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44912248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com