Skip to content

Commit ee77f48

Browse files
authored
ENGCOM-5574: Resolve "Currency Converter API" can not get Currency Rate #24008
2 parents 5dcc881 + 3075bfd commit ee77f48

File tree

4 files changed

+57
-13
lines changed

4 files changed

+57
-13
lines changed

app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@
77

88
namespace Magento\Directory\Model\Currency\Import;
99

10+
/**
11+
* Currency rate import model (From http://free.currencyconverterapi.com/)
12+
*
13+
* Class \Magento\Directory\Model\Currency\Import\CurrencyConverterApi
14+
*/
1015
class CurrencyConverterApi extends AbstractImport
1116
{
1217
/**
1318
* @var string
1419
*/
15-
const CURRENCY_CONVERTER_URL = 'http://free.currencyconverterapi.com/api/v3/convert?q={{CURRENCY_FROM}}_{{CURRENCY_TO}}&compact=ultra'; //@codingStandardsIgnoreLine
20+
const CURRENCY_CONVERTER_URL = 'http://free.currencyconverterapi.com/api/v3/convert?q={{CURRENCY_FROM}}_{{CURRENCY_TO}}&compact=ultra&apiKey={{API_KEY}}'; //@codingStandardsIgnoreLine
1621

1722
/**
1823
* Http Client Factory
@@ -46,7 +51,7 @@ public function __construct(
4651
}
4752

4853
/**
49-
* {@inheritdoc}
54+
* @inheritdoc
5055
*/
5156
public function fetchRates()
5257
{
@@ -74,23 +79,25 @@ public function fetchRates()
7479
*/
7580
private function convertBatch($data, $currencyFrom, $currenciesTo)
7681
{
82+
$apiKey = $this->scopeConfig->getValue(
83+
'currency/currencyconverterapi/api_key',
84+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
85+
);
86+
if (!$apiKey) {
87+
$this->_messages[] = __('No API Key was specified.');
88+
return $data;
89+
}
7790
foreach ($currenciesTo as $to) {
91+
//phpcs:ignore Magento2.Functions.DiscouragedFunction
7892
set_time_limit(0);
7993
try {
8094
$url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, self::CURRENCY_CONVERTER_URL);
8195
$url = str_replace('{{CURRENCY_TO}}', $to, $url);
82-
$response = $this->getServiceResponse($url);
96+
$url = str_replace('{{API_KEY}}', $apiKey, $url);
8397
if ($currencyFrom == $to) {
8498
$data[$currencyFrom][$to] = $this->_numberFormat(1);
8599
} else {
86-
if (empty($response)) {
87-
$this->_messages[] = __('We can\'t retrieve a rate from %1 for %2.', $url, $to);
88-
$data[$currencyFrom][$to] = null;
89-
} else {
90-
$data[$currencyFrom][$to] = $this->_numberFormat(
91-
(double)$response[$currencyFrom . '_' . $to]
92-
);
93-
}
100+
$data[$currencyFrom][$to] = $this->getCurrencyRate($currencyFrom, $to, $url);
94101
}
95102
} finally {
96103
ini_restore('max_execution_time');
@@ -100,6 +107,36 @@ private function convertBatch($data, $currencyFrom, $currenciesTo)
100107
return $data;
101108
}
102109

110+
/**
111+
* Get currency rate from api
112+
*
113+
* @param string $currencyFrom
114+
* @param string $to
115+
* @param string $url
116+
* @return double
117+
*/
118+
private function getCurrencyRate($currencyFrom, $to, $url)
119+
{
120+
$rate = null;
121+
$response = $this->getServiceResponse($url);
122+
if (empty($response)) {
123+
$this->_messages[] = __('We can\'t retrieve a rate from %1 for %2.', $url, $to);
124+
$rate = null;
125+
} else {
126+
if (isset($response['error']) && $response['error']) {
127+
if (!in_array($response['error'], $this->_messages)) {
128+
$this->_messages[] = $response['error'];
129+
}
130+
$rate = null;
131+
} else {
132+
$rate = $this->_numberFormat(
133+
(double)$response[$currencyFrom . '_' . $to]
134+
);
135+
}
136+
}
137+
return $rate;
138+
}
139+
103140
/**
104141
* Get Fixer.io service response
105142
*
@@ -137,7 +174,7 @@ private function getServiceResponse($url, $retry = 0)
137174
}
138175

139176
/**
140-
* {@inheritdoc}
177+
* @inheritdoc
141178
*/
142179
protected function _convert($currencyFrom, $currencyTo)
143180
{

app/code/Magento/Directory/etc/adminhtml/system.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@
4747
</group>
4848
<group id="currencyconverterapi" translate="label" sortOrder="45" showInDefault="1" showInWebsite="0" showInStore="0">
4949
<label>Currency Converter API</label>
50-
<field id="timeout" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="0" showInStore="0">
50+
<field id="api_key" translate="label" type="obscure" sortOrder="0" showInDefault="1" showInWebsite="0" showInStore="0">
51+
<label>API Key</label>
52+
<config_path>currency/currencyconverterapi/api_key</config_path>
53+
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
54+
</field>
55+
<field id="timeout" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
5156
<label>Connection Timeout in Seconds</label>
5257
</field>
5358
</group>

app/code/Magento/Directory/etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
</fixerio>
2525
<currencyconverterapi>
2626
<timeout>100</timeout>
27+
<api_key backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
2728
</currencyconverterapi>
2829
<import>
2930
<enabled>0</enabled>

app/code/Magento/Directory/i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,4 @@ Service,Service
5252
"The """%1"" is not allowed as base currency for your subscription plan.","The """%1"" is not allowed as base currency for your subscription plan."
5353
"An invalid base currency has been entered.","An invalid base currency has been entered."
5454
"Currency rates can't be retrieved.","Currency rates can't be retrieved."
55+
"No API Key was specified.","No API Key was specified."

0 commit comments

Comments
 (0)