From e4f4701d360162f2d2aeffa1d252065857fd2573 Mon Sep 17 00:00:00 2001 From: Eden Date: Mon, 5 Aug 2019 07:48:48 +0700 Subject: [PATCH 1/6] Resolve "Currency Converter API" can not get Currency Rate (issue 24007) --- .../Currency/Import/CurrencyConverterApi.php | 25 +++++++++++++++---- .../Directory/etc/adminhtml/system.xml | 7 +++++- app/code/Magento/Directory/etc/config.xml | 1 + 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php b/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php index f52886a14264d..6bd99c7db1aa8 100644 --- a/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php +++ b/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php @@ -7,6 +7,11 @@ namespace Magento\Directory\Model\Currency\Import; +/** + * Currency rate import model (From http://free.currencyconverterapi.com/) + * + * Class \Magento\Directory\Model\Currency\Import\CurrencyConverterApi + */ class CurrencyConverterApi extends AbstractImport { /** @@ -46,7 +51,7 @@ public function __construct( } /** - * {@inheritdoc} + * @inheritdoc */ public function fetchRates() { @@ -75,10 +80,15 @@ public function fetchRates() private function convertBatch($data, $currencyFrom, $currenciesTo) { foreach ($currenciesTo as $to) { + //phpcs:ignore Magento2.Functions.DiscouragedFunction set_time_limit(0); try { $url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, self::CURRENCY_CONVERTER_URL); $url = str_replace('{{CURRENCY_TO}}', $to, $url); + $url = $url . '&apiKey=' . $this->scopeConfig->getValue( + 'currency/currencyconverterapi/api_key', + \Magento\Store\Model\ScopeInterface::SCOPE_STORE + ); $response = $this->getServiceResponse($url); if ($currencyFrom == $to) { $data[$currencyFrom][$to] = $this->_numberFormat(1); @@ -87,9 +97,14 @@ private function convertBatch($data, $currencyFrom, $currenciesTo) $this->_messages[] = __('We can\'t retrieve a rate from %1 for %2.', $url, $to); $data[$currencyFrom][$to] = null; } else { - $data[$currencyFrom][$to] = $this->_numberFormat( - (double)$response[$currencyFrom . '_' . $to] - ); + if (isset($response['error']) && $response['error']) { + $this->_messages[] = __($response['error']); + $data[$currencyFrom][$to] = null; + } else { + $data[$currencyFrom][$to] = $this->_numberFormat( + (double)$response[$currencyFrom . '_' . $to] + ); + } } } } finally { @@ -137,7 +152,7 @@ private function getServiceResponse($url, $retry = 0) } /** - * {@inheritdoc} + * @inheritdoc */ protected function _convert($currencyFrom, $currencyTo) { diff --git a/app/code/Magento/Directory/etc/adminhtml/system.xml b/app/code/Magento/Directory/etc/adminhtml/system.xml index ec5fa35b6a152..899453c771e4b 100644 --- a/app/code/Magento/Directory/etc/adminhtml/system.xml +++ b/app/code/Magento/Directory/etc/adminhtml/system.xml @@ -47,7 +47,12 @@ - + + + currency/currencyconverterapi/api_key + Magento\Config\Model\Config\Backend\Encrypted + + diff --git a/app/code/Magento/Directory/etc/config.xml b/app/code/Magento/Directory/etc/config.xml index 276d7088cc2ea..c18c4f29d5822 100644 --- a/app/code/Magento/Directory/etc/config.xml +++ b/app/code/Magento/Directory/etc/config.xml @@ -24,6 +24,7 @@ 100 + 0 From 405fbcbf49d0d380380246c4e0ebacb933cbdb54 Mon Sep 17 00:00:00 2001 From: Eden Date: Fri, 9 Aug 2019 10:37:02 +0700 Subject: [PATCH 2/6] Handle the "empty API Key" case issue 24007 --- .../Currency/Import/CurrencyConverterApi.php | 18 ++++++++++++++---- app/code/Magento/Directory/i18n/en_US.csv | 1 + 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php b/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php index 6bd99c7db1aa8..6fdc95763b8b1 100644 --- a/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php +++ b/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php @@ -19,6 +19,11 @@ class CurrencyConverterApi extends AbstractImport */ const CURRENCY_CONVERTER_URL = 'http://free.currencyconverterapi.com/api/v3/convert?q={{CURRENCY_FROM}}_{{CURRENCY_TO}}&compact=ultra'; //@codingStandardsIgnoreLine + /** + * @var string + */ + const API_KEY = 'apiKey'; + /** * Http Client Factory * @@ -79,16 +84,21 @@ public function fetchRates() */ private function convertBatch($data, $currencyFrom, $currenciesTo) { + $apiKey = $this->scopeConfig->getValue( + 'currency/currencyconverterapi/api_key', + \Magento\Store\Model\ScopeInterface::SCOPE_STORE + ); + if (!$apiKey) { + $this->_messages[] = __('No API Key was specified.'); + return $data; + } foreach ($currenciesTo as $to) { //phpcs:ignore Magento2.Functions.DiscouragedFunction set_time_limit(0); try { $url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, self::CURRENCY_CONVERTER_URL); $url = str_replace('{{CURRENCY_TO}}', $to, $url); - $url = $url . '&apiKey=' . $this->scopeConfig->getValue( - 'currency/currencyconverterapi/api_key', - \Magento\Store\Model\ScopeInterface::SCOPE_STORE - ); + $url = $url . '&' . self::API_KEY . '=' . $apiKey; $response = $this->getServiceResponse($url); if ($currencyFrom == $to) { $data[$currencyFrom][$to] = $this->_numberFormat(1); diff --git a/app/code/Magento/Directory/i18n/en_US.csv b/app/code/Magento/Directory/i18n/en_US.csv index 3dcd2ceebf134..9bd059c752064 100644 --- a/app/code/Magento/Directory/i18n/en_US.csv +++ b/app/code/Magento/Directory/i18n/en_US.csv @@ -52,3 +52,4 @@ Service,Service "The """%1"" is not allowed as base currency for your subscription plan.","The """%1"" is not allowed as base currency for your subscription plan." "An invalid base currency has been entered.","An invalid base currency has been entered." "Currency rates can't be retrieved.","Currency rates can't be retrieved." +"No API Key was specified.","No API Key was specified." From 90253073b0a1e5253eda0410f5f000c75351d752 Mon Sep 17 00:00:00 2001 From: Oleksandr Kravchuk Date: Fri, 9 Aug 2019 10:25:56 +0300 Subject: [PATCH 3/6] Update CurrencyConverterApi.php --- .../Model/Currency/Import/CurrencyConverterApi.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php b/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php index 6fdc95763b8b1..d3c530ee6c315 100644 --- a/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php +++ b/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php @@ -17,12 +17,7 @@ class CurrencyConverterApi extends AbstractImport /** * @var string */ - const CURRENCY_CONVERTER_URL = 'http://free.currencyconverterapi.com/api/v3/convert?q={{CURRENCY_FROM}}_{{CURRENCY_TO}}&compact=ultra'; //@codingStandardsIgnoreLine - - /** - * @var string - */ - const API_KEY = 'apiKey'; + const CURRENCY_CONVERTER_URL = 'http://free.currencyconverterapi.com/api/v3/convert?q={{CURRENCY_FROM}}_{{CURRENCY_TO}}&compact=ultra&apiKey={{API_KEY}}'; //@codingStandardsIgnoreLine /** * Http Client Factory @@ -98,7 +93,7 @@ private function convertBatch($data, $currencyFrom, $currenciesTo) try { $url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, self::CURRENCY_CONVERTER_URL); $url = str_replace('{{CURRENCY_TO}}', $to, $url); - $url = $url . '&' . self::API_KEY . '=' . $apiKey; + $url = str_replace('{{API_KEY}}', $apiKey, $url); $response = $this->getServiceResponse($url); if ($currencyFrom == $to) { $data[$currencyFrom][$to] = $this->_numberFormat(1); From 23d566ba0357d8728d66847f4a9418bf258a6ad3 Mon Sep 17 00:00:00 2001 From: Oleksandr Kravchuk Date: Fri, 9 Aug 2019 10:28:02 +0300 Subject: [PATCH 4/6] Update CurrencyConverterApi.php --- .../Directory/Model/Currency/Import/CurrencyConverterApi.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php b/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php index d3c530ee6c315..e5b8f06ef2cf2 100644 --- a/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php +++ b/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php @@ -103,7 +103,7 @@ private function convertBatch($data, $currencyFrom, $currenciesTo) $data[$currencyFrom][$to] = null; } else { if (isset($response['error']) && $response['error']) { - $this->_messages[] = __($response['error']); + $this->_messages[] = $response['error']; $data[$currencyFrom][$to] = null; } else { $data[$currencyFrom][$to] = $this->_numberFormat( From 3fbdb4d78b3b3c5270edd0b9b4b0fd909c41b254 Mon Sep 17 00:00:00 2001 From: Eden Date: Wed, 14 Aug 2019 21:08:40 +0700 Subject: [PATCH 5/6] Fix dupplicate error message issue 24007 --- .../Directory/Model/Currency/Import/CurrencyConverterApi.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php b/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php index e5b8f06ef2cf2..12eb7ffcb200b 100644 --- a/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php +++ b/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php @@ -103,7 +103,9 @@ private function convertBatch($data, $currencyFrom, $currenciesTo) $data[$currencyFrom][$to] = null; } else { if (isset($response['error']) && $response['error']) { - $this->_messages[] = $response['error']; + if (!in_array($response['error'], $this->_messages)) { + $this->_messages[] = $response['error']; + } $data[$currencyFrom][$to] = null; } else { $data[$currencyFrom][$to] = $this->_numberFormat( From a0113091c2f68ba357ec9366fbfc60509a51bb51 Mon Sep 17 00:00:00 2001 From: Eden Date: Wed, 14 Aug 2019 21:48:13 +0700 Subject: [PATCH 6/6] refactor code to pass static test --- .../Currency/Import/CurrencyConverterApi.php | 47 ++++++++++++------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php b/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php index 12eb7ffcb200b..8b9e0b1be7df2 100644 --- a/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php +++ b/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php @@ -94,25 +94,10 @@ private function convertBatch($data, $currencyFrom, $currenciesTo) $url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, self::CURRENCY_CONVERTER_URL); $url = str_replace('{{CURRENCY_TO}}', $to, $url); $url = str_replace('{{API_KEY}}', $apiKey, $url); - $response = $this->getServiceResponse($url); if ($currencyFrom == $to) { $data[$currencyFrom][$to] = $this->_numberFormat(1); } else { - if (empty($response)) { - $this->_messages[] = __('We can\'t retrieve a rate from %1 for %2.', $url, $to); - $data[$currencyFrom][$to] = null; - } else { - if (isset($response['error']) && $response['error']) { - if (!in_array($response['error'], $this->_messages)) { - $this->_messages[] = $response['error']; - } - $data[$currencyFrom][$to] = null; - } else { - $data[$currencyFrom][$to] = $this->_numberFormat( - (double)$response[$currencyFrom . '_' . $to] - ); - } - } + $data[$currencyFrom][$to] = $this->getCurrencyRate($currencyFrom, $to, $url); } } finally { ini_restore('max_execution_time'); @@ -122,6 +107,36 @@ private function convertBatch($data, $currencyFrom, $currenciesTo) return $data; } + /** + * Get currency rate from api + * + * @param string $currencyFrom + * @param string $to + * @param string $url + * @return double + */ + private function getCurrencyRate($currencyFrom, $to, $url) + { + $rate = null; + $response = $this->getServiceResponse($url); + if (empty($response)) { + $this->_messages[] = __('We can\'t retrieve a rate from %1 for %2.', $url, $to); + $rate = null; + } else { + if (isset($response['error']) && $response['error']) { + if (!in_array($response['error'], $this->_messages)) { + $this->_messages[] = $response['error']; + } + $rate = null; + } else { + $rate = $this->_numberFormat( + (double)$response[$currencyFrom . '_' . $to] + ); + } + } + return $rate; + } + /** * Get Fixer.io service response *