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

InitOneClickPaymentRequest::send()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 48
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 48
ccs 23
cts 23
cp 1
rs 9.36
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4
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 InitOneClickPaymentRequest
15
{
16
17
	/** @var string */
18
	private $merchantId;
19
20
	/** @var string */
21
	private $origPayId;
22
23
	/** @var string */
24
	private $orderId;
25
26
	/** @var Price|null */
27
	private $price;
28
29
	/** @var string|null */
30
	private $description;
31
32
	/** @var string */
33
	private $clientIp;
34
35 2
	public function __construct(
36
		string $merchantId,
37
		string $origPayId,
38
		string $orderId,
39
		string $clientIp,
40
		?Price $price = null,
41
		?string $description = null
42
	)
43
	{
44 2
		Validator::checkPayId($origPayId);
45 2
		Validator::checkOrderId($orderId);
46 2
		if ($description !== null) {
47 2
			Validator::checkDescription($description);
48
		}
49
50 2
		$this->merchantId = $merchantId;
51 2
		$this->origPayId = $origPayId;
52 2
		$this->orderId = $orderId;
53 2
		$this->clientIp = $clientIp;
54 2
		$this->price = $price;
55 2
		$this->description = $description;
56 2
	}
57
58 1
	public function send(ApiClient $apiClient): PaymentResponse
59
	{
60
		$requestData = [
61 1
			'merchantId' => $this->merchantId,
62 1
			'origPayId' => $this->origPayId,
63 1
			'orderNo' => $this->orderId,
64 1
			'clientIp' => $this->clientIp,
65
		];
66
67 1
		if ($this->price !== null) {
68 1
			$requestData['totalAmount'] = $this->price->getAmount();
69 1
			$requestData['currency'] = $this->price->getCurrency()->getValue();
70
		}
71
72 1
		if ($this->description !== null) {
73 1
			$requestData['description'] = $this->description;
74
		}
75
76 1
		$response = $apiClient->post(
77 1
			'oneclick/init',
78
			$requestData,
79 1
			new SignatureDataFormatter([
80 1
				'merchantId' => null,
81
				'origPayId' => null,
82
				'orderNo' => null,
83
				'dttm' => null,
84
				'clientIp' => 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']),
0 ignored issues
show
Bug introduced by
It seems like DateTimeImmutable::creat...YmdHis', $data['dttm']) 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

102
			/** @scrutinizer ignore-type */ DateTimeImmutable::createFromFormat('YmdHis', $data['dttm']),
Loading history...
103 1
			ResultCode::get($data['resultCode']),
104 1
			$data['resultMessage'],
105 1
			isset($data['paymentStatus']) ? PaymentStatus::get($data['paymentStatus']) : null
106
		);
107
	}
108
109
}
110