|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Adyen\Hyva\Block; |
| 6 | + |
| 7 | +use Adyen\Hyva\Model\Configuration; |
| 8 | +use Adyen\Hyva\Model\MethodList; |
| 9 | +use Exception; |
| 10 | +use Magento\Checkout\Model\Session; |
| 11 | +use Magento\Framework\Serialize\Serializer\Json as JsonSerializer; |
| 12 | +use Magento\Framework\View\Element\Template; |
| 13 | +use Magento\Quote\Model\Quote; |
| 14 | + |
| 15 | +class PaymentMethod extends Template |
| 16 | +{ |
| 17 | + public function __construct( |
| 18 | + Template\Context $context, |
| 19 | + private Configuration $configuration, |
| 20 | + private MethodList $methodList, |
| 21 | + private Session $checkoutSession, |
| 22 | + private JsonSerializer $jsonSerializer, |
| 23 | + array $data = [] |
| 24 | + ) { |
| 25 | + parent::__construct($context, $data); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @return Configuration |
| 30 | + */ |
| 31 | + public function getConfiguration(): Configuration |
| 32 | + { |
| 33 | + return $this->configuration; |
| 34 | + } |
| 35 | + |
| 36 | + public function getAvailableMethods(): array |
| 37 | + { |
| 38 | + return $this->methodList->collectAvailableMethods(); |
| 39 | + } |
| 40 | + |
| 41 | + public function getQuoteShippingAddress(): string |
| 42 | + { |
| 43 | + try { |
| 44 | + $quote = $this->getQuote(); |
| 45 | + |
| 46 | + if ($quote && $quote->getShippingAddress()) { |
| 47 | + return $this->jsonSerializer->serialize($quote->getShippingAddress()->getData()); |
| 48 | + } |
| 49 | + } catch (\InvalidArgumentException $exception) { |
| 50 | + return $this->defaultResponse(); |
| 51 | + } |
| 52 | + |
| 53 | + return $this->defaultResponse(); |
| 54 | + } |
| 55 | + |
| 56 | + public function getQuoteBillingAddress(): string |
| 57 | + { |
| 58 | + try { |
| 59 | + $quote = $this->getQuote(); |
| 60 | + |
| 61 | + if ($quote && $quote->getBillingAddress()) { |
| 62 | + return $this->jsonSerializer->serialize($quote->getBillingAddress()->getData()); |
| 63 | + } |
| 64 | + } catch (\InvalidArgumentException $exception) { |
| 65 | + return $this->defaultResponse(); |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + return $this->defaultResponse(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @return Quote|null |
| 74 | + */ |
| 75 | + private function getQuote(): ?Quote |
| 76 | + { |
| 77 | + try { |
| 78 | + /** @var Quote $quote */ |
| 79 | + $quote = $this->checkoutSession->getQuote(); |
| 80 | + |
| 81 | + return $quote; |
| 82 | + } catch (Exception $exception) { |
| 83 | + return null; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @return string |
| 89 | + */ |
| 90 | + private function defaultResponse(): string |
| 91 | + { |
| 92 | + return $this->jsonSerializer->serialize([]); |
| 93 | + } |
| 94 | +} |
0 commit comments