Completed
Push — master ( 7d5f81...069e15 )
by Jan
02:05
created

BasicCheckoutRequest::send()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 47
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 47
ccs 19
cts 19
cp 1
rs 9.0303
c 0
b 0
f 0
cc 2
eloc 36
nc 1
nop 1
crap 2
1
<?php declare(strict_types = 1);
2
3
namespace SlevomatCsobGateway\Call\Masterpass;
4
5
use DateTimeImmutable;
6
use SlevomatCsobGateway\Api\ApiClient;
7
use SlevomatCsobGateway\Call\PaymentStatus;
8
use SlevomatCsobGateway\Call\ResultCode;
9
use SlevomatCsobGateway\Crypto\SignatureDataFormatter;
10
use SlevomatCsobGateway\Validator;
11
12
class BasicCheckoutRequest
13
{
14
15
	/**
16
	 * @var string
17
	 */
18
	private $merchantId;
19
20
	/**
21
	 * @var string
22
	 */
23
	private $payId;
24
25
	/** @var string */
26
	private $callbackUrl;
27
28 2
	public function __construct(
29
		string $merchantId,
30
		string $payId,
31
		string $callbackUrl
32
	)
33
	{
34 2
		Validator::checkPayId($payId);
35 2
		Validator::checkReturnUrl($callbackUrl);
36
37 2
		$this->merchantId = $merchantId;
38 2
		$this->payId = $payId;
39 2
		$this->callbackUrl = $callbackUrl;
40 2
	}
41
42 1
	public function send(ApiClient $apiClient): CheckoutResponse
43
	{
44
		$requestData = [
45 1
			'merchantId' => $this->merchantId,
46 1
			'payId' => $this->payId,
47 1
			'callbackUrl' => $this->callbackUrl,
48
		];
49
50 1
		$response = $apiClient->post(
51 1
			'masterpass/basic/checkout',
52 1
			$requestData,
53 1
			new SignatureDataFormatter([
54 1
				'merchantId' => null,
55
				'payId' => null,
56
				'dttm' => null,
57
				'callbackUrl' => null,
58
			]),
59 1
			new SignatureDataFormatter([
60 1
				'payId' => null,
61
				'dttm' => null,
62
				'resultCode' => null,
63
				'resultMessage' => null,
64
				'paymentStatus' => null,
65
				'lightboxParams' => [
66
					'requestToken' => null,
67
					'callbackUrl' => null,
68
					'merchantCheckoutId' => null,
69
					'allowedCardTypes' => null,
70
					'suppressShippingAddressEnable' => null,
71
					'loyaltyEnabled' => null,
72
					'version' => null,
73
					'shippingLocationProfile' => null,
74
				],
75
			])
76
		);
77
78 1
		$data = $response->getData();
79
80 1
		return new CheckoutResponse(
81 1
			$data['payId'],
82 1
			DateTimeImmutable::createFromFormat('YmdHis', $data['dttm']),
83 1
			ResultCode::get($data['resultCode']),
84 1
			$data['resultMessage'],
85 1
			isset($data['paymentStatus']) ? PaymentStatus::get($data['paymentStatus']) : null,
86 1
			$data['lightboxParams'] ?? null
87
		);
88
	}
89
90
}
91