Skip to content

Commit

Permalink
Merge pull request #37 from buckaroo-it/BP-3734-Add-support-for-PayBy…
Browse files Browse the repository at this point in the history
…Bank

BP-3734-Add-support-for-PayByBank
  • Loading branch information
vegimcarkaxhija authored Sep 11, 2024
2 parents 63f92e7 + e190912 commit ef2f991
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 2 deletions.
184 changes: 184 additions & 0 deletions Magewire/Payment/Method/PayByBank.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
<?php

declare(strict_types=1);

namespace Buckaroo\HyvaCheckout\Magewire\Payment\Method;

use Rakit\Validation\Validator;
use Magewirephp\Magewire\Component;
use Magento\Framework\View\Asset\Repository;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Checkout\Model\Session as SessionCheckout;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Hyva\Checkout\Model\Magewire\Component\EvaluationInterface;
use Hyva\Checkout\Model\Magewire\Component\EvaluationResultFactory;
use Hyva\Checkout\Model\Magewire\Component\EvaluationResultInterface;
use Buckaroo\Magento2\Model\ConfigProvider\Method\PayByBank as MethodPayByBank;

class PayByBank extends Component\Form implements EvaluationInterface
{
public ?string $issuer = null;

protected $loader = [
'issuer' => 'Saving Bank issuer'
];

protected $rules = [
'issuer' => 'required'
];

protected $messages = [
'issuer:required' => 'The bank issuer is required'
];

protected SessionCheckout $sessionCheckout;

protected CartRepositoryInterface $quoteRepository;

protected Repository $assetRepo;

protected ScopeConfigInterface $scopeConfig;


public function __construct(
Validator $validator,
SessionCheckout $sessionCheckout,
CartRepositoryInterface $quoteRepository,
Repository $assetRepo,
ScopeConfigInterface $scopeConfig,

) {
parent::__construct($validator);

$this->sessionCheckout = $sessionCheckout;
$this->quoteRepository = $quoteRepository;
$this->assetRepo = $assetRepo;
$this->scopeConfig = $scopeConfig;

}

/**
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function mount(): void
{
$this->issuer = $this->getLastIssuer();
}

/**
* Listen for bank issuer been updated.
*/
public function updatedIssuer(string $value): ?string
{
$this->validateOnly();
$value = empty($value) ? null : $value;

try {
$quote = $this->sessionCheckout->getQuote();
$quote->getPayment()->setAdditionalInformation('issuer', $value);

$this->quoteRepository->save($quote);
} catch (LocalizedException $exception) {
$this->dispatchErrorMessage($exception->getMessage());
}

return $value;
}

public function evaluateCompletion(EvaluationResultFactory $resultFactory): EvaluationResultInterface
{
if ($this->issuer === null) {
return $resultFactory->createErrorMessageEvent()
->withCustomEvent('payment:method:error')
->withMessage('The bank issuer is required');
}

return $resultFactory->createSuccess();
}

public function getIssuers(): array
{
return [
[
'name' => 'ABN AMRO',
'code' => 'ABNANL2A',
'imgName' => 'abnamro'
],
[
'name' => 'ASN Bank',
'code' => 'ASNBNL21',
'imgName' => 'asnbank'
],
[
'name' => 'ING',
'code' => 'INGBNL2A',
'imgName' => 'ing'
],
[
'name' => 'Knab Bank',
'code' => 'KNABNL2H',
'imgName' => 'knab'
],
[
'name' => 'Rabobank',
'code' => 'RABONL2U',
'imgName' => 'rabobank'
],
[
'name' => 'RegioBank',
'code' => 'RBRBNL21',
'imgName' => 'regiobank'
],
[
'name' => 'SNS Bank',
'code' => 'SNSBNL2A',
'imgName' => 'sns'
],
[
'name' => 'N26',
'code' => 'NTSBDEB1',
'imgName' => 'n26'
]
];
}

public function getLastIssuer()
{

$quote = $this->sessionCheckout->getQuote();

$customerId = $quote->getCustomerId();

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerRepository = $objectManager->get(\Magento\Customer\Api\CustomerRepositoryInterface::class);

$customer = $customerRepository->getById($customerId);

$customAttributes = $customer->getCustomAttributes();
$issuerAttribute = $customAttributes['buckaroo_last_paybybank_issuer'] ?? null;

if ($issuerAttribute) {
return $issuerAttribute->getValue();
} else {
return $issuerAttribute;
}

}

public function getImageUrl(string $issuerImage): string
{
return $this->assetRepo->getUrl("Buckaroo_Magento2::images/ideal/{$issuerImage}.svg");
}

public function displayAsSelect($storeId = null): bool
{
return $this->scopeConfig->getValue(
MethodPayByBank::XPATH_PAYBYBANK_SELECTION_TYPE,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$storeId
) === '2';
}
}
4 changes: 2 additions & 2 deletions etc/frontend/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<item name="buckaroo_magento2_eps" xsi:type="object">\Buckaroo\HyvaCheckout\Model\Magewire\Payment\PlaceOrderService</item>
<item name="buckaroo_magento2_transfer" xsi:type="object">\Buckaroo\HyvaCheckout\Model\Magewire\Payment\PlaceOrderService</item>
<item name="buckaroo_magento2_payconiq" xsi:type="object">\Buckaroo\HyvaCheckout\Model\Magewire\Payment\PlaceOrderService</item>

<item name="buckaroo_magento2_paybybank" xsi:type="object">\Buckaroo\HyvaCheckout\Model\Magewire\Payment\PlaceOrderService</item>
<item name="buckaroo_magento2_ideal" xsi:type="object">\Buckaroo\HyvaCheckout\Model\Magewire\Payment\PlaceOrderService</item>
<item name="buckaroo_magento2_idealprocessing" xsi:type="object">\Buckaroo\HyvaCheckout\Model\Magewire\Payment\PlaceOrderService</item>
<item name="buckaroo_magento2_klarna" xsi:type="object">\Buckaroo\HyvaCheckout\Model\Magewire\Payment\PlaceOrderService</item>
Expand Down Expand Up @@ -48,4 +48,4 @@
</argument>
</arguments>
</type>
</config>
</config>
10 changes: 10 additions & 0 deletions view/frontend/layout/hyva_checkout_components.xml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,16 @@
</arguments>
</block>

<block name="checkout.payment.method.buckaroo_magento2_paybybank"
as="buckaroo_magento2_paybybank"
template="Buckaroo_HyvaCheckout::component/payment/method/paybybank.phtml">
<arguments>
<argument name="magewire" xsi:type="object">
\Buckaroo\HyvaCheckout\Magewire\Payment\Method\PayByBank
</argument>
</arguments>
</block>

</referenceBlock>


Expand Down
47 changes: 47 additions & 0 deletions view/frontend/templates/component/payment/method/paybybank.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);
/** @var Escaper $escaper */
/** @var \Buckaroo\HyvaCheckout\Magewire\Checkout\Payment\Method\PayByBank $magewire */
use Magento\Framework\Escaper;

$issuers = $magewire->getIssuers();

?>
<div class="col-span-6">
<div class="flex flex-col gap-y-2">
<?php if ($magewire->displayAsSelect()) { ?>
<label for="buckaroo_magento2_paybybank_issuer"><?= $escaper->escapeHtml(__('Select a bank:')); ?></label>
<select name="issuer" id="buckaroo_magento2_paybybank_issuer" wire:model="issuer" class="form-select">
<?php foreach ($issuers as $issuerOption): ?>
<option value="<?= $escaper->escapeHtmlAttr($issuerOption['code']) ?>" <?= $issuerOption['code'] === $magewire->issuer ? 'selected' : '' ?>>
<?= $escaper->escapeHtml($issuerOption['name']) ?>
</option>
<?php endforeach; ?>
</select>

<?php } else { ?>
<p><?= $escaper->escapeHtml(__('Select a bank:')); ?></p>
<?php foreach ($issuers as $issuerOption): ?>
<div class="flex flex-row flex-grow gap-x-2 items-center">
<input
type="radio"
name="issuer"
wire:model="issuer"
id="bk_paybybank_issuer_<?= $escaper->escapeHtmlAttr($issuerOption['code']) ?>"
value="<?= $escaper->escapeHtmlAttr($issuerOption['code']) ?>"
<?= $issuerOption['code'] === $magewire->issuer ? 'checked' : '' ?>
/>
<label for="bk_paybybank_issuer_<?= $escaper->escapeHtmlAttr($issuerOption['code']) ?>" class="flex flex-row flex-grow gap-x-2 items-center">
<img
src="<?= $escaper->escapeUrl($magewire->getImageUrl($issuerOption['imgName'])) ?>"
alt="<?= $escaper->escapeHtmlAttr($issuerOption['name']) ?>"
style="max-height:25px;"
/>
<?= $escaper->escapeHtml($issuerOption['name']) ?>
</label>
</div>
<?php endforeach; ?>
<?php } ?>
</div>
</div>

0 comments on commit ef2f991

Please sign in to comment.