|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Directory\Model\Currency\Import; |
| 9 | + |
| 10 | +use Exception; |
| 11 | +use Laminas\Http\Request; |
| 12 | +use Magento\Directory\Model\CurrencyFactory; |
| 13 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 14 | +use Magento\Framework\HTTP\LaminasClientFactory as HttpClientFactory; |
| 15 | +use Magento\Store\Model\ScopeInterface; |
| 16 | +use Magento\Framework\HTTP\LaminasClient; |
| 17 | + |
| 18 | +/** |
| 19 | + * Currency rate import model (https://apilayer.com/marketplace/fixer-api) |
| 20 | + */ |
| 21 | +class FixerIoApiLayer extends AbstractImport |
| 22 | +{ |
| 23 | + private const CURRENCY_CONVERTER_HOST = 'https://api.apilayer.com'; |
| 24 | + private const CURRENCY_CONVERTER_URL_PATH = '/fixer/latest?' |
| 25 | + . 'apikey={{ACCESS_KEY}}&base={{CURRENCY_FROM}}&symbols={{CURRENCY_TO}}'; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var HttpClientFactory |
| 29 | + */ |
| 30 | + private $httpClientFactory; |
| 31 | + |
| 32 | + /** |
| 33 | + * Core scope config |
| 34 | + * |
| 35 | + * @var ScopeConfigInterface |
| 36 | + */ |
| 37 | + private $scopeConfig; |
| 38 | + |
| 39 | + /** |
| 40 | + * Initialize dependencies |
| 41 | + * |
| 42 | + * @param CurrencyFactory $currencyFactory |
| 43 | + * @param ScopeConfigInterface $scopeConfig |
| 44 | + * @param HttpClientFactory $httpClientFactory |
| 45 | + */ |
| 46 | + public function __construct( |
| 47 | + CurrencyFactory $currencyFactory, |
| 48 | + ScopeConfigInterface $scopeConfig, |
| 49 | + HttpClientFactory $httpClientFactory |
| 50 | + ) { |
| 51 | + parent::__construct($currencyFactory); |
| 52 | + $this->scopeConfig = $scopeConfig; |
| 53 | + $this->httpClientFactory = $httpClientFactory; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @inheritdoc |
| 58 | + */ |
| 59 | + public function fetchRates() |
| 60 | + { |
| 61 | + $data = []; |
| 62 | + $currencies = $this->_getCurrencyCodes(); |
| 63 | + $defaultCurrencies = $this->_getDefaultCurrencyCodes(); |
| 64 | + |
| 65 | + foreach ($defaultCurrencies as $currencyFrom) { |
| 66 | + if (!isset($data[$currencyFrom])) { |
| 67 | + $data[$currencyFrom] = []; |
| 68 | + } |
| 69 | + $data = $this->convertBatch($data, $currencyFrom, $currencies); |
| 70 | + ksort($data[$currencyFrom]); |
| 71 | + } |
| 72 | + return $data; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Return currencies convert rates in batch mode |
| 77 | + * |
| 78 | + * @param array $data |
| 79 | + * @param string $currencyFrom |
| 80 | + * @param array $currenciesTo |
| 81 | + * @return array |
| 82 | + */ |
| 83 | + private function convertBatch(array $data, string $currencyFrom, array $currenciesTo): array |
| 84 | + { |
| 85 | + $accessKey = $this->scopeConfig->getValue('currency/apilayer/api_key', ScopeInterface::SCOPE_STORE); |
| 86 | + if (empty($accessKey)) { |
| 87 | + $this->_messages[] = __('No API Key was specified or an invalid API Key was specified.'); |
| 88 | + $data[$currencyFrom] = $this->makeEmptyResponse($currenciesTo); |
| 89 | + return $data; |
| 90 | + } |
| 91 | + |
| 92 | + $currenciesStr = implode(',', $currenciesTo); |
| 93 | + $url = str_replace( |
| 94 | + ['{{ACCESS_KEY}}', '{{CURRENCY_FROM}}', '{{CURRENCY_TO}}'], |
| 95 | + [$accessKey, $currencyFrom, $currenciesStr], |
| 96 | + self::CURRENCY_CONVERTER_HOST . self::CURRENCY_CONVERTER_URL_PATH |
| 97 | + ); |
| 98 | + // phpcs:ignore Magento2.Functions.DiscouragedFunction |
| 99 | + set_time_limit(0); |
| 100 | + try { |
| 101 | + $response = $this->getServiceResponse($url); |
| 102 | + } finally { |
| 103 | + ini_restore('max_execution_time'); |
| 104 | + } |
| 105 | + |
| 106 | + if (!$this->validateResponse($response, $currencyFrom)) { |
| 107 | + $data[$currencyFrom] = $this->makeEmptyResponse($currenciesTo); |
| 108 | + return $data; |
| 109 | + } |
| 110 | + |
| 111 | + foreach ($currenciesTo as $currencyTo) { |
| 112 | + if ($currencyFrom == $currencyTo) { |
| 113 | + $data[$currencyFrom][$currencyTo] = $this->_numberFormat(1); |
| 114 | + } else { |
| 115 | + if (empty($response['rates'][$currencyTo])) { |
| 116 | + $message = 'We can\'t retrieve a rate from %1 for %2.'; |
| 117 | + $this->_messages[] = __($message, self::CURRENCY_CONVERTER_HOST, $currencyTo); |
| 118 | + $data[$currencyFrom][$currencyTo] = null; |
| 119 | + } else { |
| 120 | + $data[$currencyFrom][$currencyTo] = $this->_numberFormat( |
| 121 | + (double)$response['rates'][$currencyTo] |
| 122 | + ); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + return $data; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @inheritdoc |
| 131 | + */ |
| 132 | + protected function _convert($currencyFrom, $currencyTo) |
| 133 | + { |
| 134 | + return 1; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Get apilayer.com service response |
| 139 | + * |
| 140 | + * @param string $url |
| 141 | + * @param int $retry |
| 142 | + * @return array |
| 143 | + */ |
| 144 | + private function getServiceResponse(string $url, int $retry = 0): array |
| 145 | + { |
| 146 | + /** @var LaminasClient $httpClient */ |
| 147 | + $httpClient = $this->httpClientFactory->create(); |
| 148 | + $response = []; |
| 149 | + |
| 150 | + try { |
| 151 | + $httpClient->setUri($url); |
| 152 | + $httpClient->setOptions( |
| 153 | + [ |
| 154 | + 'timeout' => $this->scopeConfig->getValue( |
| 155 | + 'currency/apilayer/timeout', |
| 156 | + ScopeInterface::SCOPE_STORE |
| 157 | + ), |
| 158 | + ] |
| 159 | + ); |
| 160 | + $httpClient->setMethod(Request::METHOD_GET); |
| 161 | + $jsonResponse = $httpClient->send()->getBody(); |
| 162 | + |
| 163 | + $response = json_decode($jsonResponse, true); |
| 164 | + } catch (Exception $e) { |
| 165 | + if ($retry == 0) { |
| 166 | + $response = $this->getServiceResponse($url, 1); |
| 167 | + } |
| 168 | + } |
| 169 | + return $response; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Creates array for provided currencies with empty rates. |
| 174 | + * |
| 175 | + * @param array $currenciesTo |
| 176 | + * @return array |
| 177 | + */ |
| 178 | + private function makeEmptyResponse(array $currenciesTo): array |
| 179 | + { |
| 180 | + return array_fill_keys($currenciesTo, null); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Validates rates response. |
| 185 | + * |
| 186 | + * @param array $response |
| 187 | + * @param string $baseCurrency |
| 188 | + * @return bool |
| 189 | + */ |
| 190 | + private function validateResponse(array $response, string $baseCurrency): bool |
| 191 | + { |
| 192 | + if ($response['success']) { |
| 193 | + return true; |
| 194 | + } |
| 195 | + |
| 196 | + $errorCodes = [ |
| 197 | + 101 => __('No API Key was specified or an invalid API Key was specified.'), |
| 198 | + 102 => __('The account this API request is coming from is inactive.'), |
| 199 | + 105 => __('The "%1" is not allowed as base currency for your subscription plan.', $baseCurrency), |
| 200 | + 201 => __('An invalid base currency has been entered.'), |
| 201 | + ]; |
| 202 | + |
| 203 | + $this->_messages[] = $errorCodes[$response['error']['code']] ?? __('Currency rates can\'t be retrieved.'); |
| 204 | + |
| 205 | + return false; |
| 206 | + } |
| 207 | +} |
0 commit comments