Skip to content

Commit

Permalink
Translate some error messages during payment processing if exists
Browse files Browse the repository at this point in the history
- Stripe error messages aren't translated. But, Stripe send the error code. Use it (if found it, and translation key does exist) to translated the error message
 - Populate the en.yml file with some of the most known errors

Update app/models/spree/payment/processing.rb

Co-Authored-By: Matt-Yorkley <[email protected]>
  • Loading branch information
jibees and Matt-Yorkley committed Nov 5, 2021
1 parent 5fa5497 commit 99e9660
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
11 changes: 10 additions & 1 deletion app/models/spree/payment/processing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def protect_from_connection_error

def gateway_error(error)
text = if error.is_a? ActiveMerchant::Billing::Response
error.params['message'] || error.params['response_reason_text'] || error.message
error_text(error)
elsif error.is_a? ActiveMerchant::ConnectionError
Spree.t(:unable_to_connect_to_gateway)
else
Expand All @@ -271,6 +271,15 @@ def gateway_error(error)
raise Core::GatewayError, text
end

def error_text(error)
byebug
if (code = error.params.dig('error', 'code')) && I18n.exists?("stripe.error_code.#{code}")
I18n.t("stripe.error_code.#{code}")
else
error.params['message'] || error.params['response_reason_text'] || error.message
end
end

# Saftey check to make sure we're not accidentally performing operations on a live gateway.
# Ex. When testing in staging environment with a copy of production data.
def check_environment
Expand Down
15 changes: 14 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ en:
using_producer_stock_settings_but_count_on_hand_set: "must be blank because using producer stock settings"
on_demand_but_count_on_hand_set: "must be blank if on demand"
limited_stock_but_no_count_on_hand: "must be specified because forcing limited stock"
stripe:
error_code:
incorrect_number: "The card number is incorrect."
invalid_number: "The card number is not a valid credit card number."
invalid_expiry_month: "The card's expiration month is invalid."
invalid_expiry_year: "The card's expiration year is invalid."
invalid_cvc: "The card's security code is invalid."
expired_card: "The card has expired."
incorrect_cvc: "The card's security code is incorrect."
incorrect_zip: "The card's zip code failed validation."
card_declined: "The card was declined."
missing: "There is no card on a customer that is being charged."
processing_error: "An error occurred while processing the card."
rate_limit: "An error occurred due to requests hitting the API too quickly. Please let us know if you're consistently running into this error."
activemodel:
attributes:
order_management/reports/enterprise_fee_summary/parameters:
Expand Down Expand Up @@ -3461,7 +3475,6 @@ See the %{link} to find out more about %{sitename}'s features and to start using
there_are_no_items_for_this_order: "There are no items for this order."
order_populator:
out_of_stock: ! '%{item} is out of stock.'

actions:
update: "Update"
cancel: "Cancel"
Expand Down

0 comments on commit 99e9660

Please sign in to comment.