Skip to content

Commit 5e66404

Browse files
committed
added Currency Converter Api connection feature.
1 parent 166dd61 commit 5e66404

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
namespace Magento\Directory\Model\Currency\Import;
3+
4+
class CurrencyConverterApi extends AbstractImport
5+
{
6+
/**
7+
* @var string
8+
*/
9+
const CURRENCY_CONVERTER_URL = 'http://free.currencyconverterapi.com/api/v3/convert?q={{CURRENCY_FROM}}_{{CURRENCY_TO}}&compact=ultra';
10+
11+
/**
12+
* Http Client Factory
13+
*
14+
* @var \Magento\Framework\HTTP\ZendClientFactory
15+
*/
16+
protected $httpClientFactory;
17+
18+
/**
19+
* Core scope config
20+
*
21+
* @var \Magento\Framework\App\Config\ScopeConfigInterface
22+
*/
23+
private $scopeConfig;
24+
25+
/**
26+
* Initialize dependencies
27+
*
28+
* @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
29+
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
30+
* @param \Magento\Framework\HTTP\ZendClientFactory $httpClientFactory
31+
*/
32+
public function __construct(
33+
\Magento\Directory\Model\CurrencyFactory $currencyFactory,
34+
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
35+
\Magento\Framework\HTTP\ZendClientFactory $httpClientFactory
36+
) {
37+
parent::__construct($currencyFactory);
38+
$this->scopeConfig = $scopeConfig;
39+
$this->httpClientFactory = $httpClientFactory;
40+
}
41+
42+
/**
43+
* {@inheritdoc}
44+
*/
45+
public function fetchRates()
46+
{
47+
$data = [];
48+
$currencies = $this->_getCurrencyCodes();
49+
$defaultCurrencies = $this->_getDefaultCurrencyCodes();
50+
51+
foreach ($defaultCurrencies as $currencyFrom) {
52+
if (!isset($data[$currencyFrom])) {
53+
$data[$currencyFrom] = [];
54+
}
55+
$data = $this->convertBatch($data, $currencyFrom, $currencies);
56+
ksort($data[$currencyFrom]);
57+
}
58+
return $data;
59+
}
60+
61+
/**
62+
* Return currencies convert rates in batch mode
63+
*
64+
* @param array $data
65+
* @param string $currencyFrom
66+
* @param array $currenciesTo
67+
* @return array
68+
*/
69+
private function convertBatch($data, $currencyFrom, $currenciesTo)
70+
{
71+
foreach($currenciesTo as $to) {
72+
set_time_limit(0);
73+
try {
74+
$url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, self::CURRENCY_CONVERTER_URL);
75+
$url = str_replace('{{CURRENCY_TO}}', $to, $url);
76+
$response = $this->getServiceResponse($url);
77+
if ($currencyFrom == $to) {
78+
$data[$currencyFrom][$to] = $this->_numberFormat(1);
79+
} else {
80+
if (empty($response)) {
81+
$this->_messages[] = __('We can\'t retrieve a rate from %1 for %2.', $url, $to);
82+
$data[$currencyFrom][$to] = null;
83+
} else {
84+
$data[$currencyFrom][$to] = $this->_numberFormat(
85+
(double)$response[$currencyFrom . '_' . $to]
86+
);
87+
}
88+
}
89+
} finally {
90+
ini_restore('max_execution_time');
91+
}
92+
93+
}
94+
95+
96+
return $data;
97+
}
98+
99+
/**
100+
* Get Fixer.io service response
101+
*
102+
* @param string $url
103+
* @param int $retry
104+
* @return array
105+
*/
106+
private function getServiceResponse($url, $retry = 0)
107+
{
108+
/** @var \Magento\Framework\HTTP\ZendClient $httpClient */
109+
$httpClient = $this->httpClientFactory->create();
110+
$response = [];
111+
112+
try {
113+
$jsonResponse = $httpClient->setUri(
114+
$url
115+
)->setConfig(
116+
[
117+
'timeout' => $this->scopeConfig->getValue(
118+
'currency/currencyconverterapi/timeout',
119+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
120+
),
121+
]
122+
)->request(
123+
'GET'
124+
)->getBody();
125+
126+
$response = json_decode($jsonResponse, true);
127+
} catch (\Exception $e) {
128+
if ($retry == 0) {
129+
$response = $this->getServiceResponse($url, 1);
130+
}
131+
}
132+
return $response;
133+
}
134+
135+
/**
136+
* {@inheritdoc}
137+
*/
138+
protected function _convert($currencyFrom, $currencyTo)
139+
{
140+
}
141+
}

app/code/Magento/Directory/etc/adminhtml/system.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@
5252
<label>Connection Timeout in Seconds</label>
5353
</field>
5454
</group>
55+
<group id="currencyconverterapi" translate="label" sortOrder="45" showInDefault="1" showInWebsite="0" showInStore="0">
56+
<label>Currency Converter API</label>
57+
<field id="timeout" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="0" showInStore="0">
58+
<label>Connection Timeout in Seconds</label>
59+
</field>
60+
</group>
5561
<group id="import" translate="label" type="text" sortOrder="50" showInDefault="1" showInWebsite="0" showInStore="0">
5662
<label>Scheduled Import Settings</label>
5763
<field id="enabled" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">

app/code/Magento/Directory/etc/config.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
<webservicex>
2828
<timeout>100</timeout>
2929
</webservicex>
30+
<currencyconverterapi>
31+
<timeout>100</timeout>
32+
</currencyconverterapi>
3033
<import>
3134
<enabled>0</enabled>
3235
<error_email />

app/code/Magento/Directory/etc/di.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
<item name="label" xsi:type="string" translatable="true">Fixer.io</item>
2323
<item name="class" xsi:type="string">Magento\Directory\Model\Currency\Import\FixerIo</item>
2424
</item>
25+
<item name="currencyconverterapi" xsi:type="array">
26+
<item name="label" xsi:type="string" translatable="true">Currency Converter API</item>
27+
<item name="class" xsi:type="string">Magento\Directory\Model\Currency\Import\CurrencyConverterApi</item>
28+
</item>
2529
</argument>
2630
</arguments>
2731
</type>

0 commit comments

Comments
 (0)