From ea7f1125d6b1dfc763438519914d49db1e74c755 Mon Sep 17 00:00:00 2001 From: Kristjan Heinaste Date: Tue, 4 Aug 2020 16:12:55 +0300 Subject: [PATCH 1/8] Process Hosted payment fields through translit before passing them to gateway --- simplifycommerce.php | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/simplifycommerce.php b/simplifycommerce.php index 8721b3d..ac862a9 100644 --- a/simplifycommerce.php +++ b/simplifycommerce.php @@ -237,17 +237,17 @@ public function hookPaymentOptions($params) // Set js variables to send in card tokenization $this->smarty->assign('simplify_public_key', Simplify::$public_key); - $this->smarty->assign('firstname', $cardholder_details->firstname); - $this->smarty->assign('lastname', $cardholder_details->lastname); - $this->smarty->assign('city', $cardholder_details->city); - $this->smarty->assign('address1', $cardholder_details->address1); - $this->smarty->assign('address2', $cardholder_details->address2); - $this->smarty->assign('state', isset($cardholder_details->state)?$cardholder_details->state:''); - $this->smarty->assign('postcode', $cardholder_details->postcode); + $this->smarty->assign('firstname', $this->safe($cardholder_details->firstname)); + $this->smarty->assign('lastname', $this->safe($cardholder_details->lastname)); + $this->smarty->assign('city', $this->safe($cardholder_details->city)); + $this->smarty->assign('address1', $this->safe($cardholder_details->address1)); + $this->smarty->assign('address2', $this->safe($cardholder_details->address2)); + $this->smarty->assign('state', isset($cardholder_details->state)?$this->safe($cardholder_details->state):''); + $this->smarty->assign('postcode', $this->safe($cardholder_details->postcode)); //fields related to hosted payments - $this->smarty->assign('hosted_payment_name', $this->context->shop->name); - $this->smarty->assign('hosted_payment_description', $this->context->shop->name.$this->l(' Order Number: ').(int)$this->context->cart->id); + $this->smarty->assign('hosted_payment_name', $this->safe($this->context->shop->name)); + $this->smarty->assign('hosted_payment_description', $this->safe($this->context->shop->name).$this->l(' Order Number: ').(int)$this->context->cart->id); $this->smarty->assign('hosted_payment_reference', 'Order Number'.(int)$this->context->cart->id); $this->smarty->assign('hosted_payment_amount', ($this->context->cart->getOrderTotal() * 100)); @@ -262,6 +262,30 @@ public function hookPaymentOptions($params) return [$option]; } + protected function safe($field) + { + $copy = $field; + $encoding = mb_detect_encoding($field); + if ($encoding !== 'ASCII') { + if (function_exists('transliterator_transliterate')) { + $field = transliterator_transliterate('Any-Latin; Latin-ASCII', $field); + } else if (function_exists('iconv')) { + // fall back to iconv if intl module not available + $field = iconv($encoding, 'ASCII//TRANSLIT//IGNORE', $field); + $field = str_ireplace('?', '', $field); + $field = trim($field); + } else { + // no transliteration possible, revert to original field + return $field; + } + if (!$field) { + // if translit turned the string into any false-like value, return original instead + return $copy; + } + } + return $field; + } + public function getPaymentOption() { $option = new PaymentOption(); From eaf1a64b3080c8fda25991c5e7254b6e98981c47 Mon Sep 17 00:00:00 2001 From: Kristjan Heinaste Date: Tue, 25 Aug 2020 16:44:52 +0300 Subject: [PATCH 2/8] Improved the transliterator_transliterate rules --- simplifycommerce.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simplifycommerce.php b/simplifycommerce.php index ac862a9..d4ab648 100644 --- a/simplifycommerce.php +++ b/simplifycommerce.php @@ -268,7 +268,7 @@ protected function safe($field) $encoding = mb_detect_encoding($field); if ($encoding !== 'ASCII') { if (function_exists('transliterator_transliterate')) { - $field = transliterator_transliterate('Any-Latin; Latin-ASCII', $field); + $field = transliterator_transliterate('Any-Latin; Latin-ASCII; [\u0080-\u7fff] remove', $field); } else if (function_exists('iconv')) { // fall back to iconv if intl module not available $field = iconv($encoding, 'ASCII//TRANSLIT//IGNORE', $field); From ffaf52e6cd926b1a466a3cd9cce16d49c889f20d Mon Sep 17 00:00:00 2001 From: Kristjan Heinaste Date: Tue, 1 Sep 2020 10:01:39 +0300 Subject: [PATCH 3/8] Removed function exist check for iconv, added string lenght handling for safe() method --- simplifycommerce.php | 40 ++++++++++++++++++------------- views/js/simplify.js | 33 ++++++++++++++++++------- views/templates/front/payment.tpl | 7 +++--- 3 files changed, 50 insertions(+), 30 deletions(-) diff --git a/simplifycommerce.php b/simplifycommerce.php index d4ab648..dd195bf 100644 --- a/simplifycommerce.php +++ b/simplifycommerce.php @@ -237,16 +237,14 @@ public function hookPaymentOptions($params) // Set js variables to send in card tokenization $this->smarty->assign('simplify_public_key', Simplify::$public_key); - $this->smarty->assign('firstname', $this->safe($cardholder_details->firstname)); - $this->smarty->assign('lastname', $this->safe($cardholder_details->lastname)); - $this->smarty->assign('city', $this->safe($cardholder_details->city)); - $this->smarty->assign('address1', $this->safe($cardholder_details->address1)); - $this->smarty->assign('address2', $this->safe($cardholder_details->address2)); - $this->smarty->assign('state', isset($cardholder_details->state)?$this->safe($cardholder_details->state):''); - $this->smarty->assign('postcode', $this->safe($cardholder_details->postcode)); + $this->smarty->assign('city', $this->safe($cardholder_details->city, 2, 50)); + $this->smarty->assign('address1', $this->safe($cardholder_details->address1, 0, 255)); + $this->smarty->assign('address2', $this->safe($cardholder_details->address2, 0, 255)); + $this->smarty->assign('state', isset($cardholder_details->state)?$this->safe($cardholder_details->state, 0, 255):''); + $this->smarty->assign('postcode', $this->safe($cardholder_details->postcode, 0, 255)); //fields related to hosted payments - $this->smarty->assign('hosted_payment_name', $this->safe($this->context->shop->name)); + $this->smarty->assign('hosted_payment_name', $this->safe($this->context->shop->name), 0, 255); $this->smarty->assign('hosted_payment_description', $this->safe($this->context->shop->name).$this->l(' Order Number: ').(int)$this->context->cart->id); $this->smarty->assign('hosted_payment_reference', 'Order Number'.(int)$this->context->cart->id); $this->smarty->assign('hosted_payment_amount', ($this->context->cart->getOrderTotal() * 100)); @@ -257,30 +255,38 @@ public function hookPaymentOptions($params) $this->smarty->assign('currency_iso', $currency->iso_code); + $this->smarty->assign( + 'customer_name', + $this->safe($cardholder_details->firstname . ' ' . $this->safe($cardholder_details->lastname), 2, 50) + ); + $option = $this->getPaymentOption(); return [$option]; } - protected function safe($field) + protected function safe($field, $minLength = 0, $maxLength = 0) { - $copy = $field; $encoding = mb_detect_encoding($field); if ($encoding !== 'ASCII') { if (function_exists('transliterator_transliterate')) { $field = transliterator_transliterate('Any-Latin; Latin-ASCII; [\u0080-\u7fff] remove', $field); - } else if (function_exists('iconv')) { + } else { // fall back to iconv if intl module not available $field = iconv($encoding, 'ASCII//TRANSLIT//IGNORE', $field); $field = str_ireplace('?', '', $field); $field = trim($field); - } else { - // no transliteration possible, revert to original field - return $field; } - if (!$field) { - // if translit turned the string into any false-like value, return original instead - return $copy; + } + $field = (string) $field; + if ($minLength > 0) { + if (strlen($field) < $minLength) { + return null; + } + } + if ($maxLength > 0) { + if (strlen($field) > $maxLength) { + return null; } } return $field; diff --git a/views/js/simplify.js b/views/js/simplify.js index 6b20248..c21b2ce 100644 --- a/views/js/simplify.js +++ b/views/js/simplify.js @@ -169,21 +169,36 @@ $(document).ready(function () { return true; } else { - SimplifyCommerce.generateToken({ + var card = { key: simplifyPublicKey, card: { number: $(".simplify-card-number").val().trim().replace(/\s+/g, ''), cvc: $(".simplify-card-cvc").val(), expMonth: $("#simplify-cc-details select[name='Date_Month']").val(), - expYear: $("#simplify-cc-details select[name='Date_Year']").val().substring(2), - name: simplifyFirstname + ' ' + simplifyLastname, - addressCity: simplifyCity, - addressLine1: simplifyAddress1, - addressLine2: simplifyAddress2, - addressState: simplifyState, - addressZip: simplifyPostcode + expYear: $("#simplify-cc-details select[name='Date_Year']").val().substring(2) } - }, simplifyResponseHandler); + }; + + if (simplifyCustomerName) { + card['card']['name'] = simplifyCustomerName; + } + if (simplifyCity) { + card['card']['addressCity'] = simplifyCustomerName; + } + if (simplifyAddress1) { + card['card']['addressLine1'] = simplifyAddress1; + } + if (simplifyAddress2) { + card['card']['addressLine2'] = simplifyAddress2; + } + if (simplifyState) { + card['card']['addressState'] = simplifyState; + } + if (simplifyPostcode) { + card['card']['addressZip'] = simplifyPostcode; + } + + SimplifyCommerce.generateToken(card, simplifyResponseHandler); } return false; /* Prevent the form from submitting with the default action */ diff --git a/views/templates/front/payment.tpl b/views/templates/front/payment.tpl index 81ad14f..850b3eb 100644 --- a/views/templates/front/payment.tpl +++ b/views/templates/front/payment.tpl @@ -16,8 +16,7 @@