From e19091298e063e1fabb2894239505ea133dc2b57 Mon Sep 17 00:00:00 2001 From: "a.baraliu" Date: Wed, 11 Sep 2024 01:53:18 +0200 Subject: [PATCH] BP-3734-Add-support-for-PayByBank --- Magewire/Payment/Method/PayByBank.php | 184 ++++++++++++++++++ etc/frontend/di.xml | 4 +- .../layout/hyva_checkout_components.xml | 10 + .../component/payment/method/paybybank.phtml | 47 +++++ 4 files changed, 243 insertions(+), 2 deletions(-) create mode 100644 Magewire/Payment/Method/PayByBank.php create mode 100644 view/frontend/templates/component/payment/method/paybybank.phtml diff --git a/Magewire/Payment/Method/PayByBank.php b/Magewire/Payment/Method/PayByBank.php new file mode 100644 index 0000000..916634d --- /dev/null +++ b/Magewire/Payment/Method/PayByBank.php @@ -0,0 +1,184 @@ + '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'; + } +} diff --git a/etc/frontend/di.xml b/etc/frontend/di.xml index a88943f..ddd9644 100644 --- a/etc/frontend/di.xml +++ b/etc/frontend/di.xml @@ -14,7 +14,7 @@ \Buckaroo\HyvaCheckout\Model\Magewire\Payment\PlaceOrderService \Buckaroo\HyvaCheckout\Model\Magewire\Payment\PlaceOrderService \Buckaroo\HyvaCheckout\Model\Magewire\Payment\PlaceOrderService - + \Buckaroo\HyvaCheckout\Model\Magewire\Payment\PlaceOrderService \Buckaroo\HyvaCheckout\Model\Magewire\Payment\PlaceOrderService \Buckaroo\HyvaCheckout\Model\Magewire\Payment\PlaceOrderService \Buckaroo\HyvaCheckout\Model\Magewire\Payment\PlaceOrderService @@ -48,4 +48,4 @@ - \ No newline at end of file + diff --git a/view/frontend/layout/hyva_checkout_components.xml b/view/frontend/layout/hyva_checkout_components.xml index 3292516..850d814 100644 --- a/view/frontend/layout/hyva_checkout_components.xml +++ b/view/frontend/layout/hyva_checkout_components.xml @@ -195,6 +195,16 @@ + + + + \Buckaroo\HyvaCheckout\Magewire\Payment\Method\PayByBank + + + + diff --git a/view/frontend/templates/component/payment/method/paybybank.phtml b/view/frontend/templates/component/payment/method/paybybank.phtml new file mode 100644 index 0000000..1ad4044 --- /dev/null +++ b/view/frontend/templates/component/payment/method/paybybank.phtml @@ -0,0 +1,47 @@ +getIssuers(); + +?> +
+
+ displayAsSelect()) { ?> + + + + +

escapeHtml(__('Select a bank:')); ?>

+ +
+ issuer ? 'checked' : '' ?> + /> + +
+ + +
+