Skip to content

Commit

Permalink
Merge pull request #46 from Adyen/develop
Browse files Browse the repository at this point in the history
Release 1.0.0
  • Loading branch information
candemiralp authored Jul 18, 2024
2 parents 7725d6b + 197a724 commit a45a9f9
Show file tree
Hide file tree
Showing 23 changed files with 736 additions and 294 deletions.
168 changes: 168 additions & 0 deletions Block/Template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,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);
}
}
49 changes: 49 additions & 0 deletions Magewire/Payment/Method/AbstractPaymentMethodWire.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Adyen\Hyva\Magewire\Payment\Method;

use Hyva\Checkout\Model\Magewire\Component\Evaluation\EvaluationResult;
use Hyva\Checkout\Model\Magewire\Component\EvaluationInterface;
use Hyva\Checkout\Model\Magewire\Component\EvaluationResultFactory;

class AbstractPaymentMethodWire extends AdyenPaymentComponent implements EvaluationInterface
{
/**
* @var string
*/
protected string $methodCode;

/**
* {@inheritDoc}
*/
public function evaluateCompletion(EvaluationResultFactory $resultFactory): EvaluationResult
{
return $resultFactory->createValidation('validate-adyen-component-state');
}

/**
* @return string
*/
public function getMethodCode(): string
{
return $this->methodCode;
}

/**
* @param string $methodCode
* @return $this
*/
public function setMethodCode(string $methodCode): AbstractPaymentMethodWire
{
$this->methodCode = $methodCode;

return $this;
}

public function getContainerName(): string
{
return str_replace('-', '', $this->getMethodCode());
}
}
30 changes: 0 additions & 30 deletions Magewire/Payment/Method/ApplePay.php

This file was deleted.

30 changes: 0 additions & 30 deletions Magewire/Payment/Method/GooglePay.php

This file was deleted.

30 changes: 0 additions & 30 deletions Magewire/Payment/Method/Klarna.php

This file was deleted.

30 changes: 0 additions & 30 deletions Magewire/Payment/Method/Paypal.php

This file was deleted.

Loading

0 comments on commit a45a9f9

Please sign in to comment.