10
10
11
11
use Hyva \Checkout \Model \CheckoutInformation \Luma ;
12
12
use Hyva \Checkout \Model \ConfigData \HyvaThemes \SystemConfigGeneral as HyvaCheckoutConfig ;
13
- use Magento \Framework \App \Config \ScopeConfigInterface ;
14
13
use Magento \Framework \Event \Observer ;
15
14
use Magento \Framework \Event \ObserverInterface ;
16
15
use Magento \Framework \Exception \State \InvalidTransitionException ;
16
+ use Magento \Payment \Api \Data \PaymentMethodInterface ;
17
+ use Magento \Payment \Api \PaymentMethodListInterface ;
17
18
use Magento \Quote \Api \Data \PaymentInterfaceFactory ;
18
19
use Magento \Quote \Api \PaymentMethodManagementInterface ;
19
20
use Magento \Quote \Model \Quote ;
20
- use Magento \Store \Model \ScopeInterface ;
21
- use Magento \Store \Model \StoreManagerInterface ;
22
21
use Mollie \Payment \Config ;
23
22
24
23
class SetDefaultSelectedPaymentMethod implements ObserverInterface
25
24
{
26
25
private PaymentInterfaceFactory $ paymentFactory ;
27
26
private Config $ config ;
28
- private StoreManagerInterface $ storeManager ;
29
- private ScopeConfigInterface $ scopeConfig ;
30
27
private HyvaCheckoutConfig $ hyvaCheckoutConfig ;
31
28
private PaymentMethodManagementInterface $ paymentMethodManagement ;
29
+ private PaymentMethodListInterface $ paymentMethodList ;
30
+
31
+ private array $ methodList = [];
32
+ private int $ storeId ;
32
33
33
34
public function __construct (
34
35
HyvaCheckoutConfig $ hyvaCheckoutConfig ,
35
- ScopeConfigInterface $ scopeConfig ,
36
36
Config $ config ,
37
37
PaymentInterfaceFactory $ paymentFactory ,
38
- StoreManagerInterface $ storeManager ,
39
- PaymentMethodManagementInterface $ paymentMethodManagement
38
+ PaymentMethodManagementInterface $ paymentMethodManagement ,
39
+ PaymentMethodListInterface $ paymentMethodList
40
40
) {
41
41
$ this ->paymentFactory = $ paymentFactory ;
42
42
$ this ->config = $ config ;
43
- $ this ->storeManager = $ storeManager ;
44
- $ this ->scopeConfig = $ scopeConfig ;
45
43
$ this ->hyvaCheckoutConfig = $ hyvaCheckoutConfig ;
46
44
$ this ->paymentMethodManagement = $ paymentMethodManagement ;
45
+ $ this ->paymentMethodList = $ paymentMethodList ;
47
46
}
48
47
49
48
public function execute (Observer $ observer ): void
@@ -56,13 +55,14 @@ public function execute(Observer $observer): void
56
55
return ;
57
56
}
58
57
59
- $ defaultMethod = $ this ->config ->getDefaultSelectedMethod ($ this ->storeManager ->getStore ()->getId ());
58
+ $ this ->storeId = (int )$ quote ->getStoreId ();
59
+ $ defaultMethod = $ this ->config ->getDefaultSelectedMethod ();
60
60
if (!$ defaultMethod ) {
61
61
return ;
62
62
}
63
63
64
64
if ($ defaultMethod == 'first_mollie_method ' ) {
65
- $ defaultMethod = $ this ->getFirstAvailableMollieMethod ($ quote );
65
+ $ defaultMethod = $ this ->getFirstAvailableMollieMethod ();
66
66
}
67
67
68
68
if ($ defaultMethod && !$ this ->isMethodActive ($ defaultMethod )) {
@@ -91,11 +91,16 @@ public function execute(Observer $observer): void
91
91
*/
92
92
private function isMethodActive (string $ methodCode ): bool
93
93
{
94
- return $ this ->scopeConfig ->isSetFlag (
95
- sprintf ('payment/%s/active ' , $ methodCode ),
96
- ScopeInterface::SCOPE_STORE ,
97
- $ this ->storeManager ->getStore ()->getCode ()
98
- );
94
+ $ methods = $ this ->getMethodList ();
95
+
96
+ /** @var PaymentMethodInterface $method */
97
+ foreach ($ methods as $ method ) {
98
+ if ($ method ->getCode () === $ methodCode ) {
99
+ return $ method ->getIsActive ();
100
+ }
101
+ }
102
+
103
+ return false ;
99
104
}
100
105
101
106
private function isHyvaCheckoutActive (): bool
@@ -108,9 +113,9 @@ private function quoteHasActivePaymentMethod(Quote $quote): bool
108
113
return $ quote ->getPayment ()->getMethod () !== null ;
109
114
}
110
115
111
- private function getFirstAvailableMollieMethod (Quote $ quote ): ?string
116
+ private function getFirstAvailableMollieMethod (): ?string
112
117
{
113
- $ methods = $ this ->paymentMethodManagement -> getList ( $ quote -> getId () );
118
+ $ methods = $ this ->getMethodList ( );
114
119
115
120
foreach ($ methods as $ method ) {
116
121
$ methodCode = $ method ->getCode ();
@@ -124,4 +129,14 @@ private function getFirstAvailableMollieMethod(Quote $quote): ?string
124
129
125
130
return null ;
126
131
}
132
+
133
+ private function getMethodList (): array
134
+ {
135
+ if ($ this ->methodList !== []) {
136
+ return $ this ->methodList ;
137
+ }
138
+
139
+ $ this ->methodList = $ this ->paymentMethodList ->getList ($ this ->storeId );
140
+ return $ this ->methodList ;
141
+ }
127
142
}
0 commit comments