关于ruby:Rails:无法向用户显示Stripe :: InvalidRequestError

Rails: Cannot display Stripe::InvalidRequestError to user

我正在使用数据条作为支付网关(嵌入式表单)。它工作正常。

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

enter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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

我想在我的网站上将条带错误显示为Flash消息。如何解决?
谢谢。


在您的代码中,您正在从Stripe::CardError进行救援,但最初获得的是Stripe::InvalidRequestError。因此,这就是为什么您的代码无法从错误中恢复的原因。

无效请求错误发生在您的请求具有无效参数时。请参阅Stripe API错误参考。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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