Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

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. If the charge is declined or blocked, the account creation should cease and go back to the account create screen, with the error message.

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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Stripe provides a sample error handling snippet in their API reference. 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::AuthenticationError
  • Stripe::APIConnectionError
  • Stripe::StripeError (this may also be the one you want)

And, if all else fails, you should perform a general rescue to catch anything else.

You can perform only the general rescue and examine the error to determine which specific one is being thrown.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...