diff --git a/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php b/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php
index f52886a14264d..8b9e0b1be7df2 100644
--- a/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php
+++ b/app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php
@@ -7,12 +7,17 @@
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
{
/**
* @var string
*/
- const CURRENCY_CONVERTER_URL = 'http://free.currencyconverterapi.com/api/v3/convert?q={{CURRENCY_FROM}}_{{CURRENCY_TO}}&compact=ultra'; //@codingStandardsIgnoreLine
+ 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
@@ -46,7 +51,7 @@ public function __construct(
}
/**
- * {@inheritdoc}
+ * @inheritdoc
*/
public function fetchRates()
{
@@ -74,23 +79,25 @@ 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);
- $response = $this->getServiceResponse($url);
+ $url = str_replace('{{API_KEY}}', $apiKey, $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]
- );
- }
+ $data[$currencyFrom][$to] = $this->getCurrencyRate($currencyFrom, $to, $url);
}
} finally {
ini_restore('max_execution_time');
@@ -100,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
*
@@ -137,7 +174,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
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."