Completed
Pull Request — master (#32)
by Jan
03:24
created

PaymentButtonRequest::send()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 49
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 3

Importance

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