Handle connection errors gracefully

This commit is contained in:
Brian Quinn 2010-12-17 11:20:09 +00:00
parent 4b31bc2019
commit d6620ea283
1 changed files with 22 additions and 4 deletions

View File

@ -28,6 +28,9 @@ module Spree::PaypalExpress
end end
redirect_to (gateway.redirect_url_for response.token, :review => payment_method.preferred_review) redirect_to (gateway.redirect_url_for response.token, :review => payment_method.preferred_review)
rescue ActiveMerchant::ConnectionError => e
gateway_error I18n.t(:unable_to_connect_to_gateway)
redirect_to :back
end end
# Outbound redirect to PayPal from checkout payments step # Outbound redirect to PayPal from checkout payments step
@ -51,6 +54,9 @@ module Spree::PaypalExpress
end end
redirect_to (gateway.redirect_url_for response.token, :review => payment_method.preferred_review) redirect_to (gateway.redirect_url_for response.token, :review => payment_method.preferred_review)
rescue ActiveMerchant::ConnectionError => e
gateway_error I18n.t(:unable_to_connect_to_gateway)
redirect_to :back
end end
# Inbound post from PayPal after (possible) successful completion # Inbound post from PayPal after (possible) successful completion
@ -103,12 +109,16 @@ module Spree::PaypalExpress
else else
paypal_finish paypal_finish
end end
else else
gateway_error(@ppx_details) gateway_error(@ppx_details)
#Failed trying to get payment details from PPX #Failed trying to get payment details from PPX
redirect_to edit_order_checkout_url(@order, :step => "payment") redirect_to edit_order_checkout_url(@order, :step => "payment")
end end
rescue ActiveMerchant::ConnectionError => e
gateway_error I18n.t(:unable_to_connect_to_gateway)
redirect_to edit_order_url(@order)
end end
# Local call from A) Order Review Screen, or B) Automatically after paypal_confirm (no review). # Local call from A) Order Review Screen, or B) Automatically after paypal_confirm (no review).
@ -180,6 +190,9 @@ module Spree::PaypalExpress
#Failed trying to complete pending payment! #Failed trying to complete pending payment!
redirect_to edit_order_checkout_url(@order, :step => "payment") redirect_to edit_order_checkout_url(@order, :step => "payment")
end end
rescue ActiveMerchant::ConnectionError => e
gateway_error I18n.t(:unable_to_connect_to_gateway)
redirect_to edit_order_url(@order)
end end
private private
@ -347,10 +360,15 @@ module Spree::PaypalExpress
end end
def gateway_error(response) def gateway_error(response)
text = response.params['message'] || if response.is_a? ActiveMerchant::Billing::Response
response.params['response_reason_text'] || text = response.params['message'] ||
response.message response.params['response_reason_text'] ||
msg = "#{I18n.t('gateway_error')} ... #{text}" response.message
else
text = response.to_s
end
msg = "#{I18n.t('gateway_error')}: #{text}"
logger.error(msg) logger.error(msg)
flash[:error] = msg flash[:error] = msg
end end