19 .
'&base={{CURRENCY_FROM}}&symbols={{CURRENCY_TO}}';
43 \
Magento\Directory\Model\CurrencyFactory $currencyFactory,
47 parent::__construct($currencyFactory);
48 $this->scopeConfig = $scopeConfig;
61 foreach ($defaultCurrencies as $currencyFrom) {
62 if (!isset(
$data[$currencyFrom])) {
63 $data[$currencyFrom] = [];
65 $data = $this->convertBatch(
$data, $currencyFrom, $currencies);
66 ksort(
$data[$currencyFrom]);
74 protected function _convert($currencyFrom, $currencyTo)
86 private function convertBatch(array
$data,
string $currencyFrom, array $currenciesTo): array
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);
95 $currenciesStr = implode(
',', $currenciesTo);
97 [
'{{ACCESS_KEY}}',
'{{CURRENCY_FROM}}',
'{{CURRENCY_TO}}'],
98 [$accessKey, $currencyFrom, $currenciesStr],
99 self::CURRENCY_CONVERTER_URL
106 ini_restore(
'max_execution_time');
109 if (!$this->validateResponse(
$response, $currencyFrom)) {
110 $data[$currencyFrom] = $this->makeEmptyResponse($currenciesTo);
114 foreach ($currenciesTo as $currencyTo) {
115 if ($currencyFrom == $currencyTo) {
118 if (empty(
$response[
'rates'][$currencyTo])) {
119 $this->_messages[] =
__(
'We can\'t retrieve a rate from %1 for %2.',
$url, $currencyTo);
120 $data[$currencyFrom][$currencyTo] =
null;
138 private function getServiceResponse(
string $url,
int $retry = 0): array
141 $httpClient = $this->httpClientFactory->create();
145 $jsonResponse = $httpClient->setUri(
$url)
148 'timeout' => $this->scopeConfig->getValue(
149 'currency/fixerio/timeout',
150 ScopeInterface::SCOPE_STORE
157 $response = json_decode($jsonResponse,
true);
158 }
catch (\Exception $e) {
173 private function validateResponse(array
$response,
string $baseCurrency): bool
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.'),
186 $this->_messages[] = $errorCodes[
$response[
'error'][
'code']] ??
__(
'Currency rates can\'t be retrieved.');
197 private function makeEmptyResponse(array $currenciesTo): array
199 return array_fill_keys($currenciesTo,
null);
const CURRENCY_CONVERTER_URL
__construct(\Magento\Directory\Model\CurrencyFactory $currencyFactory, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\HTTP\ZendClientFactory $httpClientFactory)
_getDefaultCurrencyCodes()
_convert($currencyFrom, $currencyTo)