Skip to content

Commit 0baac34

Browse files
authored
Merge pull request #120 from payplug/qa
Release 1.20.0
2 parents 1454de4 + be8ad28 commit 0baac34

File tree

4 files changed

+121
-12
lines changed

4 files changed

+121
-12
lines changed

Helper/Config.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Config extends AbstractHelper
2020
const PAYMENT_PAGE_REDIRECT = 'redirect';
2121
const PAYMENT_PAGE_EMBEDDED = 'embedded';
2222

23-
const MODULE_VERSION = '1.19.0';
23+
const MODULE_VERSION = '1.20.0';
2424

2525
/**
2626
* @var WriterInterface
@@ -263,6 +263,7 @@ public function clearConfig()
263263
'payplug_payments/general/can_create_installment_plan',
264264
'payplug_payments/general/can_create_deferred_payment',
265265
'payplug_payments/general/can_use_oney',
266+
'payplug_payments/general/merchand_country',
266267
// Payplug payment Standard configuration
267268
'payment/payplug_payments_standard/active',
268269
'payment/payplug_payments_standard/title',
@@ -301,6 +302,14 @@ public function clearConfig()
301302
'payment/payplug_payments_oney/allowspecific',
302303
'payment/payplug_payments_oney/specificcountry',
303304
'payment/payplug_payments_oney/sort_order',
305+
// Payplug payment Oney without feexs configuration
306+
'payment/payplug_payments_oney_without_fees/active',
307+
'payment/payplug_payments_oney_without_fees/title',
308+
'payment/payplug_payments_oney_without_fees/processing_order_status',
309+
'payment/payplug_payments_oney_without_fees/canceled_order_status',
310+
'payment/payplug_payments_oney_without_fees/allowspecific',
311+
'payment/payplug_payments_oney_without_fees/specificcountry',
312+
'payment/payplug_payments_oney_without_fees/sort_order',
304313
];
305314

306315
foreach ($keys as $key) {

Helper/Oney.php

Lines changed: 105 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
use Magento\Checkout\Model\Session as CheckoutSession;
66
use Magento\Customer\Model\Session as CustomerSession;
77
use Magento\Directory\Model\CountryFactory;
8+
use Magento\Framework\App\Config\Storage\WriterInterface;
89
use Magento\Framework\App\Helper\AbstractHelper;
910
use Magento\Framework\App\Helper\Context;
11+
use Magento\Framework\App\ResourceConnection;
12+
use Magento\Framework\DB\Adapter\AdapterInterface;
1013
use Magento\Framework\Locale\Resolver;
1114
use Magento\Quote\Model\Quote;
1215
use Magento\Sales\Model\Order\Item;
@@ -17,6 +20,7 @@
1720
use Payplug\OneySimulation;
1821
use Payplug\Payments\Gateway\Config\OneyWithoutFees;
1922
use Payplug\Payments\Logger\Logger;
23+
use Payplug\Payments\Model\Api\Login;
2024
use Payplug\Payments\Model\OneySimulation\Option;
2125
use Payplug\Payments\Model\OneySimulation\Result;
2226
use Payplug\Payments\Model\OneySimulation\Schedule;
@@ -81,22 +85,40 @@ class Oney extends AbstractHelper
8185
*/
8286
private $paymentHelper;
8387

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+
84103
/**
85104
* @var string
86105
*/
87106
private $oneyMethod;
88107

89108
/**
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
99118
* @param \Magento\Payment\Helper\Data $paymentHelper
119+
* @param Login $login
120+
* @param WriterInterface $writer
121+
* @param ResourceConnection $resourceConnection
100122
*/
101123
public function __construct(
102124
Context $context,
@@ -108,7 +130,10 @@ public function __construct(
108130
Logger $logger,
109131
CheckoutSession $checkoutSession,
110132
CustomerSession $customerSession,
111-
\Magento\Payment\Helper\Data $paymentHelper
133+
\Magento\Payment\Helper\Data $paymentHelper,
134+
Login $login,
135+
WriterInterface $writer,
136+
ResourceConnection $resourceConnection
112137
) {
113138
parent::__construct($context);
114139

@@ -121,6 +146,9 @@ public function __construct(
121146
$this->checkoutSession = $checkoutSession;
122147
$this->customerSession = $customerSession;
123148
$this->paymentHelper = $paymentHelper;
149+
$this->login = $login;
150+
$this->writer = $writer;
151+
$this->resourceConnection = $resourceConnection->getConnection();
124152
}
125153

126154
/**
@@ -156,9 +184,76 @@ public function canDisplayOney(): bool
156184
return false;
157185
}
158186

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+
159199
return true;
160200
}
161201

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+
162257
/**
163258
* @param float $amount
164259
*

Observer/PaymentConfigObserver.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ private function treatAccountResponse($jsonAnswer)
578578
'oney_countries' => $this->getConfig('oney_countries'),
579579
'oney_min_amounts' => $this->getConfig('oney_min_amounts'),
580580
'oney_max_amounts' => $this->getConfig('oney_max_amounts'),
581+
'merchand_country' => $this->getConfig('merchand_country'),
581582
];
582583
if (isset($jsonAnswer['configuration'])) {
583584
if (!empty($jsonAnswer['configuration']['currencies'])) {
@@ -606,6 +607,9 @@ private function treatAccountResponse($jsonAnswer)
606607
$configuration['oney_max_amounts'] = $this->processAmounts($jsonAnswer['configuration']['oney']['max_amounts']);
607608
}
608609
}
610+
if (!empty($jsonAnswer['country'])) {
611+
$configuration['merchand_country'] = $jsonAnswer['country'];
612+
}
609613
}
610614

611615
$currencies = implode(';', $configuration['currencies']);
@@ -616,6 +620,7 @@ private function treatAccountResponse($jsonAnswer)
616620
$this->saveConfig('oney_min_amounts', $configuration['oney_min_amounts']);
617621
$this->saveConfig('oney_max_amounts', $configuration['oney_max_amounts']);
618622
$this->saveConfig('company_id', $id);
623+
$this->saveConfig('merchand_country', $configuration['merchand_country']);
619624

620625
$permissions = [
621626
'use_live_mode',

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "payplug/payplug-magento2",
33
"description": "Payplug Payments module for Magento 2",
44
"type": "magento2-module",
5-
"version": "1.19.0",
5+
"version": "1.20.0",
66
"license": [
77
"OSL-3.0"
88
],

0 commit comments

Comments
 (0)