-
Notifications
You must be signed in to change notification settings - Fork 8
/
Template.php
168 lines (143 loc) · 5.45 KB
/
Template.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
declare(strict_types=1);
namespace Adyen\Hyva\Block;
use Adyen\Hyva\Magewire\Payment\Method\AbstractPaymentMethodWire;
use Adyen\Hyva\Model\MethodList;
use Adyen\Hyva\Model\PaymentMethodBlock;
use Adyen\Hyva\Model\PaymentMethodBlockFactory;
use Adyen\Hyva\Model\Ui\AdyenHyvaConfigProvider;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\View\Element\Template\Context;
use Adyen\Hyva\Magewire\Payment\Method\AbstractPaymentMethodWireFactory;
class Template extends \Magento\Framework\View\Element\Template
{
const PARENT_PAYMENT_METHODS_BLOCK = 'checkout.payment.methods';
const DEFAULT_ADYEN_PAYMENT_METHOD_TEMPLATE = 'adyen-default-method.phtml';
const TEMPLATE_DIR = 'Adyen_Hyva::payment/method-renderer';
const MAGEWIRE = 'magewire';
/**
* @var AdyenHyvaConfigProvider
*/
private AdyenHyvaConfigProvider $adyenHyvaConfigProvider;
/**
* @var PaymentMethodBlockFactory
*/
private PaymentMethodBlockFactory $paymentMethodBlockFactory;
/**
* @var AbstractPaymentMethodWireFactory
*/
private AbstractPaymentMethodWireFactory $paymentMethodWireFactory;
/**
* @var MethodList
*/
private MethodList $methodList;
public function __construct(
AdyenHyvaConfigProvider $adyenHyvaConfigProvider,
PaymentMethodBlockFactory $paymentMethodBlockFactory,
AbstractPaymentMethodWireFactory $paymentMethodWireFactory,
Context $context,
MethodList $methodList,
array $data = []
) {
parent::__construct($context, $data);
$this->adyenHyvaConfigProvider = $adyenHyvaConfigProvider;
$this->paymentMethodBlockFactory = $paymentMethodBlockFactory;
$this->paymentMethodWireFactory = $paymentMethodWireFactory;
$this->methodList = $methodList;
}
/**
* Overrides parent method to include Adyen payment methods
*
* @return Template
* @throws LocalizedException
*/
public function _prepareLayout()
{
$this->renderAdyenPaymentMethods();
return parent::_prepareLayout();
}
/**
* Render available payment methods
*
* @return void
* @throws LocalizedException
*/
private function renderAdyenPaymentMethods(): void
{
$methods = $this->methodList->collectAvailableMethods();
/** @var PaymentMethodBlock[] $paymentMethodBlocks */
$paymentMethodBlocks = [];
foreach ($methods as $method) {
/** @var PaymentMethodBlock $paymentMethodBlock */
$paymentMethodBlock = $this->paymentMethodBlockFactory->create();
if ($this->methodList->getCustomMagewireClass($method)) {
$paymentMethodBlock->setWire($this->methodList->getCustomMagewireClass($method));
} else {
/** @var AbstractPaymentMethodWire $paymentMethodWire */
$paymentMethodWire = $this->paymentMethodWireFactory->create();
$paymentMethodWire->setMethodCode($method);
$paymentMethodBlock->setWire($paymentMethodWire);
}
$paymentMethodBlock->setMethodName($method);
$paymentMethodBlock->setBlockName(
$this->generateBlockNameFromPaymentMethodName($method)
);
$paymentMethodBlock->setTemplate(
$this->getPaymentMethodTemplate($method)
);
$paymentMethodBlocks[] = $paymentMethodBlock;
}
foreach ($paymentMethodBlocks as $paymentMethodBlock) {
$this->createPaymentMethodBlock($paymentMethodBlock);
}
}
/**
* Creates the payment method block in the checkout layout
*
* @param PaymentMethodBlock $paymentMethodBlock
* @return void
* @throws LocalizedException
*/
private function createPaymentMethodBlock(PaymentMethodBlock $paymentMethodBlock): void
{
$layout = $this->getLayout();
if (!array_key_exists($paymentMethodBlock->getBlockName(), $layout->getAllBlocks())) {
$block = $layout->createBlock(
\Magento\Framework\View\Element\Template::class,
$paymentMethodBlock->getBlockName()
);
$block->setTemplate($paymentMethodBlock->getTemplate());
$block->setData(self::MAGEWIRE, $paymentMethodBlock->getWire());
$layout->setChild(
self::PARENT_PAYMENT_METHODS_BLOCK,
$paymentMethodBlock->getBlockName(),
$paymentMethodBlock->getMethodName()
);
}
}
/**
* Generates the payment method block name from the given method name
*
* @param string $paymentMethodName
* @return string
*/
private function generateBlockNameFromPaymentMethodName(string $paymentMethodName): string
{
return sprintf('%s.%s', self::PARENT_PAYMENT_METHODS_BLOCK, $paymentMethodName);
}
/**
* Builds template file depending on the custom renderer requirement.
*
* @param string $paymentMethodName
* @return string
*/
private function getPaymentMethodTemplate(string $paymentMethodName): string
{
if ($this->adyenHyvaConfigProvider->isCustomRendererRequired($paymentMethodName)) {
$template = $this->methodList->getCustomMethodRenderer($paymentMethodName);
} else {
$template = self::DEFAULT_ADYEN_PAYMENT_METHOD_TEMPLATE;
}
return sprintf('%s/%s', self::TEMPLATE_DIR, $template);
}
}