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