1 | <?php declare(strict_types = 1); |
||
2 | |||
3 | namespace SlevomatCsobGateway\Call\Button; |
||
4 | |||
5 | use DateTimeImmutable; |
||
6 | use SlevomatCsobGateway\Api\ApiClient; |
||
7 | use SlevomatCsobGateway\Api\HttpMethod; |
||
8 | use SlevomatCsobGateway\Call\PaymentStatus; |
||
9 | use SlevomatCsobGateway\Call\ResultCode; |
||
10 | use SlevomatCsobGateway\Crypto\SignatureDataFormatter; |
||
11 | use SlevomatCsobGateway\Language; |
||
12 | use SlevomatCsobGateway\Price; |
||
13 | use SlevomatCsobGateway\Validator; |
||
14 | |||
15 | class PaymentButtonRequest |
||
16 | { |
||
17 | |||
18 | /** @var string */ |
||
19 | private $merchantId; |
||
20 | |||
21 | /** @var string */ |
||
22 | private $orderId; |
||
23 | |||
24 | /** @var string */ |
||
25 | private $clientIp; |
||
26 | |||
27 | /** @var Price */ |
||
28 | private $totalPrice; |
||
29 | |||
30 | /** @var string */ |
||
31 | private $returnUrl; |
||
32 | |||
33 | /** @var HttpMethod */ |
||
34 | private $returnMethod; |
||
35 | |||
36 | /** @var PaymentButtonBrand */ |
||
37 | private $brand; |
||
38 | |||
39 | /** @var string|null */ |
||
40 | private $merchantData; |
||
41 | |||
42 | /** @var Language */ |
||
43 | private $language; |
||
44 | |||
45 | 2 | public function __construct( |
|
46 | string $merchantId, |
||
47 | string $orderId, |
||
48 | string $clientIp, |
||
49 | Price $totalPrice, |
||
50 | string $returnUrl, |
||
51 | HttpMethod $returnMethod, |
||
52 | PaymentButtonBrand $brand, |
||
53 | ?string $merchantData, |
||
54 | Language $language |
||
55 | ) |
||
56 | { |
||
57 | 2 | Validator::checkReturnUrl($returnUrl); |
|
58 | 2 | if ($merchantData !== null) { |
|
59 | Validator::checkMerchantData($merchantData); |
||
60 | } |
||
61 | |||
62 | 2 | $this->merchantId = $merchantId; |
|
63 | 2 | $this->orderId = $orderId; |
|
64 | 2 | $this->clientIp = $clientIp; |
|
65 | 2 | $this->totalPrice = $totalPrice; |
|
66 | 2 | $this->returnUrl = $returnUrl; |
|
67 | 2 | $this->returnMethod = $returnMethod; |
|
68 | 2 | $this->brand = $brand; |
|
69 | 2 | $this->merchantData = $merchantData; |
|
70 | 2 | $this->language = $language; |
|
71 | 2 | } |
|
72 | |||
73 | 1 | public function send(ApiClient $apiClient): PaymentButtonResponse |
|
74 | { |
||
75 | $requestData = [ |
||
76 | 1 | 'merchantId' => $this->merchantId, |
|
77 | 1 | 'orderNo' => $this->orderId, |
|
78 | 1 | 'clientIp' => $this->clientIp, |
|
79 | 1 | 'totalAmount' => $this->totalPrice->getAmount(), |
|
80 | 1 | 'currency' => $this->totalPrice->getCurrency()->getValue(), |
|
81 | 1 | 'returnUrl' => $this->returnUrl, |
|
82 | 1 | 'returnMethod' => $this->returnMethod->getValue(), |
|
83 | 1 | 'brand' => $this->brand->getValue(), |
|
84 | 1 | 'language' => $this->language->getValue(), |
|
85 | ]; |
||
86 | |||
87 | 1 | if ($this->merchantData !== null) { |
|
88 | $requestData['merchantData'] = $this->merchantData; |
||
89 | } |
||
90 | |||
91 | 1 | $response = $apiClient->post( |
|
92 | 1 | 'button/init', |
|
93 | $requestData, |
||
94 | 1 | new SignatureDataFormatter([ |
|
95 | 1 | 'merchantId' => null, |
|
96 | 'orderNo' => null, |
||
97 | 'dttm' => null, |
||
98 | 'clientIp' => null, |
||
99 | 'totalAmount' => null, |
||
100 | 'currency' => null, |
||
101 | 'returnUrl' => null, |
||
102 | 'returnMethod' => null, |
||
103 | 'brand' => null, |
||
104 | 'merchantData' => null, |
||
105 | 'language' => null, |
||
106 | ]), |
||
107 | 1 | new SignatureDataFormatter([ |
|
108 | 1 | 'payId' => null, |
|
109 | 'dttm' => null, |
||
110 | 'resultCode' => null, |
||
111 | 'resultMessage' => null, |
||
112 | 'paymentStatus' => null, |
||
113 | 'redirect' => [ |
||
114 | 'method' => null, |
||
115 | 'url' => null, |
||
116 | 'params' => null, |
||
117 | ], |
||
118 | ]) |
||
119 | ); |
||
120 | |||
121 | 1 | $data = $response->getData(); |
|
122 | |||
123 | 1 | $redirectUrl = null; |
|
124 | 1 | $redirectMethod = null; |
|
125 | 1 | $redirectParams = []; |
|
126 | 1 | if (isset($data['redirect'])) { |
|
127 | 1 | $redirectUrl = $data['redirect']['url']; |
|
128 | 1 | $redirectMethod = HttpMethod::get($data['redirect']['method']); |
|
129 | 1 | $redirectParams = $data['redirect']['params'] ?? null; |
|
130 | } |
||
131 | |||
132 | 1 | return new PaymentButtonResponse( |
|
133 | 1 | $data['payId'], |
|
134 | 1 | DateTimeImmutable::createFromFormat('YmdHis', $data['dttm']), |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
135 | 1 | ResultCode::get($data['resultCode']), |
|
136 | 1 | $data['resultMessage'], |
|
137 | 1 | isset($data['paymentStatus']) ? PaymentStatus::get($data['paymentStatus']) : null, |
|
138 | $redirectMethod, |
||
139 | $redirectUrl, |
||
140 | $redirectParams |
||
141 | ); |
||
142 | } |
||
143 | |||
144 | } |
||
145 |