Completed
Pull Request — master (#16)
by Josef
02:58
created

InitPaymentRequest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
dl 0
loc 99
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 2
B send() 0 48 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\Cart;
11
use SlevomatCsobGateway\Crypto\SignatureDataFormatter;
12
use SlevomatCsobGateway\Validator;
13
14
class InitPaymentRequest
15
{
16
17
	/**
18
	 * @var string
19
	 */
20
	private $merchantId;
21
22
	/**
23
	 * @var string
24
	 */
25
	private $origPayId;
26
27
	/**
28
	 * @var string
29
	 */
30
	private $orderId;
31
32
	/**
33
	 * @var Cart|null
34
	 */
35
	private $cart;
36
37
	/**
38
	 * @var string|null
39
	 */
40
	private $description;
41
42
	public function __construct(
43
		string $merchantId,
44
		string $origPayId,
45
		string $orderId,
46
		Cart $cart = null,
47
		string $description = null
48
	)
49
	{
50
		Validator::checkPayId($origPayId);
51
		Validator::checkOrderId($orderId);
52
		if ($description !== null) {
53
			Validator::checkDescription($description);
54
		}
55
56
		$this->merchantId = $merchantId;
57
		$this->origPayId = $origPayId;
58
		$this->orderId = $orderId;
59
		$this->cart = $cart;
60
		$this->description = $description;
61
	}
62
63
	public function send(ApiClient $apiClient): PaymentResponse
64
	{
65
		$requestData = [
66
			'merchantId' => $this->merchantId,
67
			'origPayId' => $this->origPayId,
68
			'orderNo' => $this->orderId,
69
		];
70
71
		if ($this->cart !== null) {
72
			$requestData['totalAmount'] = $this->cart->countTotalAmount();
73
			$requestData['currency'] = $this->cart->getCurrency()->getValue();
74
		}
75
76
		if ($this->description !== null) {
77
			$requestData['description'] = $this->description;
78
		}
79
80
		$response = $apiClient->post(
81
			'payment/oneclick/init',
82
			$requestData,
83
			new SignatureDataFormatter([
84
				'merchantId' => null,
85
				'origPayId' => null,
86
				'orderNo' => null,
87
				'dttm' => null,
88
				'totalAmount' => null,
89
				'currency' => null,
90
				'description' => null,
91
			]),
92
			new SignatureDataFormatter([
93
				'payId' => null,
94
				'dttm' => null,
95
				'resultCode' => null,
96
				'resultMessage' => null,
97
				'paymentStatus' => null,
98
			])
99
		);
100
101
		$data = $response->getData();
102
103
		return new PaymentResponse(
104
			$data['payId'],
105
			DateTimeImmutable::createFromFormat('YmdHis', $data['dttm']),
106
			new ResultCode($data['resultCode']),
107
			$data['resultMessage'],
108
			isset($data['paymentStatus']) ? new PaymentStatus($data['paymentStatus']) : null
109
		);
110
	}
111
112
}
113