Skip to content

Commit c1dda19

Browse files
author
Mastiuhin Olexandr
committed
MAGETWO-93761: [2.3] Currency conversion rate services do not work in admin panel
1 parent 05f9df7 commit c1dda19

File tree

12 files changed

+170
-780
lines changed

12 files changed

+170
-780
lines changed

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

Lines changed: 74 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@
55
*/
66
namespace Magento\Directory\Model\Currency\Import;
77

8+
use Magento\Store\Model\ScopeInterface;
9+
810
/**
911
* Currency rate import model (From http://fixer.io/)
1012
*/
11-
class FixerIo extends \Magento\Directory\Model\Currency\Import\AbstractImport
13+
class FixerIo extends AbstractImport
1214
{
1315
/**
1416
* @var string
1517
*/
16-
const CURRENCY_CONVERTER_URL = 'http://api.fixer.io/latest?base={{CURRENCY_FROM}}&symbols={{CURRENCY_TO}}';
18+
const CURRENCY_CONVERTER_URL = 'http://data.fixer.io/api/latest?access_key={{ACCESS_KEY}}'
19+
. '&base={{CURRENCY_FROM}}&symbols={{CURRENCY_TO}}';
1720

1821
/**
1922
* Http Client Factory
@@ -47,7 +50,7 @@ public function __construct(
4750
}
4851

4952
/**
50-
* {@inheritdoc}
53+
* @inheritdoc
5154
*/
5255
public function fetchRates()
5356
{
@@ -65,6 +68,13 @@ public function fetchRates()
6568
return $data;
6669
}
6770

71+
/**
72+
* @inheritdoc
73+
*/
74+
protected function _convert($currencyFrom, $currencyTo)
75+
{
76+
}
77+
6878
/**
6979
* Return currencies convert rates in batch mode
7080
*
@@ -73,11 +83,21 @@ public function fetchRates()
7383
* @param array $currenciesTo
7484
* @return array
7585
*/
76-
private function convertBatch($data, $currencyFrom, $currenciesTo)
86+
private function convertBatch(array $data, string $currencyFrom, array $currenciesTo): array
7787
{
88+
$accessKey = $this->scopeConfig->getValue('currency/fixerio/api_key', ScopeInterface::SCOPE_STORE);
89+
if (empty($accessKey)) {
90+
$this->_messages[] = __('No API Key was specified or an invalid API Key was specified.');
91+
$data[$currencyFrom] = $this->makeEmptyResponse($currenciesTo);
92+
return $data;
93+
}
94+
7895
$currenciesStr = implode(',', $currenciesTo);
79-
$url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, self::CURRENCY_CONVERTER_URL);
80-
$url = str_replace('{{CURRENCY_TO}}', $currenciesStr, $url);
96+
$url = str_replace(
97+
['{{ACCESS_KEY}}', '{{CURRENCY_FROM}}', '{{CURRENCY_TO}}'],
98+
[$accessKey, $currencyFrom, $currenciesStr],
99+
self::CURRENCY_CONVERTER_URL
100+
);
81101

82102
set_time_limit(0);
83103
try {
@@ -86,6 +106,11 @@ private function convertBatch($data, $currencyFrom, $currenciesTo)
86106
ini_restore('max_execution_time');
87107
}
88108

109+
if (!$this->validateResponse($response, $currencyFrom)) {
110+
$data[$currencyFrom] = $this->makeEmptyResponse($currenciesTo);
111+
return $data;
112+
}
113+
89114
foreach ($currenciesTo as $currencyTo) {
90115
if ($currencyFrom == $currencyTo) {
91116
$data[$currencyFrom][$currencyTo] = $this->_numberFormat(1);
@@ -110,25 +135,24 @@ private function convertBatch($data, $currencyFrom, $currenciesTo)
110135
* @param int $retry
111136
* @return array
112137
*/
113-
private function getServiceResponse($url, $retry = 0)
138+
private function getServiceResponse(string $url, int $retry = 0): array
114139
{
115140
/** @var \Magento\Framework\HTTP\ZendClient $httpClient */
116141
$httpClient = $this->httpClientFactory->create();
117142
$response = [];
118143

119144
try {
120-
$jsonResponse = $httpClient->setUri(
121-
$url
122-
)->setConfig(
123-
[
124-
'timeout' => $this->scopeConfig->getValue(
125-
'currency/fixerio/timeout',
126-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
127-
),
128-
]
129-
)->request(
130-
'GET'
131-
)->getBody();
145+
$jsonResponse = $httpClient->setUri($url)
146+
->setConfig(
147+
[
148+
'timeout' => $this->scopeConfig->getValue(
149+
'currency/fixerio/timeout',
150+
ScopeInterface::SCOPE_STORE
151+
),
152+
]
153+
)
154+
->request('GET')
155+
->getBody();
132156

133157
$response = json_decode($jsonResponse, true);
134158
} catch (\Exception $e) {
@@ -140,9 +164,38 @@ private function getServiceResponse($url, $retry = 0)
140164
}
141165

142166
/**
143-
* {@inheritdoc}
167+
* Validates rates response.
168+
*
169+
* @param array $response
170+
* @param string $baseCurrency
171+
* @return bool
144172
*/
145-
protected function _convert($currencyFrom, $currencyTo)
173+
private function validateResponse(array $response, string $baseCurrency): bool
174+
{
175+
if ($response['success']) {
176+
return true;
177+
}
178+
179+
$errorCodes = [
180+
101 => __('No API Key was specified or an invalid API Key was specified.'),
181+
102 => __('The account this API request is coming from is inactive.'),
182+
105 => __('The "%1" is not allowed as base currency for your subscription plan.', $baseCurrency),
183+
201 => __('An invalid base currency has been entered.'),
184+
];
185+
186+
$this->_messages[] = $errorCodes[$response['error']['code']] ?? __('Currency rates can\'t be retrieved.');
187+
188+
return false;
189+
}
190+
191+
/**
192+
* Creates array for provided currencies with empty rates.
193+
*
194+
* @param array $currenciesTo
195+
* @return array
196+
*/
197+
private function makeEmptyResponse(array $currenciesTo): array
146198
{
199+
return array_fill_keys($currenciesTo, null);
147200
}
148201
}

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

Lines changed: 0 additions & 92 deletions
This file was deleted.

0 commit comments

Comments
 (0)