From cc508d0d789b928ab6fa1bc136b9fb04453d1d5d Mon Sep 17 00:00:00 2001 From: sam Date: Tue, 19 Sep 2023 20:06:53 +0530 Subject: [PATCH 1/2] feat: support multicurrency separator --- Api/RequestHandler.php | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/Api/RequestHandler.php b/Api/RequestHandler.php index 1d75a13..f792103 100644 --- a/Api/RequestHandler.php +++ b/Api/RequestHandler.php @@ -44,6 +44,13 @@ class RequestHandler extends \NoFraud\Connect\Api\Request\Handler\AbstractHandle 'vi' => 'Visa', ]; + /** + * Japan locale code + */ + private const JAPAN_LOCALE_CODE = 'ja_JP'; + + protected $_localeResolver; + /** * Constructor * @@ -60,7 +67,8 @@ public function __construct( \Magento\Directory\Model\Currency $currency, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, \Magento\Customer\Model\Customer $customer, - \Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface $orderCollectionFactory + \Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface $orderCollectionFactory, + \Magento\Framework\Locale\ResolverInterface $localeResolver ) { parent::__construct($logger, $curl); @@ -69,6 +77,7 @@ public function __construct( $this->customerRepository = $customerRepository; $this->customer = $customer; $this->orderCollectionFactory = $orderCollectionFactory; + $this->_localeResolver = $localeResolver; } /** @@ -407,8 +416,35 @@ protected function formatTotal($amount) if (empty($amount)) { return; } + + $value = $this->currency->formatTxt($amount, ['display' => \Magento\Framework\Currency::NO_SYMBOL]); - return $this->currency->formatTxt($amount, ['display' => \Magento\Framework\Currency::NO_SYMBOL]); + $separatorComa = strpos($value, ','); + $separatorDot = strpos($value, '.'); + + if ($separatorComa !== false && $separatorDot !== false) { + if ($separatorComa > $separatorDot) { + $price = preg_replace("/(\d+)\.(\d+),(\d+)/", "$1,$2.$3", $value); + } else { + $price = $value; + } + } elseif ($separatorComa !== false) { + $locale = $this->_localeResolver->getLocale(); + + /** + * It's hard code for Japan locale. + */ + $price = number_format( + (float) + str_replace(',', $locale === self::JAPAN_LOCALE_CODE ? '' : '.', $value), + 2, + '.', + ',' + ); + } else { + $price = str_replace(",", ".", $value); + } + return $price; } /** From 87e103ba07194f2320fa29926a6f4b5fedc2fdc8 Mon Sep 17 00:00:00 2001 From: sam Date: Wed, 27 Sep 2023 17:06:31 +0530 Subject: [PATCH 2/2] feat: support multicurrency separator optimize --- Api/RequestHandler.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Api/RequestHandler.php b/Api/RequestHandler.php index f792103..b54d966 100644 --- a/Api/RequestHandler.php +++ b/Api/RequestHandler.php @@ -421,12 +421,11 @@ protected function formatTotal($amount) $separatorComa = strpos($value, ','); $separatorDot = strpos($value, '.'); + $price = $value; if ($separatorComa !== false && $separatorDot !== false) { if ($separatorComa > $separatorDot) { $price = preg_replace("/(\d+)\.(\d+),(\d+)/", "$1,$2.$3", $value); - } else { - $price = $value; } } elseif ($separatorComa !== false) { $locale = $this->_localeResolver->getLocale(); @@ -441,9 +440,7 @@ protected function formatTotal($amount) '.', ',' ); - } else { - $price = str_replace(",", ".", $value); - } + } return $price; }