Skip to content

Commit e4f4701

Browse files
committed
Resolve "Currency Converter API" can not get Currency Rate (issue 24007)
1 parent d3c55bb commit e4f4701

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
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
/**
@@ -46,7 +51,7 @@ public function __construct(
4651
}
4752

4853
/**
49-
* {@inheritdoc}
54+
* @inheritdoc
5055
*/
5156
public function fetchRates()
5257
{
@@ -75,10 +80,15 @@ public function fetchRates()
7580
private function convertBatch($data, $currencyFrom, $currenciesTo)
7681
{
7782
foreach ($currenciesTo as $to) {
83+
//phpcs:ignore Magento2.Functions.DiscouragedFunction
7884
set_time_limit(0);
7985
try {
8086
$url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, self::CURRENCY_CONVERTER_URL);
8187
$url = str_replace('{{CURRENCY_TO}}', $to, $url);
88+
$url = $url . '&apiKey=' . $this->scopeConfig->getValue(
89+
'currency/currencyconverterapi/api_key',
90+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
91+
);
8292
$response = $this->getServiceResponse($url);
8393
if ($currencyFrom == $to) {
8494
$data[$currencyFrom][$to] = $this->_numberFormat(1);
@@ -87,9 +97,14 @@ private function convertBatch($data, $currencyFrom, $currenciesTo)
8797
$this->_messages[] = __('We can\'t retrieve a rate from %1 for %2.', $url, $to);
8898
$data[$currencyFrom][$to] = null;
8999
} else {
90-
$data[$currencyFrom][$to] = $this->_numberFormat(
91-
(double)$response[$currencyFrom . '_' . $to]
92-
);
100+
if (isset($response['error']) && $response['error']) {
101+
$this->_messages[] = __($response['error']);
102+
$data[$currencyFrom][$to] = null;
103+
} else {
104+
$data[$currencyFrom][$to] = $this->_numberFormat(
105+
(double)$response[$currencyFrom . '_' . $to]
106+
);
107+
}
93108
}
94109
}
95110
} finally {
@@ -137,7 +152,7 @@ private function getServiceResponse($url, $retry = 0)
137152
}
138153

139154
/**
140-
* {@inheritdoc}
155+
* @inheritdoc
141156
*/
142157
protected function _convert($currencyFrom, $currencyTo)
143158
{

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>

0 commit comments

Comments
 (0)