Passed
Pull Request — master (#39)
by Jan
08:36
created

StartApplePayRequest::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
dl 0
loc 13
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 4
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
	public function __construct(
37
		string $merchantId,
38
		string $payId,
39
		array $payload,
40
		?int $totalAmount
41
	)
42
	{
43
		Validator::checkPayId($payId);
44
45
		$this->merchantId = $merchantId;
46
		$this->payId = $payId;
47
		$this->payload = $payload;
48
		$this->totalAmount = $totalAmount;
49
	}
50
51
	public function send(ApiClient $apiClient): PaymentResponse
52
	{
53
		$requestData = [
54
			'merchantId' => $this->merchantId,
55
			'payId' => $this->payId,
56
			'payload' => base64_encode(Json::encode($this->payload)),
57
		];
58
59
		if ($this->totalAmount !== null) {
60
			$requestData['totalAmount'] = $this->totalAmount;
61
		}
62
63
		$response = $apiClient->post(
64
			'applepay/start',
65
			$requestData,
66
			new SignatureDataFormatter([
67
				'merchantId' => null,
68
				'payId' => null,
69
				'payload' => null,
70
				'totalAmount' => null,
71
				'dttm' => null,
72
			]),
73
			new SignatureDataFormatter([
74
				'payId' => null,
75
				'dttm' => null,
76
				'resultCode' => null,
77
				'resultMessage' => null,
78
				'paymentStatus' => null,
79
			])
80
		);
81
82
		$data = $response->getData();
83
		$responseDateTime = DateTimeImmutable::createFromFormat('YmdHis', $data['dttm']);
84
85
		return new PaymentResponse(
86
			$data['payId'],
87
			$responseDateTime,
0 ignored issues
show
Bug introduced by
It seems like $responseDateTime can also be of type false; however, parameter $responseDateTime of SlevomatCsobGateway\Call...Response::__construct() does only seem to accept DateTimeImmutable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
			/** @scrutinizer ignore-type */ $responseDateTime,
Loading history...
88
			ResultCode::get($data['resultCode']),
89
			$data['resultMessage'],
90
			isset($data['paymentStatus']) ? PaymentStatus::get($data['paymentStatus']) : null
91
		);
92
	}
93
94
}
95