|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace SlevomatCsobGateway\Call\ApplePay; |
|
4
|
|
|
|
|
5
|
|
|
use DateTimeImmutable; |
|
6
|
|
|
use Nette\Utils\Json; |
|
7
|
|
|
use SlevomatCsobGateway\Api\ApiClient; |
|
8
|
|
|
use SlevomatCsobGateway\Call\PaymentResponse; |
|
9
|
|
|
use SlevomatCsobGateway\Call\PaymentStatus; |
|
10
|
|
|
use SlevomatCsobGateway\Call\ResultCode; |
|
11
|
|
|
use SlevomatCsobGateway\Crypto\SignatureDataFormatter; |
|
12
|
|
|
use SlevomatCsobGateway\Validator; |
|
13
|
|
|
use function base64_encode; |
|
14
|
|
|
|
|
15
|
|
|
class StartApplePayRequest |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** @var string */ |
|
19
|
|
|
private $merchantId; |
|
20
|
|
|
|
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
private $payId; |
|
23
|
|
|
|
|
24
|
|
|
/** @var array|mixed[] */ |
|
25
|
|
|
private $payload; |
|
26
|
|
|
|
|
27
|
|
|
/** @var int|null */ |
|
28
|
|
|
private $totalAmount; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param string $merchantId |
|
32
|
|
|
* @param string $payId |
|
33
|
|
|
* @param mixed[] $payload |
|
34
|
|
|
* @param int|null $totalAmount |
|
35
|
|
|
*/ |
|
36
|
2 |
|
public function __construct( |
|
37
|
|
|
string $merchantId, |
|
38
|
|
|
string $payId, |
|
39
|
|
|
array $payload, |
|
40
|
|
|
?int $totalAmount |
|
41
|
|
|
) |
|
42
|
|
|
{ |
|
43
|
2 |
|
Validator::checkPayId($payId); |
|
44
|
|
|
|
|
45
|
2 |
|
$this->merchantId = $merchantId; |
|
46
|
2 |
|
$this->payId = $payId; |
|
47
|
2 |
|
$this->payload = $payload; |
|
48
|
2 |
|
$this->totalAmount = $totalAmount; |
|
49
|
2 |
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
public function send(ApiClient $apiClient): PaymentResponse |
|
52
|
|
|
{ |
|
53
|
|
|
$requestData = [ |
|
54
|
1 |
|
'merchantId' => $this->merchantId, |
|
55
|
1 |
|
'payId' => $this->payId, |
|
56
|
1 |
|
'payload' => base64_encode(Json::encode($this->payload)), |
|
57
|
|
|
]; |
|
58
|
|
|
|
|
59
|
1 |
|
if ($this->totalAmount !== null) { |
|
60
|
|
|
$requestData['totalAmount'] = $this->totalAmount; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
$response = $apiClient->post( |
|
64
|
1 |
|
'applepay/start', |
|
65
|
|
|
$requestData, |
|
66
|
1 |
|
new SignatureDataFormatter([ |
|
67
|
1 |
|
'merchantId' => null, |
|
68
|
|
|
'payId' => null, |
|
69
|
|
|
'payload' => null, |
|
70
|
|
|
'totalAmount' => null, |
|
71
|
|
|
'dttm' => null, |
|
72
|
|
|
]), |
|
73
|
1 |
|
new SignatureDataFormatter([ |
|
74
|
1 |
|
'payId' => null, |
|
75
|
|
|
'dttm' => null, |
|
76
|
|
|
'resultCode' => null, |
|
77
|
|
|
'resultMessage' => null, |
|
78
|
|
|
'paymentStatus' => null, |
|
79
|
|
|
]) |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
1 |
|
$data = $response->getData(); |
|
83
|
1 |
|
$responseDateTime = DateTimeImmutable::createFromFormat('YmdHis', $data['dttm']); |
|
84
|
|
|
|
|
85
|
1 |
|
return new PaymentResponse( |
|
86
|
1 |
|
$data['payId'], |
|
87
|
|
|
$responseDateTime, |
|
|
|
|
|
|
88
|
1 |
|
ResultCode::get($data['resultCode']), |
|
89
|
1 |
|
$data['resultMessage'], |
|
90
|
1 |
|
isset($data['paymentStatus']) ? PaymentStatus::get($data['paymentStatus']) : null |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|