1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace SlevomatCsobGateway\Call; |
4
|
|
|
|
5
|
|
|
use DateTimeImmutable; |
6
|
|
|
use SlevomatCsobGateway\Api\ApiClient; |
7
|
|
|
use SlevomatCsobGateway\Crypto\SignatureDataFormatter; |
8
|
|
|
use SlevomatCsobGateway\Price; |
9
|
|
|
use SlevomatCsobGateway\Validator; |
10
|
|
|
|
11
|
|
|
class OneclickInitPaymentRequest |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $merchantId; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $origPayId; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $orderId; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Price|null |
31
|
|
|
*/ |
32
|
|
|
private $price; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string|null |
36
|
|
|
*/ |
37
|
|
|
private $description; |
38
|
|
|
|
39
|
2 |
|
public function __construct( |
40
|
|
|
string $merchantId, |
41
|
|
|
string $origPayId, |
42
|
|
|
string $orderId, |
43
|
|
|
Price $price = null, |
44
|
|
|
string $description = null |
45
|
|
|
) |
46
|
|
|
{ |
47
|
2 |
|
Validator::checkPayId($origPayId); |
48
|
2 |
|
Validator::checkOrderId($orderId); |
49
|
2 |
|
if ($description !== null) { |
50
|
2 |
|
Validator::checkDescription($description); |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
$this->merchantId = $merchantId; |
54
|
2 |
|
$this->origPayId = $origPayId; |
55
|
2 |
|
$this->orderId = $orderId; |
56
|
2 |
|
$this->price = $price; |
57
|
2 |
|
$this->description = $description; |
58
|
2 |
|
} |
59
|
|
|
|
60
|
1 |
|
public function send(ApiClient $apiClient): PaymentResponse |
61
|
|
|
{ |
62
|
|
|
$requestData = [ |
63
|
1 |
|
'merchantId' => $this->merchantId, |
64
|
1 |
|
'origPayId' => $this->origPayId, |
65
|
1 |
|
'orderNo' => $this->orderId, |
66
|
|
|
]; |
67
|
|
|
|
68
|
1 |
|
if ($this->price !== null) { |
69
|
1 |
|
$requestData['totalAmount'] = $this->price->getAmount(); |
70
|
1 |
|
$requestData['currency'] = $this->price->getCurrency()->getValue(); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
if ($this->description !== null) { |
74
|
1 |
|
$requestData['description'] = $this->description; |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
$response = $apiClient->post( |
78
|
1 |
|
'payment/oneclick/init', |
79
|
|
|
$requestData, |
80
|
1 |
|
new SignatureDataFormatter([ |
81
|
1 |
|
'merchantId' => null, |
82
|
|
|
'origPayId' => null, |
83
|
|
|
'orderNo' => null, |
84
|
|
|
'dttm' => null, |
85
|
|
|
'totalAmount' => null, |
86
|
|
|
'currency' => null, |
87
|
|
|
'description' => null, |
88
|
|
|
]), |
89
|
1 |
|
new SignatureDataFormatter([ |
90
|
1 |
|
'payId' => null, |
91
|
|
|
'dttm' => null, |
92
|
|
|
'resultCode' => null, |
93
|
|
|
'resultMessage' => null, |
94
|
|
|
'paymentStatus' => null, |
95
|
|
|
]) |
96
|
|
|
); |
97
|
|
|
|
98
|
1 |
|
$data = $response->getData(); |
99
|
|
|
|
100
|
1 |
|
return new PaymentResponse( |
101
|
1 |
|
$data['payId'], |
102
|
1 |
|
DateTimeImmutable::createFromFormat('YmdHis', $data['dttm']), |
103
|
1 |
|
new ResultCode($data['resultCode']), |
104
|
1 |
|
$data['resultMessage'], |
105
|
1 |
|
isset($data['paymentStatus']) ? new PaymentStatus($data['paymentStatus']) : null |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|