1 | <?php declare(strict_types = 1); |
||
2 | |||
3 | namespace SlevomatCsobGateway\Call; |
||
4 | |||
5 | use DateTimeImmutable; |
||
6 | use InvalidArgumentException; |
||
7 | use SlevomatCsobGateway\Api\ApiClient; |
||
8 | use SlevomatCsobGateway\Api\HttpMethod; |
||
9 | use SlevomatCsobGateway\Cart; |
||
10 | use SlevomatCsobGateway\CartItem; |
||
11 | use SlevomatCsobGateway\Crypto\SignatureDataFormatter; |
||
12 | use SlevomatCsobGateway\Language; |
||
13 | use SlevomatCsobGateway\Validator; |
||
14 | use function array_map; |
||
15 | use function base64_encode; |
||
16 | use function sprintf; |
||
17 | |||
18 | class InitPaymentRequest |
||
19 | { |
||
20 | |||
21 | /** @var string */ |
||
22 | private $merchantId; |
||
23 | |||
24 | /** @var string */ |
||
25 | private $orderId; |
||
26 | |||
27 | /** @var PayOperation */ |
||
28 | private $payOperation; |
||
29 | |||
30 | /** @var PayMethod */ |
||
31 | private $payMethod; |
||
32 | |||
33 | /** @var bool */ |
||
34 | private $closePayment; |
||
35 | |||
36 | /** @var string */ |
||
37 | private $returnUrl; |
||
38 | |||
39 | /** @var HttpMethod */ |
||
40 | private $returnMethod; |
||
41 | |||
42 | /** @var Cart */ |
||
43 | private $cart; |
||
44 | |||
45 | /** @var string|null */ |
||
46 | private $merchantData; |
||
47 | |||
48 | /** @var string|null */ |
||
49 | private $customerId; |
||
50 | |||
51 | /** @var Language */ |
||
52 | private $language; |
||
53 | |||
54 | /** @var int|null */ |
||
55 | private $ttlSec; |
||
56 | |||
57 | /** @var int|null */ |
||
58 | private $logoVersion; |
||
59 | |||
60 | /** @var int|null */ |
||
61 | private $colorSchemeVersion; |
||
62 | |||
63 | /** @var DateTimeImmutable|null */ |
||
64 | private $customExpiry; |
||
65 | |||
66 | 2 | public function __construct( |
|
67 | string $merchantId, |
||
68 | string $orderId, |
||
69 | PayOperation $payOperation, |
||
70 | PayMethod $payMethod, |
||
71 | bool $closePayment, |
||
72 | string $returnUrl, |
||
73 | HttpMethod $returnMethod, |
||
74 | Cart $cart, |
||
75 | ?string $merchantData, |
||
76 | ?string $customerId, |
||
77 | Language $language, |
||
78 | ?int $ttlSec = null, |
||
79 | ?int $logoVersion = null, |
||
80 | ?int $colorSchemeVersion = null, |
||
81 | ?DateTimeImmutable $customExpiry = null |
||
82 | ) |
||
83 | { |
||
84 | 2 | Validator::checkOrderId($orderId); |
|
85 | 2 | Validator::checkReturnUrl($returnUrl); |
|
86 | 2 | if ($merchantData !== null) { |
|
87 | 2 | Validator::checkMerchantData($merchantData); |
|
88 | } |
||
89 | 2 | if ($customerId !== null) { |
|
90 | 2 | Validator::checkCustomerId($customerId); |
|
91 | } |
||
92 | 2 | if ($ttlSec !== null) { |
|
93 | 2 | Validator::checkTtlSec($ttlSec); |
|
94 | } |
||
95 | |||
96 | 2 | if ($payOperation->equals(PayOperation::get(PayOperation::CUSTOM_PAYMENT)) && $customExpiry === null) { |
|
97 | throw new InvalidArgumentException(sprintf('Custom expiry parameter is required for custom payment.')); |
||
98 | } |
||
99 | |||
100 | 2 | $this->merchantId = $merchantId; |
|
101 | 2 | $this->orderId = $orderId; |
|
102 | 2 | $this->payOperation = $payOperation; |
|
103 | 2 | $this->payMethod = $payMethod; |
|
104 | 2 | $this->closePayment = $closePayment; |
|
105 | 2 | $this->returnUrl = $returnUrl; |
|
106 | 2 | $this->returnMethod = $returnMethod; |
|
107 | 2 | $this->cart = $cart; |
|
108 | 2 | $this->merchantData = $merchantData; |
|
109 | 2 | $this->customerId = $customerId; |
|
110 | 2 | $this->language = $language; |
|
111 | 2 | $this->ttlSec = $ttlSec; |
|
112 | 2 | $this->logoVersion = $logoVersion; |
|
113 | 2 | $this->colorSchemeVersion = $colorSchemeVersion; |
|
114 | 2 | $this->customExpiry = $customExpiry; |
|
115 | 2 | } |
|
116 | |||
117 | 1 | public function send(ApiClient $apiClient): PaymentResponse |
|
118 | { |
||
119 | 1 | $price = $this->cart->getCurrentPrice(); |
|
120 | |||
121 | $requestData = [ |
||
122 | 1 | 'merchantId' => $this->merchantId, |
|
123 | 1 | 'orderNo' => $this->orderId, |
|
124 | 1 | 'payOperation' => $this->payOperation->getValue(), |
|
125 | 1 | 'payMethod' => $this->payMethod->getValue(), |
|
126 | 1 | 'totalAmount' => $price->getAmount(), |
|
127 | 1 | 'currency' => $price->getCurrency()->getValue(), |
|
128 | 1 | 'closePayment' => $this->closePayment, |
|
129 | 1 | 'returnUrl' => $this->returnUrl, |
|
130 | 1 | 'returnMethod' => $this->returnMethod->getValue(), |
|
131 | 'cart' => array_map(static function (CartItem $cartItem) { |
||
132 | $cartItemValues = [ |
||
133 | 1 | 'name' => $cartItem->getName(), |
|
134 | 1 | 'quantity' => $cartItem->getQuantity(), |
|
135 | 1 | 'amount' => $cartItem->getAmount(), |
|
136 | ]; |
||
137 | |||
138 | 1 | if ($cartItem->getDescription() !== null) { |
|
139 | 1 | $cartItemValues['description'] = $cartItem->getDescription(); |
|
140 | } |
||
141 | |||
142 | 1 | return $cartItemValues; |
|
143 | 1 | }, $this->cart->getItems()), |
|
144 | 1 | 'language' => $this->language->getValue(), |
|
145 | ]; |
||
146 | |||
147 | 1 | if ($this->merchantData !== null) { |
|
148 | 1 | $requestData['merchantData'] = base64_encode($this->merchantData); |
|
149 | } |
||
150 | |||
151 | 1 | if ($this->customerId !== null) { |
|
152 | 1 | $requestData['customerId'] = $this->customerId; |
|
153 | } |
||
154 | |||
155 | 1 | if ($this->ttlSec !== null) { |
|
156 | 1 | $requestData['ttlSec'] = $this->ttlSec; |
|
157 | } |
||
158 | |||
159 | 1 | if ($this->logoVersion !== null) { |
|
160 | 1 | $requestData['logoVersion'] = $this->logoVersion; |
|
161 | } |
||
162 | |||
163 | 1 | if ($this->colorSchemeVersion !== null) { |
|
164 | 1 | $requestData['colorSchemeVersion'] = $this->colorSchemeVersion; |
|
165 | } |
||
166 | |||
167 | 1 | if ($this->customExpiry !== null) { |
|
168 | $requestData['customExpiry'] = $this->customExpiry->format('YmdHis'); |
||
169 | } |
||
170 | |||
171 | 1 | $response = $apiClient->post( |
|
172 | 1 | 'payment/init', |
|
173 | $requestData, |
||
174 | 1 | new SignatureDataFormatter([ |
|
175 | 1 | 'merchantId' => null, |
|
176 | 'orderNo' => null, |
||
177 | 'dttm' => null, |
||
178 | 'payOperation' => null, |
||
179 | 'payMethod' => null, |
||
180 | 'totalAmount' => null, |
||
181 | 'currency' => null, |
||
182 | 'closePayment' => null, |
||
183 | 'returnUrl' => null, |
||
184 | 'returnMethod' => null, |
||
185 | 'cart' => [ |
||
186 | [ |
||
187 | 'name' => null, |
||
188 | 'quantity' => null, |
||
189 | 'amount' => null, |
||
190 | 'description' => null, |
||
191 | ], |
||
192 | ], |
||
193 | 'merchantData' => null, |
||
194 | 'customerId' => null, |
||
195 | 'language' => null, |
||
196 | 'ttlSec' => null, |
||
197 | 'logoVersion' => null, |
||
198 | 'colorSchemeVersion' => null, |
||
199 | 'customExpiry' => null, |
||
200 | ]), |
||
201 | 1 | new SignatureDataFormatter([ |
|
202 | 1 | 'payId' => null, |
|
203 | 'dttm' => null, |
||
204 | 'resultCode' => null, |
||
205 | 'resultMessage' => null, |
||
206 | 'paymentStatus' => null, |
||
207 | 'authCode' => null, |
||
208 | 'customerCode' => null, |
||
209 | ]) |
||
210 | ); |
||
211 | |||
212 | 1 | $data = $response->getData(); |
|
213 | |||
214 | 1 | return new InitPaymentResponse( |
|
215 | 1 | $data['payId'], |
|
216 | 1 | DateTimeImmutable::createFromFormat('YmdHis', $data['dttm']), |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
217 | 1 | ResultCode::get($data['resultCode']), |
|
218 | 1 | $data['resultMessage'], |
|
219 | 1 | isset($data['paymentStatus']) ? PaymentStatus::get($data['paymentStatus']) : null, |
|
220 | 1 | $data['authCode'] ?? null, |
|
221 | 1 | null, |
|
222 | 1 | $data['customerCode'] ?? null |
|
223 | ); |
||
224 | } |
||
225 | |||
226 | } |
||
227 |