Rails:无法向用户显示 Stripe::InvalidRequestError

Rails: Cannot display Stripe::InvalidRequestError to user

我正在使用 stripe 作为支付网关(嵌入式形式)。它工作正常。

但是,我无法在我的网站上显示卡片错误。 动作控制器错误页面中显示的错误!

我的控制器

def process
 begin

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

  charge = Stripe::Charge.create(
    :customer    => customer.id,
    :amount      => totalprice, #Amount should be in cents
    :description => orderid,
    :currency    => 'aud'
  )


  rescue Stripe::CardError => e
  flash[:error]= e.message <-------------not working?!
  redirect_to root_url
  end

  showconfirmation
end

我想在我的网站上以闪现消息的形式显示条带错误。如何解决? 谢谢

在您的代码中,您正在从 Stripe::CardError 中拯救,但最初您得到的是 Stripe::InvalidRequestError。所以,这就是为什么您的代码无法从错误中恢复的原因。

当您的请求包含无效参数时,会出现无效请求错误。参见 Stripe API Error reference

您必须确保发送的参数正确。或者,您可以根据需要从 Stripe::InvalidRequestError 中拯救:

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

  charge = Stripe::Charge.create(
      :customer    => customer.id,
      :amount      => totalprice, #Amount should be in cents
      :description => orderid,
      :currency    => 'aud'
  )

rescue Stripe::CardError, Stripe::InvalidRequestError => e
  flash[:error]= e.message
  redirect_to root_url
end