From 3ec7d0fd536f1b2b8b90ec6cf12b3c7e3400c6f4 Mon Sep 17 00:00:00 2001 From: Maher Nabil Date: Tue, 12 Sep 2023 00:51:04 +0300 Subject: [PATCH 1/2] Enhancement to API error messages Instead of displaying to the customer the message (error.technical) for every error returned from Tamara API, this enhancement checks for the common errors returned from Tamara API and returns a message accordingly. --- .../scripts/hooks/payment/processor/tamara.js | 27 ++++++++++++++++++- .../templates/resources/tamara.properties | 8 +++++- .../templates/resources/tamara_ar.properties | 7 +++++ 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 cartridges/int_tamara_sfra/cartridge/templates/resources/tamara_ar.properties diff --git a/cartridges/int_tamara_sfra/cartridge/scripts/hooks/payment/processor/tamara.js b/cartridges/int_tamara_sfra/cartridge/scripts/hooks/payment/processor/tamara.js index 2a36d6b..03eff3f 100644 --- a/cartridges/int_tamara_sfra/cartridge/scripts/hooks/payment/processor/tamara.js +++ b/cartridges/int_tamara_sfra/cartridge/scripts/hooks/payment/processor/tamara.js @@ -3,6 +3,17 @@ var Resource = require("dw/web/Resource"); var Transaction = require("dw/system/Transaction"); var tamaraHelper = require("*/cartridge/scripts/util/tamaraHelper"); + +const errorMessageMap = { + "amount_is_higher_than_max_limit": "error.amount_is_higher_than_max_limit", + "consumer_empty_first_name": "error.consumer_empty_first_name", + "consumer_invalid_phone_number": "error.consumer_invalid_phone_number", + "consumer_invalid_email": "error.consumer_invalid_email", + "consumer_empty_email": "error.consumer_empty_email", + "total_amount_invalid_currency": "error.total_amount_invalid_currency", + "not_supported_delivery_country": "error.not_supported_delivery_country" +}; + /** * Creates a token. This should be replaced by utilizing a tokenization provider * @returns {string} a token @@ -98,7 +109,21 @@ function Authorize(orderNumber, paymentInstrument, paymentProcessor) { }); } catch (e) { error = true; - serverErrors.push(Resource.msg("error.technical", "checkout", null)); + if (e.message) { + const errorKey = Object.keys(errorMessageMap).find(key => e.message.includes(key)); + if (errorKey) { + const errorMessage = errorMessageMap[errorKey]; + if (errorMessage) { + serverErrors.push(Resource.msg(errorMessage, "tamara", null)); + } else { + serverErrors.push(Resource.msg("error.technical", "checkout", null)); + } + } else { + serverErrors.push(Resource.msg("error.technical", "checkout", null)); + } + } else { + serverErrors.push(Resource.msg("error.technical", "checkout", null)); + } tamaraHelper .getTamaraLogger() .error( diff --git a/cartridges/int_tamara_sfra/cartridge/templates/resources/tamara.properties b/cartridges/int_tamara_sfra/cartridge/templates/resources/tamara.properties index 53f3269..0ee4986 100644 --- a/cartridges/int_tamara_sfra/cartridge/templates/resources/tamara.properties +++ b/cartridges/int_tamara_sfra/cartridge/templates/resources/tamara.properties @@ -18,7 +18,13 @@ note.content.tamara.capture=[Tamara] Capture from Tamara successfully note.content.tamara.refund=[Tamara] Refund from Tamara successfully note.content.tamara.createsession=[Tamara] Create Checkout Session successfully. Triger from tamara.js - Authorize Hook. - +error.amount_is_higher_than_max_limit=Oops! It seems like your cart value exceeds your maximum limit to pay using Tamara. +error.consumer_empty_first_name=Oops! It seems like your first name is empty. +error.consumer_invalid_phone_number=Oops! Your phone number is invalid. Please provide a valid phone number to continue. +error.consumer_invalid_email=Oops! Your email is invalid. Please provide a valid email to continue. +error.consumer_empty_email=Oops! Your email is empty. You need to provide your email to continue. +error.total_amount_invalid_currency=Oops! The currency is invalid. +error.not_supported_delivery_country= Oops! It seems like your country is not supported by Tamara. error.paymenttypes.outoflist=The Paymentmethod doesn't support by Tamara display.description.on.tamara=The order {0} ({1}) is placed from {2} diff --git a/cartridges/int_tamara_sfra/cartridge/templates/resources/tamara_ar.properties b/cartridges/int_tamara_sfra/cartridge/templates/resources/tamara_ar.properties new file mode 100644 index 0000000..18bcfd8 --- /dev/null +++ b/cartridges/int_tamara_sfra/cartridge/templates/resources/tamara_ar.properties @@ -0,0 +1,7 @@ +error.amount_is_higher_than_max_limit=عذرًا! يبدو أن قيمة سلتك تتجاوز الحد الأقصى المسموح به للدفع باستخدام تمارا. +error.consumer_empty_first_name=عذرًا! يبدو أن اسمك الأول فارغ. +error.consumer_invalid_phone_number=عذرًا! رقم هاتفك غير صالح. يرجى تقديم رقم هاتف صحيح للمتابعة. +error.consumer_invalid_email=عذرًا! بريدك الإلكتروني غير صحيح. يرجى إدخال بريد إلكتروني صحيح للمتابعة. +error.consumer_empty_email=عذرًا! لايوجد لديك بريد إلكتروني. يجب أن يكون لديك بريد إلكتروني للمتابعة. +error.total_amount_invalid_currency=عذرًا! العُملة غير صالحة. +error.not_supported_delivery_country=عذرًا! يبدو أن بلدك غير مدعوم من قبل تمارا. \ No newline at end of file From 0d0558fcd445e2e594d9cdbec4eb30950610f66a Mon Sep 17 00:00:00 2001 From: Maher Nabil Date: Tue, 12 Sep 2023 11:59:57 +0300 Subject: [PATCH 2/2] Fixed: Arabic description is not returned The problem was the English description is always returned. This fix checks the current language code and returns the description accordingly. --- .../cartridge/scripts/util/tamaraHelper.js | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/cartridges/int_tamara_core/cartridge/scripts/util/tamaraHelper.js b/cartridges/int_tamara_core/cartridge/scripts/util/tamaraHelper.js index 31d337f..68f30e2 100644 --- a/cartridges/int_tamara_core/cartridge/scripts/util/tamaraHelper.js +++ b/cartridges/int_tamara_core/cartridge/scripts/util/tamaraHelper.js @@ -929,8 +929,11 @@ var tamaraHelperObj = { (available_payment_label) => { if (available_payment_label["payment_type"] === this.PAY_NOW) { paymentTypes.isEnablePaynow = true; - paymentTypes[this.METHOD_TAMARA_PAYNOW] = - available_payment_label["description_en"]; + if (tamaraHelperObj.getCurrentLangCode() === "ar") { + paymentTypes[this.METHOD_TAMARA_PAYNOW] = available_payment_label["description_ar"]; + } else { + paymentTypes[this.METHOD_TAMARA_PAYNOW] = available_payment_label["description_en"]; + } } if ( @@ -939,8 +942,11 @@ var tamaraHelperObj = { available_payment_label["instalment"] === 0 ) { paymentTypes.isEnableTamaraPay = true; - paymentTypes[this.METHOD_PAY_BY_INSTALMENTS] = - available_payment_label["description_en"]; + if (tamaraHelperObj.getCurrentLangCode() === "ar") { + paymentTypes[this.METHOD_PAY_BY_INSTALMENTS] = available_payment_label["description_ar"]; + } else { + paymentTypes[this.METHOD_PAY_BY_INSTALMENTS] = available_payment_label["description_en"]; + } } if ( @@ -949,8 +955,11 @@ var tamaraHelperObj = { available_payment_label["instalment"] === 3 ) { paymentTypes.isEnableInstalments = true; - paymentTypes[this.METHOD_TAMARA_INSTALMENTS] = - available_payment_label["description_en"]; + if (tamaraHelperObj.getCurrentLangCode() === "ar") { + paymentTypes[this.METHOD_TAMARA_INSTALMENTS] = available_payment_label["description_ar"]; + } else { + paymentTypes[this.METHOD_TAMARA_INSTALMENTS] = available_payment_label["description_en"]; + } } if ( @@ -959,8 +968,11 @@ var tamaraHelperObj = { available_payment_label["instalment"] === 6 ) { paymentTypes.is6InstalmentsEnabled = true; - paymentTypes[this.METHOD_TAMARA_6_INSTALMENTS] = - available_payment_label["description_en"]; + if (tamaraHelperObj.getCurrentLangCode() === "ar") { + paymentTypes[this.METHOD_TAMARA_6_INSTALMENTS] = available_payment_label["description_ar"]; + } else { + paymentTypes[this.METHOD_TAMARA_6_INSTALMENTS] = available_payment_label["description_en"]; + } } } );