|
| 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 | +} |
0 commit comments