Completed
Push — master ( 955097...f4414e )
by Jan
08:34
created

StartApplePayRequest::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 4
crap 1
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,
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 1
			ResultCode::get($data['resultCode']),
89 1
			$data['resultMessage'],
90 1
			isset($data['paymentStatus']) ? PaymentStatus::get($data['paymentStatus']) : null
91
		);
92
	}
93
94
}
95