5
5
*/
6
6
namespace Magento \Directory \Model \Currency \Import ;
7
7
8
+ use Magento \Store \Model \ScopeInterface ;
9
+
8
10
/**
9
11
* Currency rate import model (From http://fixer.io/)
10
12
*/
11
- class FixerIo extends \ Magento \ Directory \ Model \ Currency \ Import \ AbstractImport
13
+ class FixerIo extends AbstractImport
12
14
{
13
15
/**
14
16
* @var string
15
17
*/
16
- const CURRENCY_CONVERTER_URL = 'http://api.fixer.io/latest?base={{CURRENCY_FROM}}&symbols={{CURRENCY_TO}} ' ;
18
+ const CURRENCY_CONVERTER_URL = 'http://data.fixer.io/api/latest?access_key={{ACCESS_KEY}} '
19
+ . '&base={{CURRENCY_FROM}}&symbols={{CURRENCY_TO}} ' ;
17
20
18
21
/**
19
22
* Http Client Factory
@@ -47,7 +50,7 @@ public function __construct(
47
50
}
48
51
49
52
/**
50
- * { @inheritdoc}
53
+ * @inheritdoc
51
54
*/
52
55
public function fetchRates ()
53
56
{
@@ -65,6 +68,13 @@ public function fetchRates()
65
68
return $ data ;
66
69
}
67
70
71
+ /**
72
+ * @inheritdoc
73
+ */
74
+ protected function _convert ($ currencyFrom , $ currencyTo )
75
+ {
76
+ }
77
+
68
78
/**
69
79
* Return currencies convert rates in batch mode
70
80
*
@@ -73,11 +83,21 @@ public function fetchRates()
73
83
* @param array $currenciesTo
74
84
* @return array
75
85
*/
76
- private function convertBatch ($ data , $ currencyFrom , $ currenciesTo )
86
+ private function convertBatch (array $ data , string $ currencyFrom , array $ currenciesTo ): array
77
87
{
88
+ $ accessKey = $ this ->scopeConfig ->getValue ('currency/fixerio/api_key ' , ScopeInterface::SCOPE_STORE );
89
+ if (empty ($ accessKey )) {
90
+ $ this ->_messages [] = __ ('No API Key was specified or an invalid API Key was specified. ' );
91
+ $ data [$ currencyFrom ] = $ this ->makeEmptyResponse ($ currenciesTo );
92
+ return $ data ;
93
+ }
94
+
78
95
$ currenciesStr = implode (', ' , $ currenciesTo );
79
- $ url = str_replace ('{{CURRENCY_FROM}} ' , $ currencyFrom , self ::CURRENCY_CONVERTER_URL );
80
- $ url = str_replace ('{{CURRENCY_TO}} ' , $ currenciesStr , $ url );
96
+ $ url = str_replace (
97
+ ['{{ACCESS_KEY}} ' , '{{CURRENCY_FROM}} ' , '{{CURRENCY_TO}} ' ],
98
+ [$ accessKey , $ currencyFrom , $ currenciesStr ],
99
+ self ::CURRENCY_CONVERTER_URL
100
+ );
81
101
82
102
set_time_limit (0 );
83
103
try {
@@ -86,6 +106,11 @@ private function convertBatch($data, $currencyFrom, $currenciesTo)
86
106
ini_restore ('max_execution_time ' );
87
107
}
88
108
109
+ if (!$ this ->validateResponse ($ response , $ currencyFrom )) {
110
+ $ data [$ currencyFrom ] = $ this ->makeEmptyResponse ($ currenciesTo );
111
+ return $ data ;
112
+ }
113
+
89
114
foreach ($ currenciesTo as $ currencyTo ) {
90
115
if ($ currencyFrom == $ currencyTo ) {
91
116
$ data [$ currencyFrom ][$ currencyTo ] = $ this ->_numberFormat (1 );
@@ -110,25 +135,24 @@ private function convertBatch($data, $currencyFrom, $currenciesTo)
110
135
* @param int $retry
111
136
* @return array
112
137
*/
113
- private function getServiceResponse ($ url , $ retry = 0 )
138
+ private function getServiceResponse (string $ url , int $ retry = 0 ): array
114
139
{
115
140
/** @var \Magento\Framework\HTTP\ZendClient $httpClient */
116
141
$ httpClient = $ this ->httpClientFactory ->create ();
117
142
$ response = [];
118
143
119
144
try {
120
- $ jsonResponse = $ httpClient ->setUri (
121
- $ url
122
- )->setConfig (
123
- [
124
- 'timeout ' => $ this ->scopeConfig ->getValue (
125
- 'currency/fixerio/timeout ' ,
126
- \Magento \Store \Model \ScopeInterface::SCOPE_STORE
127
- ),
128
- ]
129
- )->request (
130
- 'GET '
131
- )->getBody ();
145
+ $ jsonResponse = $ httpClient ->setUri ($ url )
146
+ ->setConfig (
147
+ [
148
+ 'timeout ' => $ this ->scopeConfig ->getValue (
149
+ 'currency/fixerio/timeout ' ,
150
+ ScopeInterface::SCOPE_STORE
151
+ ),
152
+ ]
153
+ )
154
+ ->request ('GET ' )
155
+ ->getBody ();
132
156
133
157
$ response = json_decode ($ jsonResponse , true );
134
158
} catch (\Exception $ e ) {
@@ -140,9 +164,38 @@ private function getServiceResponse($url, $retry = 0)
140
164
}
141
165
142
166
/**
143
- * {@inheritdoc}
167
+ * Validates rates response.
168
+ *
169
+ * @param array $response
170
+ * @param string $baseCurrency
171
+ * @return bool
144
172
*/
145
- protected function _convert ($ currencyFrom , $ currencyTo )
173
+ private function validateResponse (array $ response , string $ baseCurrency ): bool
174
+ {
175
+ if ($ response ['success ' ]) {
176
+ return true ;
177
+ }
178
+
179
+ $ errorCodes = [
180
+ 101 => __ ('No API Key was specified or an invalid API Key was specified. ' ),
181
+ 102 => __ ('The account this API request is coming from is inactive. ' ),
182
+ 105 => __ ('The "%1" is not allowed as base currency for your subscription plan. ' , $ baseCurrency ),
183
+ 201 => __ ('An invalid base currency has been entered. ' ),
184
+ ];
185
+
186
+ $ this ->_messages [] = $ errorCodes [$ response ['error ' ]['code ' ]] ?? __ ('Currency rates can \'t be retrieved. ' );
187
+
188
+ return false ;
189
+ }
190
+
191
+ /**
192
+ * Creates array for provided currencies with empty rates.
193
+ *
194
+ * @param array $currenciesTo
195
+ * @return array
196
+ */
197
+ private function makeEmptyResponse (array $ currenciesTo ): array
146
198
{
199
+ return array_fill_keys ($ currenciesTo , null );
147
200
}
148
201
}
0 commit comments