From f4a0b8feda1de0b0794c0433041167ad936ce0a6 Mon Sep 17 00:00:00 2001 From: Andrea Vassallo Date: Fri, 27 Oct 2017 18:55:26 +0200 Subject: [PATCH 1/7] Add js.erb file that maps Braintree error slugs with I18n translations --- .../braintree_errors.js.erb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 app/assets/javascripts/solidus_paypal_braintree/braintree_errors.js.erb diff --git a/app/assets/javascripts/solidus_paypal_braintree/braintree_errors.js.erb b/app/assets/javascripts/solidus_paypal_braintree/braintree_errors.js.erb new file mode 100644 index 00000000..ebfe3200 --- /dev/null +++ b/app/assets/javascripts/solidus_paypal_braintree/braintree_errors.js.erb @@ -0,0 +1,17 @@ +BraintreeError = { + HOSTED_FIELDS_FIELDS_EMPTY: "<%= I18n.t('solidus_paypal_braintree.errors.empty_fields')%>", + HOSTED_FIELDS_FIELDS_INVALID: "<%= I18n.t('solidus_paypal_braintree.errors.invalid_fields')%>", + HOSTED_FIELDS_FAILED_TOKENIZATION: "<%= I18n.t('solidus_paypal_braintree.errors.invalid_card')%>", + HOSTED_FIELDS_TOKENIZATION_NETWORK_ERROR: "<%= I18n.t('solidus_paypal_braintree.errors.network_error')%>", + HOSTED_FIELDS_FIELD_DUPLICATE_IFRAME: "<%= I18n.t('solidus_paypal_braintree.errors.duplicate_iframe')%>", + HOSTED_FIELDS_TOKENIZATION_FAIL_ON_DUPLICATE: "<%= I18n.t('solidus_paypal_braintree.errors.fail_on_duplicate')%>", + HOSTED_FIELDS_TOKENIZATION_CVV_VERIFICATION_FAILED: "<%= I18n.t('solidus_paypal_braintree.errors.cvv_verification_failed')%>", + + getErrorFromSlug: function(slug) { + // Default error message + error = "<%= I18n.t('solidus_paypal_braintree.errors.default_error')%>" + if (slug in BraintreeError) + error = BraintreeError[slug] + return error + } +} \ No newline at end of file From 2a5c56794ba90d34efff284e4771b5922f70c1e4 Mon Sep 17 00:00:00 2001 From: Andrea Vassallo Date: Fri, 27 Oct 2017 18:56:58 +0200 Subject: [PATCH 2/7] Add braintree_error script into manifest --- app/assets/javascripts/solidus_paypal_braintree/frontend.js | 1 + 1 file changed, 1 insertion(+) diff --git a/app/assets/javascripts/solidus_paypal_braintree/frontend.js b/app/assets/javascripts/solidus_paypal_braintree/frontend.js index 962d4f2d..0aa200af 100644 --- a/app/assets/javascripts/solidus_paypal_braintree/frontend.js +++ b/app/assets/javascripts/solidus_paypal_braintree/frontend.js @@ -4,6 +4,7 @@ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the // the compiled file. // +//= require solidus_paypal_braintree/braintree_errors //= require solidus_paypal_braintree/constants //= require solidus_paypal_braintree/promise //= require solidus_paypal_braintree/client From d2a385e0ea7683ae07248335b24066b804446234 Mon Sep 17 00:00:00 2001 From: Andrea Vassallo Date: Fri, 27 Oct 2017 18:57:52 +0200 Subject: [PATCH 3/7] Add English Braintree messages --- config/locales/en.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/config/locales/en.yml b/config/locales/en.yml index c7a6602d..64daf439 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -18,6 +18,15 @@ en: braintree: Braintree nonce: Nonce token: Token + errors: + default_error: "Something bad happened!" + empty_fields: "All fields are empty! Please fill out the form." + invalid_fields: "Some payment input fields are invalid." + invalid_card: "Credit card data is incorrect." + network_error: "Network error occurred." + duplicate_iframe: "Duplicate Braintree iframe." + fail_on_duplicate: "This payment method already exists in your vault." + cvv_verification_failed: "CVV did not pass verification." payment_type: label: Payment Type apple_pay_card: Apple Pay From 2268df451d6688a7a372c1d36355f60f2ee363b3 Mon Sep 17 00:00:00 2001 From: Andrea Vassallo Date: Fri, 27 Oct 2017 18:59:29 +0200 Subject: [PATCH 4/7] Refactor Braintree error handler function to get localization messages --- .../solidus_paypal_braintree/constants.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/solidus_paypal_braintree/constants.js b/app/assets/javascripts/solidus_paypal_braintree/constants.js index 4a8fdaeb..af0bfd0d 100644 --- a/app/assets/javascripts/solidus_paypal_braintree/constants.js +++ b/app/assets/javascripts/solidus_paypal_braintree/constants.js @@ -6,14 +6,11 @@ SolidusPaypalBraintree = { clientTokens: Spree.pathFor('solidus_paypal_braintree/client_token'), transactions: Spree.pathFor('solidus_paypal_braintree/transactions') }, - + // Override to provide your own error messages. braintreeErrorHandle: function(braintreeError) { - var $contentContainer = $("#content"); - var $flash = $("
" + braintreeError.name + ": " + braintreeError.message + "
"); - $contentContainer.prepend($flash); - - $flash.show().delay(5000).fadeOut(500); + BraintreeError.getErrorFromSlug(braintreeError.code); + SolidusPaypalBraintree.showError(error); }, classes: { @@ -34,6 +31,13 @@ SolidusPaypalBraintree = { } } }, + + showError: function(error) { + var $contentContainer = $("#content"); + var $flash = $("
" + error + "
"); + $contentContainer.prepend($flash); + $flash.show().delay(5000).fadeOut(500); + }, createHostedForm: function() { return SolidusPaypalBraintree._factory(SolidusPaypalBraintree.config.classes.hostedForm(), arguments); From 3d4289242d8eef816c251a45860e281b01d23ab3 Mon Sep 17 00:00:00 2001 From: Andrea Vassallo Date: Fri, 10 Nov 2017 14:47:33 +0100 Subject: [PATCH 5/7] Add italian Braintree error messages translations --- config/locales/it.yml | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 config/locales/it.yml diff --git a/config/locales/it.yml b/config/locales/it.yml new file mode 100644 index 00000000..3f3627ea --- /dev/null +++ b/config/locales/it.yml @@ -0,0 +1,11 @@ +it: + solidus_paypal_braintree: + errors: + default_error: "Qualcosa è andato storto." + empty_fields: "I campi non possono essere vuoti." + invalid_fields: "Alcuni campi non sono corretti." + invalid_card: "I dati della carta di credito non sono corretti." + network_error: "Si è verificato un errore di rete." + duplicate_iframe: "Ci sono due form Braintree." + fail_on_duplicate: "Questo metodo di pagamento già esiste." + cvv_verification_failed: "Il codice CVV non è corretto." From fb2c13b061449f56ae654f2554c949c9b646cdf4 Mon Sep 17 00:00:00 2001 From: Andrea Vassallo Date: Fri, 10 Nov 2017 17:26:11 +0100 Subject: [PATCH 6/7] Change RSpec error messages expect with I18n --- spec/features/frontend/braintree_credit_card_checkout_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/features/frontend/braintree_credit_card_checkout_spec.rb b/spec/features/frontend/braintree_credit_card_checkout_spec.rb index 91595f93..daeb10cc 100644 --- a/spec/features/frontend/braintree_credit_card_checkout_spec.rb +++ b/spec/features/frontend/braintree_credit_card_checkout_spec.rb @@ -65,7 +65,7 @@ before(:each) do expect(page).to have_selector("iframe[type='number']") click_button "Save and Continue" - expect(page).to have_text "BraintreeError: All fields are empty. Cannot tokenize empty card fields." + expect(page).to have_text I18n.t("solidus_paypal_braintree.errors.empty_fields") expect(page).to have_selector("input[type='submit']:enabled") end @@ -75,7 +75,7 @@ expect(page).to have_selector("input[type='submit']:enabled") click_button "Save and Continue" - expect(page).to have_text "BraintreeError: All fields are empty. Cannot tokenize empty card fields." + expect(page).to have_text I18n.t("solidus_paypal_braintree.errors.empty_fields") end end From e2c9d1eff87abced598fe2f9eee2178c2e3fd155 Mon Sep 17 00:00:00 2001 From: Andrea Vassallo Date: Fri, 24 Nov 2017 10:33:41 +0100 Subject: [PATCH 7/7] Change js.erb file to html.erb and render it as partial MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit By using an html.erb partial we make sure that if you change the locale files you don’t need a new asset precompile to actually pick up the changes --- .../braintree_errors.js.erb | 17 ----------------- .../solidus_paypal_braintree/constants.js | 4 ++-- .../solidus_paypal_braintree/frontend.js | 1 - .../spree/shared/_braintree_errors.html.erb | 19 +++++++++++++++++++ .../shared/_braintree_hosted_fields.html.erb | 2 ++ 5 files changed, 23 insertions(+), 20 deletions(-) delete mode 100644 app/assets/javascripts/solidus_paypal_braintree/braintree_errors.js.erb create mode 100644 app/views/spree/shared/_braintree_errors.html.erb diff --git a/app/assets/javascripts/solidus_paypal_braintree/braintree_errors.js.erb b/app/assets/javascripts/solidus_paypal_braintree/braintree_errors.js.erb deleted file mode 100644 index ebfe3200..00000000 --- a/app/assets/javascripts/solidus_paypal_braintree/braintree_errors.js.erb +++ /dev/null @@ -1,17 +0,0 @@ -BraintreeError = { - HOSTED_FIELDS_FIELDS_EMPTY: "<%= I18n.t('solidus_paypal_braintree.errors.empty_fields')%>", - HOSTED_FIELDS_FIELDS_INVALID: "<%= I18n.t('solidus_paypal_braintree.errors.invalid_fields')%>", - HOSTED_FIELDS_FAILED_TOKENIZATION: "<%= I18n.t('solidus_paypal_braintree.errors.invalid_card')%>", - HOSTED_FIELDS_TOKENIZATION_NETWORK_ERROR: "<%= I18n.t('solidus_paypal_braintree.errors.network_error')%>", - HOSTED_FIELDS_FIELD_DUPLICATE_IFRAME: "<%= I18n.t('solidus_paypal_braintree.errors.duplicate_iframe')%>", - HOSTED_FIELDS_TOKENIZATION_FAIL_ON_DUPLICATE: "<%= I18n.t('solidus_paypal_braintree.errors.fail_on_duplicate')%>", - HOSTED_FIELDS_TOKENIZATION_CVV_VERIFICATION_FAILED: "<%= I18n.t('solidus_paypal_braintree.errors.cvv_verification_failed')%>", - - getErrorFromSlug: function(slug) { - // Default error message - error = "<%= I18n.t('solidus_paypal_braintree.errors.default_error')%>" - if (slug in BraintreeError) - error = BraintreeError[slug] - return error - } -} \ No newline at end of file diff --git a/app/assets/javascripts/solidus_paypal_braintree/constants.js b/app/assets/javascripts/solidus_paypal_braintree/constants.js index af0bfd0d..59e7575f 100644 --- a/app/assets/javascripts/solidus_paypal_braintree/constants.js +++ b/app/assets/javascripts/solidus_paypal_braintree/constants.js @@ -6,7 +6,7 @@ SolidusPaypalBraintree = { clientTokens: Spree.pathFor('solidus_paypal_braintree/client_token'), transactions: Spree.pathFor('solidus_paypal_braintree/transactions') }, - + // Override to provide your own error messages. braintreeErrorHandle: function(braintreeError) { BraintreeError.getErrorFromSlug(braintreeError.code); @@ -31,7 +31,7 @@ SolidusPaypalBraintree = { } } }, - + showError: function(error) { var $contentContainer = $("#content"); var $flash = $("
" + error + "
"); diff --git a/app/assets/javascripts/solidus_paypal_braintree/frontend.js b/app/assets/javascripts/solidus_paypal_braintree/frontend.js index 0aa200af..962d4f2d 100644 --- a/app/assets/javascripts/solidus_paypal_braintree/frontend.js +++ b/app/assets/javascripts/solidus_paypal_braintree/frontend.js @@ -4,7 +4,6 @@ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the // the compiled file. // -//= require solidus_paypal_braintree/braintree_errors //= require solidus_paypal_braintree/constants //= require solidus_paypal_braintree/promise //= require solidus_paypal_braintree/client diff --git a/app/views/spree/shared/_braintree_errors.html.erb b/app/views/spree/shared/_braintree_errors.html.erb new file mode 100644 index 00000000..149acd83 --- /dev/null +++ b/app/views/spree/shared/_braintree_errors.html.erb @@ -0,0 +1,19 @@ + diff --git a/app/views/spree/shared/_braintree_hosted_fields.html.erb b/app/views/spree/shared/_braintree_hosted_fields.html.erb index 58790a41..027e912a 100644 --- a/app/views/spree/shared/_braintree_hosted_fields.html.erb +++ b/app/views/spree/shared/_braintree_hosted_fields.html.erb @@ -1,5 +1,7 @@ <% prefix = "payment_source[#{id}]" %> +<%= render partial: "spree/shared/braintree_errors" %> +
<%= label_tag "card_number#{id}", Spree::CreditCard.human_attribute_name(:number), class: "required" %>