简体   繁体   English

Ruby on Rails - Stripe::CardError 未捕获被阻止的付款

[英]Ruby on Rails - Stripe::CardError not catching blocked payments

I am working on a subscription based website with content behind a paywall wired up with Stripe.我正在开发一个基于订阅的网站,其内容位于与 Stripe 连接的付费墙后面。 If the charge is declined or blocked, the account creation should cease and go back to the account create screen, with the error message.如果收费被拒绝或阻止,帐户创建应停止,go 返回帐户创建屏幕,并显示错误消息。

Here's my exception code:这是我的异常代码:

class RegistrationsController < Devise::RegistrationsController
  rescue_from Stripe::CardError, with: :stripe_card_error

  def stripe_card_error(e)
   flash[:error] = "#{e.error.message}"
   redirect_to request.referrer || "registrations#new" and return
  end

  def new
   #
  end
  def create
   #
  end
  //etc. etc.

It works fine for general card declines, however, when I run the test card numbers provided by Stripe that specifically say it will return fraudulent, the rescue does not trigger and the user account is created.它适用于一般卡拒绝,但是,当我运行 Stripe 提供的测试卡号时,明确表示它将返回欺诈性,救援不会触发并且用户帐户已创建。

I got into my stripe dashboard and I can see the customer, subscription, etc. but the payment is blocked.我进入我的条纹仪表板,我可以看到客户、订阅等,但付款被阻止。

I would have thought the Stripe::CardError would catch this, but apparently not.我原以为 Stripe::CardError 会捕捉到这一点,但显然不是。

If a payment is blocked due to potential fraud, I don't want the user account to be created.如果付款因潜在欺诈而被阻止,我不希望创建用户帐户。 I want to treat it as a general card decline with appropriate error messages.我想将其视为带有适当错误消息的一般卡片拒绝。

Is there another error I can rescue from or another way to do this?是否有另一个我可以挽救的错误或另一种方法来做到这一点? I can't find anything in Stripe's API about the best way to handle this.我在 Stripe 的 API 中找不到任何关于处理此问题的最佳方法的信息。

Stripe provides a sample error handling snippet in their API reference . Stripe在其 API 参考中提供了一个示例错误处理片段 That snippet shows that you should be catching and handling all of the following:该片段表明您应该捕获并处理以下所有内容:

  • Stripe::CardError
  • Stripe::RateLimitError
  • Stripe::InvalidRequestError (this may be the one that applies to this specific situation) Stripe::InvalidRequestError (这可能是适用于这种特定情况的错误)
  • Stripe::AuthenticationError
  • Stripe::APIConnectionError
  • Stripe::StripeError (this may also be the one you want) Stripe::StripeError (这也可能是你想要的)

And, if all else fails, you should perform a general rescue to catch anything else.而且,如果一切都失败了,您应该执行一般rescue以捕捉其他任何东西。

You can perform only the general rescue and examine the error to determine which specific one is being thrown.您只能执行一般rescue并检查错误以确定抛出哪个特定的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM