1 | <?php |
||
2 | |||
3 | namespace Pagantis\Pagantis\Observer; |
||
4 | |||
5 | use Magento\Framework\Event\ObserverInterface; |
||
6 | use Pagantis\Pagantis\Model\Ui\ConfigProvider; |
||
7 | |||
8 | /** |
||
9 | * Class PaymentMethodAvailable |
||
10 | * @package Pagantis\Pagantis\Observer |
||
11 | */ |
||
12 | class PaymentMethodAvailable implements ObserverInterface |
||
13 | { |
||
14 | /** |
||
15 | * @param \Magento\Framework\Event\Observer $observer |
||
16 | */ |
||
17 | public function execute(\Magento\Framework\Event\Observer $observer) |
||
18 | { |
||
19 | try { |
||
20 | if ($observer->getEvent()->getMethodInstance()->getCode()==ConfigProvider::CODE) { |
||
21 | $checkResult = $observer->getEvent()->getResult(); |
||
22 | $totalPrice = (string) floor($observer->getEvent()->getQuote()->getGrandTotal()); |
||
23 | $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); |
||
24 | $config = $objectManager->create('Pagantis\Pagantis\Helper\Config')->getConfig(); |
||
25 | $extraConfig = $objectManager->create('Pagantis\Pagantis\Helper\ExtraConfig')->getExtraConfig(); |
||
26 | $resolver = $objectManager->create('Magento\Framework\Locale\Resolver'); |
||
27 | $locale = strstr($resolver->getLocale(), '_', true); |
||
28 | $allowedCountries = unserialize($extraConfig['PAGANTIS_ALLOWED_COUNTRIES']); |
||
29 | $availableCountry = (in_array(strtolower($locale), $allowedCountries)); |
||
30 | $maxAmount = $extraConfig['PAGANTIS_DISPLAY_MAX_AMOUNT']; |
||
31 | $minAmount = $extraConfig['PAGANTIS_DISPLAY_MIN_AMOUNT']; |
||
32 | $validAmount = ($totalPrice>=$minAmount && ($totalPrice<=$maxAmount||$maxAmount=='0')); |
||
33 | $disabledPg = (!isset($config['pagantis_public_key']) || $config['pagantis_public_key'] == '' || |
||
34 | !isset($config['pagantis_private_key']) || $config['pagantis_private_key'] == '' || |
||
35 | !$availableCountry || !$validAmount || $config['active_12x']!=='1' |
||
36 | || $config['active']!=='1' ); |
||
37 | if ($disabledPg) { |
||
38 | $checkResult->setData('is_available', false); |
||
39 | } else { |
||
40 | $checkResult->setData('is_available', true); |
||
41 | } |
||
42 | } |
||
43 | |||
44 | if ($observer->getEvent()->getMethodInstance()->getCode()==ConfigProvider::CODE4X) { |
||
45 | $checkResult = $observer->getEvent()->getResult(); |
||
46 | $totalPrice = (string) floor($observer->getEvent()->getQuote()->getGrandTotal()); |
||
47 | $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); |
||
48 | $config = $objectManager->create('Pagantis\Pagantis\Helper\Config')->getConfig(); |
||
49 | $extraConfig = $objectManager->create('Pagantis\Pagantis\Helper\ExtraConfig')->getExtraConfig(); |
||
50 | $resolver = $objectManager->create('Magento\Framework\Locale\Resolver'); |
||
51 | $locale = strstr($resolver->getLocale(), '_', true); |
||
52 | $allowedCountries = unserialize($extraConfig['PAGANTIS_ALLOWED_COUNTRIES']); |
||
53 | $availableCountry = (in_array(strtolower($locale), $allowedCountries)); |
||
54 | $maxAmount = $extraConfig['PAGANTIS_DISPLAY_MAX_AMOUNT_4x']; |
||
55 | $minAmount = $extraConfig['PAGANTIS_DISPLAY_MIN_AMOUNT_4x']; |
||
56 | $validAmount = ($totalPrice>=$minAmount && ($totalPrice<=$maxAmount||$maxAmount=='0')); |
||
57 | $disabledPg4x = (!isset($config['pagantis_public_key_4x']) || $config['pagantis_public_key_4x']=='' || |
||
58 | !isset($config['pagantis_private_key_4x']) || $config['pagantis_private_key_4x']=='' || |
||
59 | !$availableCountry || !$validAmount || $config['active_4x']!=='1' |
||
60 | || $config['active']!=='1' ); |
||
61 | if ($disabledPg4x) { |
||
62 | $checkResult->setData('is_available', false); |
||
63 | } else { |
||
64 | $checkResult->setData('is_available', true); |
||
65 | } |
||
66 | } |
||
67 | } catch (\Exception $e) { |
||
68 | die($e->getMessage()); |
||
0 ignored issues
–
show
|
|||
69 | } |
||
70 | } |
||
71 | } |
||
72 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.