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