1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Kerox\Messenger\Model\Common\Button\Payment; |
6
|
|
|
|
7
|
|
|
class PaymentSummary implements \JsonSerializable |
8
|
|
|
{ |
9
|
|
|
public const PAYMENT_TYPE_FIXED_AMOUNT = 'FIXED_AMOUNT'; |
10
|
|
|
public const PAYMENT_TYPE_FLEXIBLE_AMOUNT = 'FLEXIBLE_AMOUNT'; |
11
|
|
|
|
12
|
|
|
public const USER_INFO_SHIPPING_ADDRESS = 'shipping_address'; |
13
|
|
|
public const USER_INFO_CONTACT_NAME = 'contact_name'; |
14
|
|
|
public const USER_INFO_CONTACT_PHONE = 'contact_phone'; |
15
|
|
|
public const USER_INFO_CONTACT_EMAIL = 'contact_email'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $currency; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var null|bool |
24
|
|
|
*/ |
25
|
|
|
protected $isTestPayment; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $paymentType; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $merchantName; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $requestedUserInfo = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $priceList = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* PaymentSummary constructor. |
49
|
|
|
* |
50
|
|
|
* @param string $currency |
51
|
|
|
* @param string $paymentType |
52
|
|
|
* @param string $merchantName |
53
|
|
|
* @param array $requestedUserInfo |
54
|
|
|
* @param PriceList[] $priceList |
55
|
|
|
*/ |
56
|
2 |
|
public function __construct( |
57
|
|
|
string $currency, |
58
|
|
|
string $paymentType, |
59
|
|
|
string $merchantName, |
60
|
|
|
array $requestedUserInfo, |
61
|
|
|
array $priceList |
62
|
|
|
) { |
63
|
2 |
|
$this->isValidPaymentType($paymentType); |
64
|
1 |
|
$this->isValidRequestedUserInfo($requestedUserInfo); |
65
|
|
|
|
66
|
1 |
|
$this->currency = $currency; |
67
|
1 |
|
$this->paymentType = $paymentType; |
68
|
1 |
|
$this->merchantName = $merchantName; |
69
|
1 |
|
$this->requestedUserInfo = $requestedUserInfo; |
70
|
1 |
|
$this->priceList = $priceList; |
71
|
1 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param bool $isTestPayment |
75
|
|
|
* |
76
|
|
|
* @return PaymentSummary |
77
|
|
|
*/ |
78
|
1 |
|
public function isTestPayment(bool $isTestPayment): self |
79
|
|
|
{ |
80
|
1 |
|
$this->isTestPayment = $isTestPayment; |
81
|
|
|
|
82
|
1 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $label |
87
|
|
|
* @param string $amount |
88
|
|
|
* |
89
|
|
|
* @return PaymentSummary |
90
|
|
|
*/ |
91
|
1 |
|
public function addPriceList(string $label, string $amount): self |
92
|
|
|
{ |
93
|
1 |
|
$this->priceList[] = new PriceList($label, $amount); |
94
|
|
|
|
95
|
1 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $paymentType |
100
|
|
|
* |
101
|
|
|
* @throws \InvalidArgumentException |
102
|
|
|
*/ |
103
|
2 |
|
private function isValidPaymentType(string $paymentType): void |
104
|
|
|
{ |
105
|
2 |
|
$allowedPaymentType = $this->getAllowedPaymentType(); |
106
|
2 |
|
if (!in_array($paymentType, $allowedPaymentType, true)) { |
107
|
1 |
|
throw new \InvalidArgumentException( |
108
|
1 |
|
'$paymentType must be either ' . implode(', ', $allowedPaymentType) |
109
|
|
|
); |
110
|
|
|
} |
111
|
1 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
2 |
|
private function getAllowedPaymentType(): array |
117
|
|
|
{ |
118
|
|
|
return [ |
119
|
2 |
|
self::PAYMENT_TYPE_FIXED_AMOUNT, |
120
|
2 |
|
self::PAYMENT_TYPE_FLEXIBLE_AMOUNT, |
121
|
|
|
]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param array $requestedUserInfo |
126
|
|
|
* |
127
|
|
|
* @throws \InvalidArgumentException |
128
|
|
|
*/ |
129
|
1 |
|
private function isValidRequestedUserInfo(array $requestedUserInfo): void |
130
|
|
|
{ |
131
|
1 |
|
$allowedUserInfo = $this->getAllowedUserInfo(); |
132
|
1 |
|
foreach ($requestedUserInfo as $userInfo) { |
133
|
1 |
|
if (!in_array($userInfo, $allowedUserInfo, true)) { |
134
|
|
|
throw new \InvalidArgumentException( |
135
|
|
|
"$userInfo is not a valid value. Valid values are " . implode(',', $allowedUserInfo) |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
} |
139
|
1 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
1 |
|
private function getAllowedUserInfo(): array |
145
|
|
|
{ |
146
|
|
|
return [ |
147
|
1 |
|
self::USER_INFO_SHIPPING_ADDRESS, |
148
|
1 |
|
self::USER_INFO_CONTACT_NAME, |
149
|
1 |
|
self::USER_INFO_CONTACT_PHONE, |
150
|
1 |
|
self::USER_INFO_CONTACT_EMAIL, |
151
|
|
|
]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return array |
156
|
|
|
*/ |
157
|
1 |
|
public function jsonSerialize(): array |
158
|
|
|
{ |
159
|
|
|
$json = [ |
160
|
1 |
|
'currency' => $this->currency, |
161
|
1 |
|
'payment_type' => $this->paymentType, |
162
|
1 |
|
'is_test_payment' => $this->isTestPayment, |
163
|
1 |
|
'merchant_name' => $this->merchantName, |
164
|
1 |
|
'requested_user_info' => $this->requestedUserInfo, |
165
|
1 |
|
'price_list' => $this->priceList, |
166
|
|
|
]; |
167
|
|
|
|
168
|
1 |
|
return array_filter($json); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|