5
5
use Magento \Checkout \Model \Session as CheckoutSession ;
6
6
use Magento \Customer \Model \Session as CustomerSession ;
7
7
use Magento \Directory \Model \CountryFactory ;
8
+ use Magento \Framework \App \Config \Storage \WriterInterface ;
8
9
use Magento \Framework \App \Helper \AbstractHelper ;
9
10
use Magento \Framework \App \Helper \Context ;
11
+ use Magento \Framework \App \ResourceConnection ;
12
+ use Magento \Framework \DB \Adapter \AdapterInterface ;
10
13
use Magento \Framework \Locale \Resolver ;
11
14
use Magento \Quote \Model \Quote ;
12
15
use Magento \Sales \Model \Order \Item ;
17
20
use Payplug \OneySimulation ;
18
21
use Payplug \Payments \Gateway \Config \OneyWithoutFees ;
19
22
use Payplug \Payments \Logger \Logger ;
23
+ use Payplug \Payments \Model \Api \Login ;
20
24
use Payplug \Payments \Model \OneySimulation \Option ;
21
25
use Payplug \Payments \Model \OneySimulation \Result ;
22
26
use Payplug \Payments \Model \OneySimulation \Schedule ;
@@ -81,22 +85,40 @@ class Oney extends AbstractHelper
81
85
*/
82
86
private $ paymentHelper ;
83
87
88
+ /**
89
+ * @var Login
90
+ */
91
+ private $ login ;
92
+
93
+ /**
94
+ * @var WriterInterface
95
+ */
96
+ private $ writer ;
97
+
98
+ /**
99
+ * @var AdapterInterface
100
+ */
101
+ private $ resourceConnection ;
102
+
84
103
/**
85
104
* @var string
86
105
*/
87
106
private $ oneyMethod ;
88
107
89
108
/**
90
- * @param Context $context
91
- * @param Config $payplugConfig
92
- * @param StoreManagerInterface $storeManager
93
- * @param PricingHelper $pricingHelper
94
- * @param CountryFactory $countryFactory
95
- * @param Resolver $localeResolver
96
- * @param Logger $logger
97
- * @param CheckoutSession $checkoutSession
98
- * @param CustomerSession $customerSession
109
+ * @param Context $context
110
+ * @param Config $payplugConfig
111
+ * @param StoreManagerInterface $storeManager
112
+ * @param PricingHelper $pricingHelper
113
+ * @param CountryFactory $countryFactory
114
+ * @param Resolver $localeResolver
115
+ * @param Logger $logger
116
+ * @param CheckoutSession $checkoutSession
117
+ * @param CustomerSession $customerSession
99
118
* @param \Magento\Payment\Helper\Data $paymentHelper
119
+ * @param Login $login
120
+ * @param WriterInterface $writer
121
+ * @param ResourceConnection $resourceConnection
100
122
*/
101
123
public function __construct (
102
124
Context $ context ,
@@ -108,7 +130,10 @@ public function __construct(
108
130
Logger $ logger ,
109
131
CheckoutSession $ checkoutSession ,
110
132
CustomerSession $ customerSession ,
111
- \Magento \Payment \Helper \Data $ paymentHelper
133
+ \Magento \Payment \Helper \Data $ paymentHelper ,
134
+ Login $ login ,
135
+ WriterInterface $ writer ,
136
+ ResourceConnection $ resourceConnection
112
137
) {
113
138
parent ::__construct ($ context );
114
139
@@ -121,6 +146,9 @@ public function __construct(
121
146
$ this ->checkoutSession = $ checkoutSession ;
122
147
$ this ->customerSession = $ customerSession ;
123
148
$ this ->paymentHelper = $ paymentHelper ;
149
+ $ this ->login = $ login ;
150
+ $ this ->writer = $ writer ;
151
+ $ this ->resourceConnection = $ resourceConnection ->getConnection ();
124
152
}
125
153
126
154
/**
@@ -156,9 +184,76 @@ public function canDisplayOney(): bool
156
184
return false ;
157
185
}
158
186
187
+ $ apiKey = $ liveApiKey ;
188
+ $ environmentMode = $ this ->scopeConfig ->getValue (Config::CONFIG_PATH . 'environmentmode ' , ScopeInterface::SCOPE_STORE , $ storeId );
189
+ if ($ environmentMode == Config::ENVIRONMENT_TEST ) {
190
+ $ apiKey = $ testApiKey ;
191
+ }
192
+
193
+ $ storeLocale = $ this ->scopeConfig ->getValue ('general/locale/code ' , ScopeInterface::SCOPE_STORE , $ storeId );
194
+ $ localeCountry = explode ('_ ' , $ storeLocale )[1 ] ?? null ;
195
+ if ($ localeCountry !== $ this ->getMerchandCountry ($ storeId , $ apiKey )) {
196
+ return false ;
197
+ }
198
+
159
199
return true ;
160
200
}
161
201
202
+ /**
203
+ * @param int $storeId
204
+ * @param string|null $apiKey
205
+ *
206
+ * @return mixed|string
207
+ */
208
+ private function getMerchandCountry (int $ storeId , ?string $ apiKey )
209
+ {
210
+ $ savedMerchandCountry = $ this ->getMerchandCountryFromConfig ($ storeId );
211
+ if (!empty ($ savedMerchandCountry )) {
212
+ return $ savedMerchandCountry ;
213
+ }
214
+
215
+ try {
216
+ $ result = $ this ->login ->getAccount ($ apiKey );
217
+ if (!$ result ['status ' ]) {
218
+ return '' ;
219
+ }
220
+ $ country = $ result ['answer ' ]['country ' ] ?? '' ;
221
+ $ this ->writer ->save (Config::CONFIG_PATH . 'merchand_country ' , $ country , ScopeInterface::SCOPE_STORE , $ storeId );
222
+
223
+ return $ country ;
224
+ } catch (\Exception $ e ) {
225
+ $ this ->logger ->error ('Could not retrieve Payplug merchand country ' , [
226
+ 'exception ' => $ e ,
227
+ ]);
228
+
229
+ return '' ;
230
+ }
231
+ }
232
+
233
+ /**
234
+ * @param int $storeId
235
+ *
236
+ * @return mixed|string
237
+ */
238
+ private function getMerchandCountryFromConfig (int $ storeId )
239
+ {
240
+ $ savedMerchandCountry = $ this ->scopeConfig ->getValue (Config::CONFIG_PATH . 'merchand_country ' , ScopeInterface::SCOPE_STORE , $ storeId );
241
+ if (!empty ($ savedMerchandCountry )) {
242
+ return $ savedMerchandCountry ;
243
+ }
244
+
245
+ $ select = $ this ->resourceConnection ->select ()
246
+ ->from (
247
+ ['main_table ' => $ this ->resourceConnection ->getTableName ('core_config_data ' )],
248
+ 'value '
249
+ )
250
+ ->where ('main_table.scope like ? ' , ScopeInterface::SCOPE_STORE . '% ' )
251
+ ->where ('main_table.scope_id = ? ' , $ storeId )
252
+ ->where ('main_table.path = ? ' , Config::CONFIG_PATH . 'merchand_country ' );
253
+
254
+ return $ this ->resourceConnection ->fetchOne ($ select );
255
+ }
256
+
162
257
/**
163
258
* @param float $amount
164
259
*
0 commit comments