7
7
8
8
namespace Magento \Directory \Model \Currency \Import ;
9
9
10
+ /**
11
+ * Currency rate import model (From http://free.currencyconverterapi.com/)
12
+ *
13
+ * Class \Magento\Directory\Model\Currency\Import\CurrencyConverterApi
14
+ */
10
15
class CurrencyConverterApi extends AbstractImport
11
16
{
12
17
/**
@@ -46,7 +51,7 @@ public function __construct(
46
51
}
47
52
48
53
/**
49
- * { @inheritdoc}
54
+ * @inheritdoc
50
55
*/
51
56
public function fetchRates ()
52
57
{
@@ -75,10 +80,15 @@ public function fetchRates()
75
80
private function convertBatch ($ data , $ currencyFrom , $ currenciesTo )
76
81
{
77
82
foreach ($ currenciesTo as $ to ) {
83
+ //phpcs:ignore Magento2.Functions.DiscouragedFunction
78
84
set_time_limit (0 );
79
85
try {
80
86
$ url = str_replace ('{{CURRENCY_FROM}} ' , $ currencyFrom , self ::CURRENCY_CONVERTER_URL );
81
87
$ url = str_replace ('{{CURRENCY_TO}} ' , $ to , $ url );
88
+ $ url = $ url . '&apiKey= ' . $ this ->scopeConfig ->getValue (
89
+ 'currency/currencyconverterapi/api_key ' ,
90
+ \Magento \Store \Model \ScopeInterface::SCOPE_STORE
91
+ );
82
92
$ response = $ this ->getServiceResponse ($ url );
83
93
if ($ currencyFrom == $ to ) {
84
94
$ data [$ currencyFrom ][$ to ] = $ this ->_numberFormat (1 );
@@ -87,9 +97,14 @@ private function convertBatch($data, $currencyFrom, $currenciesTo)
87
97
$ this ->_messages [] = __ ('We can \'t retrieve a rate from %1 for %2. ' , $ url , $ to );
88
98
$ data [$ currencyFrom ][$ to ] = null ;
89
99
} else {
90
- $ data [$ currencyFrom ][$ to ] = $ this ->_numberFormat (
91
- (double )$ response [$ currencyFrom . '_ ' . $ to ]
92
- );
100
+ if (isset ($ response ['error ' ]) && $ response ['error ' ]) {
101
+ $ this ->_messages [] = __ ($ response ['error ' ]);
102
+ $ data [$ currencyFrom ][$ to ] = null ;
103
+ } else {
104
+ $ data [$ currencyFrom ][$ to ] = $ this ->_numberFormat (
105
+ (double )$ response [$ currencyFrom . '_ ' . $ to ]
106
+ );
107
+ }
93
108
}
94
109
}
95
110
} finally {
@@ -137,7 +152,7 @@ private function getServiceResponse($url, $retry = 0)
137
152
}
138
153
139
154
/**
140
- * { @inheritdoc}
155
+ * @inheritdoc
141
156
*/
142
157
protected function _convert ($ currencyFrom , $ currencyTo )
143
158
{
0 commit comments