This repository has been archived by the owner on Oct 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExamplePaymentMethod.php
103 lines (96 loc) · 3.4 KB
/
ExamplePaymentMethod.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
declare(strict_types=1);
namespace Oneserv\ExamplePaymentMethod\Model\Method;
use Magento\Framework\App\Area;
use Magento\Framework\App\State;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Payment\Gateway\Command\CommandManagerInterface;
use Magento\Payment\Gateway\Command\CommandPoolInterface;
use Magento\Payment\Gateway\Config\ValueHandlerPoolInterface;
use Magento\Payment\Gateway\Data\PaymentDataObjectFactory;
use Magento\Payment\Gateway\Validator\ValidatorPoolInterface;
use Magento\Payment\Model\Method\Adapter;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\Quote;
use Psr\Log\LoggerInterface;
/**
* Class ExamplePaymentMethod
*/
class ExamplePaymentMethod extends Adapter
{
private State $state;
/**
* ExamplePaymentMethod constructor.
*
* @param ManagerInterface $eventManager
* @param ValueHandlerPoolInterface $valueHandlerPool
* @param PaymentDataObjectFactory $paymentDataObjectFactory
* @param string $code
* @param string $formBlockType
* @param string $infoBlockType
* @param State $state
* @param CommandPoolInterface|null $commandPool
* @param ValidatorPoolInterface|null $validatorPool
* @param CommandManagerInterface|null $commandExecutor
* @param LoggerInterface|null $logger
*/
public function __construct(
ManagerInterface $eventManager,
ValueHandlerPoolInterface $valueHandlerPool,
PaymentDataObjectFactory $paymentDataObjectFactory,
string $code,
string $formBlockType,
string $infoBlockType,
State $state,
CommandPoolInterface $commandPool = null,
ValidatorPoolInterface $validatorPool = null,
CommandManagerInterface $commandExecutor = null,
LoggerInterface $logger = null
) {
parent::__construct(
$eventManager,
$valueHandlerPool,
$paymentDataObjectFactory,
$code,
$formBlockType,
$infoBlockType,
$commandPool,
$validatorPool,
$commandExecutor,
$logger
);
$this->state = $state;
}
/**
* @inheritdoc
* This is a temporary workaround for https://github.com/magento/magento2/issues/33869.
* It sets the info instance before the method gets executed. Otherwise, the validator doesn't get called
* correctly.
*/
public function isAvailable(CartInterface $quote = null)
{
// remove this line if you want the availability validator to be called
return parent::isAvailable($quote);
if ($quote === null) {
return parent::isAvailable($quote);
}
/** @var Quote $quote */
/*
* This is needed to avoid issues when creating a new order via adminhtml. There is an error as the quote has
* empty data and somewhere deep in magento it causes an issue.
*/
try {
if (
$this->state->getAreaCode() === Area::AREA_ADMINHTML &&
$quote->getDataByKey(Quote::KEY_STORE_ID) === null
) {
return parent::isAvailable($quote);
}
} catch (LocalizedException $exception) {
return parent::isAvailable($quote);
}
$this->setInfoInstance($quote->getPayment());
return parent::isAvailable($quote);
}
}