Skip to content

Commit 97aab9c

Browse files
Merge branch 'improvement/check-if-default-method-is-active' into release
2 parents 4e1f907 + 96ae60f commit 97aab9c

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

src/Mollie_HyvaCheckout/Observer/SalesQuoteCollectTotalsBefore/SetDefaultSelectedPaymentMethod.php

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,39 @@
1010

1111
use Hyva\Checkout\Model\CheckoutInformation\Luma;
1212
use Hyva\Checkout\Model\ConfigData\HyvaThemes\SystemConfigGeneral as HyvaCheckoutConfig;
13-
use Magento\Framework\App\Config\ScopeConfigInterface;
1413
use Magento\Framework\Event\Observer;
1514
use Magento\Framework\Event\ObserverInterface;
1615
use Magento\Framework\Exception\State\InvalidTransitionException;
16+
use Magento\Payment\Api\Data\PaymentMethodInterface;
17+
use Magento\Payment\Api\PaymentMethodListInterface;
1718
use Magento\Quote\Api\Data\PaymentInterfaceFactory;
1819
use Magento\Quote\Api\PaymentMethodManagementInterface;
1920
use Magento\Quote\Model\Quote;
20-
use Magento\Store\Model\ScopeInterface;
21-
use Magento\Store\Model\StoreManagerInterface;
2221
use Mollie\Payment\Config;
2322

2423
class SetDefaultSelectedPaymentMethod implements ObserverInterface
2524
{
2625
private PaymentInterfaceFactory $paymentFactory;
2726
private Config $config;
28-
private StoreManagerInterface $storeManager;
29-
private ScopeConfigInterface $scopeConfig;
3027
private HyvaCheckoutConfig $hyvaCheckoutConfig;
3128
private PaymentMethodManagementInterface $paymentMethodManagement;
29+
private PaymentMethodListInterface $paymentMethodList;
30+
31+
private array $methodList = [];
32+
private int $storeId;
3233

3334
public function __construct(
3435
HyvaCheckoutConfig $hyvaCheckoutConfig,
35-
ScopeConfigInterface $scopeConfig,
3636
Config $config,
3737
PaymentInterfaceFactory $paymentFactory,
38-
StoreManagerInterface $storeManager,
39-
PaymentMethodManagementInterface $paymentMethodManagement
38+
PaymentMethodManagementInterface $paymentMethodManagement,
39+
PaymentMethodListInterface $paymentMethodList
4040
) {
4141
$this->paymentFactory = $paymentFactory;
4242
$this->config = $config;
43-
$this->storeManager = $storeManager;
44-
$this->scopeConfig = $scopeConfig;
4543
$this->hyvaCheckoutConfig = $hyvaCheckoutConfig;
4644
$this->paymentMethodManagement = $paymentMethodManagement;
45+
$this->paymentMethodList = $paymentMethodList;
4746
}
4847

4948
public function execute(Observer $observer): void
@@ -56,13 +55,14 @@ public function execute(Observer $observer): void
5655
return;
5756
}
5857

59-
$defaultMethod = $this->config->getDefaultSelectedMethod($this->storeManager->getStore()->getId());
58+
$this->storeId = (int)$quote->getStoreId();
59+
$defaultMethod = $this->config->getDefaultSelectedMethod();
6060
if (!$defaultMethod) {
6161
return;
6262
}
6363

6464
if ($defaultMethod == 'first_mollie_method') {
65-
$defaultMethod = $this->getFirstAvailableMollieMethod($quote);
65+
$defaultMethod = $this->getFirstAvailableMollieMethod();
6666
}
6767

6868
if ($defaultMethod && !$this->isMethodActive($defaultMethod)) {
@@ -91,11 +91,16 @@ public function execute(Observer $observer): void
9191
*/
9292
private function isMethodActive(string $methodCode): bool
9393
{
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;
99104
}
100105

101106
private function isHyvaCheckoutActive(): bool
@@ -108,9 +113,9 @@ private function quoteHasActivePaymentMethod(Quote $quote): bool
108113
return $quote->getPayment()->getMethod() !== null;
109114
}
110115

111-
private function getFirstAvailableMollieMethod(Quote $quote): ?string
116+
private function getFirstAvailableMollieMethod(): ?string
112117
{
113-
$methods = $this->paymentMethodManagement->getList($quote->getId());
118+
$methods = $this->getMethodList();
114119

115120
foreach ($methods as $method) {
116121
$methodCode = $method->getCode();
@@ -124,4 +129,14 @@ private function getFirstAvailableMollieMethod(Quote $quote): ?string
124129

125130
return null;
126131
}
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+
}
127142
}

0 commit comments

Comments
 (0)