Skip to content

Commit a45a9f9

Browse files
authored
Merge pull request #46 from Adyen/develop
Release 1.0.0
2 parents 7725d6b + 197a724 commit a45a9f9

23 files changed

+736
-294
lines changed

Block/Template.php

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Adyen\Hyva\Block;
6+
7+
use Adyen\Hyva\Magewire\Payment\Method\AbstractPaymentMethodWire;
8+
use Adyen\Hyva\Model\MethodList;
9+
use Adyen\Hyva\Model\PaymentMethodBlock;
10+
use Adyen\Hyva\Model\PaymentMethodBlockFactory;
11+
use Adyen\Hyva\Model\Ui\AdyenHyvaConfigProvider;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\View\Element\Template\Context;
14+
use Adyen\Hyva\Magewire\Payment\Method\AbstractPaymentMethodWireFactory;
15+
16+
class Template extends \Magento\Framework\View\Element\Template
17+
{
18+
const PARENT_PAYMENT_METHODS_BLOCK = 'checkout.payment.methods';
19+
const DEFAULT_ADYEN_PAYMENT_METHOD_TEMPLATE = 'adyen-default-method.phtml';
20+
const TEMPLATE_DIR = 'Adyen_Hyva::payment/method-renderer';
21+
const MAGEWIRE = 'magewire';
22+
23+
/**
24+
* @var AdyenHyvaConfigProvider
25+
*/
26+
private AdyenHyvaConfigProvider $adyenHyvaConfigProvider;
27+
28+
/**
29+
* @var PaymentMethodBlockFactory
30+
*/
31+
private PaymentMethodBlockFactory $paymentMethodBlockFactory;
32+
33+
/**
34+
* @var AbstractPaymentMethodWireFactory
35+
*/
36+
private AbstractPaymentMethodWireFactory $paymentMethodWireFactory;
37+
38+
/**
39+
* @var MethodList
40+
*/
41+
private MethodList $methodList;
42+
43+
public function __construct(
44+
AdyenHyvaConfigProvider $adyenHyvaConfigProvider,
45+
PaymentMethodBlockFactory $paymentMethodBlockFactory,
46+
AbstractPaymentMethodWireFactory $paymentMethodWireFactory,
47+
Context $context,
48+
MethodList $methodList,
49+
array $data = []
50+
) {
51+
parent::__construct($context, $data);
52+
53+
$this->adyenHyvaConfigProvider = $adyenHyvaConfigProvider;
54+
$this->paymentMethodBlockFactory = $paymentMethodBlockFactory;
55+
$this->paymentMethodWireFactory = $paymentMethodWireFactory;
56+
$this->methodList = $methodList;
57+
}
58+
59+
/**
60+
* Overrides parent method to include Adyen payment methods
61+
*
62+
* @return Template
63+
* @throws LocalizedException
64+
*/
65+
public function _prepareLayout()
66+
{
67+
$this->renderAdyenPaymentMethods();
68+
69+
return parent::_prepareLayout();
70+
}
71+
72+
/**
73+
* Render available payment methods
74+
*
75+
* @return void
76+
* @throws LocalizedException
77+
*/
78+
private function renderAdyenPaymentMethods(): void
79+
{
80+
$methods = $this->methodList->collectAvailableMethods();
81+
/** @var PaymentMethodBlock[] $paymentMethodBlocks */
82+
$paymentMethodBlocks = [];
83+
84+
foreach ($methods as $method) {
85+
/** @var PaymentMethodBlock $paymentMethodBlock */
86+
$paymentMethodBlock = $this->paymentMethodBlockFactory->create();
87+
88+
if ($this->methodList->getCustomMagewireClass($method)) {
89+
$paymentMethodBlock->setWire($this->methodList->getCustomMagewireClass($method));
90+
} else {
91+
/** @var AbstractPaymentMethodWire $paymentMethodWire */
92+
$paymentMethodWire = $this->paymentMethodWireFactory->create();
93+
$paymentMethodWire->setMethodCode($method);
94+
95+
$paymentMethodBlock->setWire($paymentMethodWire);
96+
}
97+
98+
$paymentMethodBlock->setMethodName($method);
99+
$paymentMethodBlock->setBlockName(
100+
$this->generateBlockNameFromPaymentMethodName($method)
101+
);
102+
$paymentMethodBlock->setTemplate(
103+
$this->getPaymentMethodTemplate($method)
104+
);
105+
106+
$paymentMethodBlocks[] = $paymentMethodBlock;
107+
}
108+
109+
foreach ($paymentMethodBlocks as $paymentMethodBlock) {
110+
$this->createPaymentMethodBlock($paymentMethodBlock);
111+
}
112+
}
113+
114+
/**
115+
* Creates the payment method block in the checkout layout
116+
*
117+
* @param PaymentMethodBlock $paymentMethodBlock
118+
* @return void
119+
* @throws LocalizedException
120+
*/
121+
private function createPaymentMethodBlock(PaymentMethodBlock $paymentMethodBlock): void
122+
{
123+
$layout = $this->getLayout();
124+
125+
if (!array_key_exists($paymentMethodBlock->getBlockName(), $layout->getAllBlocks())) {
126+
$block = $layout->createBlock(
127+
\Magento\Framework\View\Element\Template::class,
128+
$paymentMethodBlock->getBlockName()
129+
);
130+
$block->setTemplate($paymentMethodBlock->getTemplate());
131+
$block->setData(self::MAGEWIRE, $paymentMethodBlock->getWire());
132+
133+
$layout->setChild(
134+
self::PARENT_PAYMENT_METHODS_BLOCK,
135+
$paymentMethodBlock->getBlockName(),
136+
$paymentMethodBlock->getMethodName()
137+
);
138+
}
139+
}
140+
141+
/**
142+
* Generates the payment method block name from the given method name
143+
*
144+
* @param string $paymentMethodName
145+
* @return string
146+
*/
147+
private function generateBlockNameFromPaymentMethodName(string $paymentMethodName): string
148+
{
149+
return sprintf('%s.%s', self::PARENT_PAYMENT_METHODS_BLOCK, $paymentMethodName);
150+
}
151+
152+
/**
153+
* Builds template file depending on the custom renderer requirement.
154+
*
155+
* @param string $paymentMethodName
156+
* @return string
157+
*/
158+
private function getPaymentMethodTemplate(string $paymentMethodName): string
159+
{
160+
if ($this->adyenHyvaConfigProvider->isCustomRendererRequired($paymentMethodName)) {
161+
$template = $this->methodList->getCustomMethodRenderer($paymentMethodName);
162+
} else {
163+
$template = self::DEFAULT_ADYEN_PAYMENT_METHOD_TEMPLATE;
164+
}
165+
166+
return sprintf('%s/%s', self::TEMPLATE_DIR, $template);
167+
}
168+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Adyen\Hyva\Magewire\Payment\Method;
6+
7+
use Hyva\Checkout\Model\Magewire\Component\Evaluation\EvaluationResult;
8+
use Hyva\Checkout\Model\Magewire\Component\EvaluationInterface;
9+
use Hyva\Checkout\Model\Magewire\Component\EvaluationResultFactory;
10+
11+
class AbstractPaymentMethodWire extends AdyenPaymentComponent implements EvaluationInterface
12+
{
13+
/**
14+
* @var string
15+
*/
16+
protected string $methodCode;
17+
18+
/**
19+
* {@inheritDoc}
20+
*/
21+
public function evaluateCompletion(EvaluationResultFactory $resultFactory): EvaluationResult
22+
{
23+
return $resultFactory->createValidation('validate-adyen-component-state');
24+
}
25+
26+
/**
27+
* @return string
28+
*/
29+
public function getMethodCode(): string
30+
{
31+
return $this->methodCode;
32+
}
33+
34+
/**
35+
* @param string $methodCode
36+
* @return $this
37+
*/
38+
public function setMethodCode(string $methodCode): AbstractPaymentMethodWire
39+
{
40+
$this->methodCode = $methodCode;
41+
42+
return $this;
43+
}
44+
45+
public function getContainerName(): string
46+
{
47+
return str_replace('-', '', $this->getMethodCode());
48+
}
49+
}

Magewire/Payment/Method/ApplePay.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

Magewire/Payment/Method/GooglePay.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

Magewire/Payment/Method/Klarna.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

Magewire/Payment/Method/Paypal.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)