I've looked over the Stripe documentation on errors, but am still having some trouble handling / redirecting these errors properly. Basically no matter what happens I want them to go back to the edit
action (via edit_profile_path
) and display them a message (whether successful or not).
I have a form on the edit
action that POSTs to the update
action. This is working properly with a valid credit card (charge is in Stripe dashboard). I am using Stripe.js.
class ExtrasController < ApplicationController
def edit
@extras = current_user.extras
end
def update
Stripe.api_key = "hidden"
token = params[:stripeToken]
begin
charge = Stripe::Charge.create(
:amount => 5000, # amount in cents
:currency => "usd",
:card => token,
:description => current_user.email
)
rescue Stripe::CardError => e
# redirect_to edit_extras_path, notice: e.message
# What I'm trying to do, but obviously results in AbstractController::DoubleRenderError
rescue => e
# Something else happened, completely unrelated to Stripe
# Display a generic error message
end
redirect_to edit_extras_path, notice: "Card charged successfully."
end
end
Ruby also provides a separate class for an exception that is known as an Exception class which contains different types of methods. The code in which an exception is raised, is enclosed between the begin/end block, so you can use a rescue clause to handle this type of exception.
Exception handling in Ruby on Rails is similar to exception handling in Ruby. Which means, we enclose the code that could raise an exception in a begin/end block and use rescue clauses to tell Ruby the types of exceptions we want to handle.
Test strip errors often occurs when there is a problem with the test strip, or when the environment in which the test strip is used is below or above the operating range.
stripe 7.1. 0. Stripe is the easiest way to accept payments online. See https://stripe.com for details.
Though you now can pass a flash message to redirect_to
, you can also still manipulate the flash by itself.
So a minor change to your update code lets you do what you want:
def update
Stripe.api_key = "hidden"
token = params[:stripeToken]
begin
charge = Stripe::Charge.create(
:amount => 5000, # amount in cents
:currency => "usd",
:card => token,
:description => current_user.email
)
# No exceptions were raised; Set our success message.
flash[:notice] = 'Card charged successfully.'
rescue Stripe::CardError => e
# CardError; display an error message.
flash[:notice] = 'That card is presently on fire!'
rescue => e
# Some other error; display an error message.
flash[:notice] = 'Some error occurred.'
end
redirect_to edit_extras_path
end
To make your messages more clear in their purpose, you might want to swap out notice
in the error states for an alert
or error
flash type; you can then easily style them with CSS to indicate success or failure at a glance. (Bootstrap and Foundation, for example, each provide styles for displaying alerts of various types.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With