Skip to content

Resolve "Currency Converter API" can not get Currency Rate #24008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@

namespace Magento\Directory\Model\Currency\Import;

/**
* Currency rate import model (From http://free.currencyconverterapi.com/)
*
* Class \Magento\Directory\Model\Currency\Import\CurrencyConverterApi
*/
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
const CURRENCY_CONVERTER_URL = 'http://free.currencyconverterapi.com/api/v3/convert?q={{CURRENCY_FROM}}_{{CURRENCY_TO}}&compact=ultra&apiKey={{API_KEY}}'; //@codingStandardsIgnoreLine

/**
* Http Client Factory
Expand Down Expand Up @@ -46,7 +51,7 @@ public function __construct(
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function fetchRates()
{
Expand Down Expand Up @@ -74,23 +79,25 @@ public function fetchRates()
*/
private function convertBatch($data, $currencyFrom, $currenciesTo)
{
$apiKey = $this->scopeConfig->getValue(
'currency/currencyconverterapi/api_key',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
if (!$apiKey) {
$this->_messages[] = __('No API Key was specified.');
return $data;
}
foreach ($currenciesTo as $to) {
//phpcs:ignore Magento2.Functions.DiscouragedFunction
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);
$url = str_replace('{{API_KEY}}', $apiKey, $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]
);
}
$data[$currencyFrom][$to] = $this->getCurrencyRate($currencyFrom, $to, $url);
}
} finally {
ini_restore('max_execution_time');
Expand All @@ -100,6 +107,36 @@ private function convertBatch($data, $currencyFrom, $currenciesTo)
return $data;
}

/**
* Get currency rate from api
*
* @param string $currencyFrom
* @param string $to
* @param string $url
* @return double
*/
private function getCurrencyRate($currencyFrom, $to, $url)
{
$rate = null;
$response = $this->getServiceResponse($url);
if (empty($response)) {
$this->_messages[] = __('We can\'t retrieve a rate from %1 for %2.', $url, $to);
$rate = null;
} else {
if (isset($response['error']) && $response['error']) {
if (!in_array($response['error'], $this->_messages)) {
$this->_messages[] = $response['error'];
}
$rate = null;
} else {
$rate = $this->_numberFormat(
(double)$response[$currencyFrom . '_' . $to]
);
}
}
return $rate;
}

/**
* Get Fixer.io service response
*
Expand Down Expand Up @@ -137,7 +174,7 @@ private function getServiceResponse($url, $retry = 0)
}

/**
* {@inheritdoc}
* @inheritdoc
*/
protected function _convert($currencyFrom, $currencyTo)
{
Expand Down
7 changes: 6 additions & 1 deletion app/code/Magento/Directory/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@
</group>
<group id="currencyconverterapi" translate="label" sortOrder="45" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Currency Converter API</label>
<field id="timeout" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="0" showInStore="0">
<field id="api_key" translate="label" type="obscure" sortOrder="0" showInDefault="1" showInWebsite="0" showInStore="0">
<label>API Key</label>
<config_path>currency/currencyconverterapi/api_key</config_path>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
</field>
<field id="timeout" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Connection Timeout in Seconds</label>
</field>
</group>
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Directory/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
</fixerio>
<currencyconverterapi>
<timeout>100</timeout>
<api_key backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
</currencyconverterapi>
<import>
<enabled>0</enabled>
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Directory/i18n/en_US.csv
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ Service,Service
"The """%1"" is not allowed as base currency for your subscription plan.","The """%1"" is not allowed as base currency for your subscription plan."
"An invalid base currency has been entered.","An invalid base currency has been entered."
"Currency rates can't be retrieved.","Currency rates can't be retrieved."
"No API Key was specified.","No API Key was specified."