-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔃 [EngCom] Public Pull Requests - 2.3-develop
Accepted Public Pull Requests: - #17557: Replacing deprecated methods for Magento_Security module. (by @tiagosampaio) - #17548: Fixed typo in module AdminNotification (by @arnoudhgz) - #17549: Fixed typos in module CatalogRule (by @arnoudhgz) - #17550: Fixed typo 'Infomation' (by @arnoudhgz) - #17528: [Forwardport] AD-HOC feat (Profiler): Allow supplying complex profiler configuration (by @jignesh-baldha) - #17519: Added translation comment tags (by @yogeshks) - #15542: Add Currency Converter API connecting feature (by @HirokazuNishi) - #17495: [Forwardport] Magento 2.2.5: Year-to-date dropdown in Stores>Configuration>General>Reports>Dashboard #17289 (by @ronak2ram) Fixed GitHub Issues: - #15541: Cannot retrieve currency rates for TWD (reported by @HirokazuNishi) has been fixed in #15542 by @HirokazuNishi in 2.3-develop branch Related commits: 1. 5e66404 2. d682ead - #17289: Magento 2.2.5: Year-to-date dropdown in Stores>Configuration>General>Reports>Dashboard (reported by @joshuaflood) has been fixed in #17495 by @ronak2ram in 2.3-develop branch Related commits: 1. cea62ec 2. 1c70659
- Loading branch information
Showing
42 changed files
with
274 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Directory\Model\Currency\Import; | ||
|
||
class CurrencyConverterApi extends AbstractImport | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
const CURRENCY_CONVERTER_URL = 'http://free.currencyconverterapi.com/api/v3/convert?q={{CURRENCY_FROM}}_{{CURRENCY_TO}}&compact=ultra'; //@codingStandardsIgnoreLine | ||
|
||
/** | ||
* Http Client Factory | ||
* | ||
* @var \Magento\Framework\HTTP\ZendClientFactory | ||
*/ | ||
private $httpClientFactory; | ||
|
||
/** | ||
* Core scope config | ||
* | ||
* @var \Magento\Framework\App\Config\ScopeConfigInterface | ||
*/ | ||
private $scopeConfig; | ||
|
||
/** | ||
* Initialize dependencies | ||
* | ||
* @param \Magento\Directory\Model\CurrencyFactory $currencyFactory | ||
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig | ||
* @param \Magento\Framework\HTTP\ZendClientFactory $httpClientFactory | ||
*/ | ||
public function __construct( | ||
\Magento\Directory\Model\CurrencyFactory $currencyFactory, | ||
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, | ||
\Magento\Framework\HTTP\ZendClientFactory $httpClientFactory | ||
) { | ||
parent::__construct($currencyFactory); | ||
$this->scopeConfig = $scopeConfig; | ||
$this->httpClientFactory = $httpClientFactory; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function fetchRates() | ||
{ | ||
$data = []; | ||
$currencies = $this->_getCurrencyCodes(); | ||
$defaultCurrencies = $this->_getDefaultCurrencyCodes(); | ||
|
||
foreach ($defaultCurrencies as $currencyFrom) { | ||
if (!isset($data[$currencyFrom])) { | ||
$data[$currencyFrom] = []; | ||
} | ||
$data = $this->convertBatch($data, $currencyFrom, $currencies); | ||
ksort($data[$currencyFrom]); | ||
} | ||
return $data; | ||
} | ||
|
||
/** | ||
* Return currencies convert rates in batch mode | ||
* | ||
* @param array $data | ||
* @param string $currencyFrom | ||
* @param array $currenciesTo | ||
* @return array | ||
*/ | ||
private function convertBatch($data, $currencyFrom, $currenciesTo) | ||
{ | ||
foreach ($currenciesTo as $to) { | ||
set_time_limit(0); | ||
try { | ||
$url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, self::CURRENCY_CONVERTER_URL); | ||
$url = str_replace('{{CURRENCY_TO}}', $to, $url); | ||
$response = $this->getServiceResponse($url); | ||
if ($currencyFrom == $to) { | ||
$data[$currencyFrom][$to] = $this->_numberFormat(1); | ||
} else { | ||
if (empty($response)) { | ||
$this->_messages[] = __('We can\'t retrieve a rate from %1 for %2.', $url, $to); | ||
$data[$currencyFrom][$to] = null; | ||
} else { | ||
$data[$currencyFrom][$to] = $this->_numberFormat( | ||
(double)$response[$currencyFrom . '_' . $to] | ||
); | ||
} | ||
} | ||
} finally { | ||
ini_restore('max_execution_time'); | ||
} | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* Get Fixer.io service response | ||
* | ||
* @param string $url | ||
* @param int $retry | ||
* @return array | ||
*/ | ||
private function getServiceResponse($url, $retry = 0) | ||
{ | ||
/** @var \Magento\Framework\HTTP\ZendClient $httpClient */ | ||
$httpClient = $this->httpClientFactory->create(); | ||
$response = []; | ||
|
||
try { | ||
$jsonResponse = $httpClient->setUri( | ||
$url | ||
)->setConfig( | ||
[ | ||
'timeout' => $this->scopeConfig->getValue( | ||
'currency/currencyconverterapi/timeout', | ||
\Magento\Store\Model\ScopeInterface::SCOPE_STORE | ||
), | ||
] | ||
)->request( | ||
'GET' | ||
)->getBody(); | ||
|
||
$response = json_decode($jsonResponse, true); | ||
} catch (\Exception $e) { | ||
if ($retry == 0) { | ||
$response = $this->getServiceResponse($url, 1); | ||
} | ||
} | ||
return $response; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function _convert($currencyFrom, $currencyTo) | ||
{ | ||
return 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.