Reputation: 162
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.
Upvotes: 0
Views: 259
Reputation: 6520
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.
Upvotes: 0