diff --git a/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php b/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php new file mode 100644 index 0000000000000..c32fb36eb5b2e --- /dev/null +++ b/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php @@ -0,0 +1,139 @@ +scopeConfig = $scopeConfig; + $this->httpClientFactory = $httpClientFactory; + } + + /** + * {@inheritdoc} + */ + public function fetchRates() + { + $data = []; + $currencies = $this->_getCurrencyCodes(); + $defaultCurrencies = $this->_getDefaultCurrencyCodes(); + + foreach ($defaultCurrencies as $currencyFrom) { + if (!isset($data[$currencyFrom])) { + $data[$currencyFrom] = []; + } + $data = $this->convertBatch($data, $currencyFrom, $currencies); + ksort($data[$currencyFrom]); + } + return $data; + } + + /** + * Return currencies convert rates in batch mode + * + * @param array $data + * @param string $currencyFrom + * @param array $currenciesTo + * @return array + */ + private function convertBatch($data, $currencyFrom, $currenciesTo) + { + foreach($currenciesTo as $to) { + set_time_limit(0); + try { + $url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, self::CURRENCY_CONVERTER_URL); + $url = str_replace('{{CURRENCY_TO}}', $to, $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 { + $data[$currencyFrom][$to] = $this->_numberFormat( + (double)$response[$currencyFrom . '_' . $to] + ); + } + } + } finally { + ini_restore('max_execution_time'); + } + } + + return $data; + } + + /** + * Get Fixer.io service response + * + * @param string $url + * @param int $retry + * @return array + */ + private function getServiceResponse($url, $retry = 0) + { + /** @var \Magento\Framework\HTTP\ZendClient $httpClient */ + $httpClient = $this->httpClientFactory->create(); + $response = []; + + try { + $jsonResponse = $httpClient->setUri( + $url + )->setConfig( + [ + 'timeout' => $this->scopeConfig->getValue( + 'currency/currencyconverterapi/timeout', + \Magento\Store\Model\ScopeInterface::SCOPE_STORE + ), + ] + )->request( + 'GET' + )->getBody(); + + $response = json_decode($jsonResponse, true); + } catch (\Exception $e) { + if ($retry == 0) { + $response = $this->getServiceResponse($url, 1); + } + } + return $response; + } + + /** + * {@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 15a82e006bffe..cae3b1c41db36 100644 --- a/app/code/Magento/Directory/etc/adminhtml/system.xml +++ b/app/code/Magento/Directory/etc/adminhtml/system.xml @@ -52,6 +52,12 @@ + + + + + + diff --git a/app/code/Magento/Directory/etc/config.xml b/app/code/Magento/Directory/etc/config.xml index fa4e9d64d10d8..de3ff626bc12c 100644 --- a/app/code/Magento/Directory/etc/config.xml +++ b/app/code/Magento/Directory/etc/config.xml @@ -27,6 +27,9 @@ 100 + + 100 + 0 diff --git a/app/code/Magento/Directory/etc/di.xml b/app/code/Magento/Directory/etc/di.xml index 02e16af29ea14..4d0e51ab9f45a 100644 --- a/app/code/Magento/Directory/etc/di.xml +++ b/app/code/Magento/Directory/etc/di.xml @@ -22,6 +22,10 @@ Fixer.io Magento\Directory\Model\Currency\Import\FixerIo + + Currency Converter API + Magento\Directory\Model\Currency\Import\CurrencyConverterApi +