Translate some error messages during payment processing if exists

- 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

Co-Authored-By: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com>
This commit is contained in:
Jean-Baptiste Bellet
2021-10-25 17:27:24 +02:00
parent 5fa5497c2f
commit 7041ec71a4
2 changed files with 23 additions and 2 deletions

View File

@@ -260,7 +260,7 @@ module Spree
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
@@ -271,6 +271,14 @@ module Spree
raise Core::GatewayError, text
end
def error_text(error)
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

View File

@@ -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:
@@ -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"