Completed
Push — master ( 069e15...978af9 )
by Jan
02:19
created

ProcessPaymentRequest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 89.47%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
lcom 1
cbo 6
dl 0
loc 59
ccs 17
cts 19
cp 0.8947
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B send() 0 33 2
1
<?php declare(strict_types = 1);
2
3
namespace SlevomatCsobGateway\Call;
4
5
use SlevomatCsobGateway\Api\ApiClient;
6
use SlevomatCsobGateway\Api\InvalidPaymentException;
7
use SlevomatCsobGateway\Api\Response;
8
use SlevomatCsobGateway\Api\ResponseCode;
9
use SlevomatCsobGateway\Crypto\SignatureDataFormatter;
10
use SlevomatCsobGateway\Validator;
11
12
class ProcessPaymentRequest
13
{
14
15
	/**
16
	 * @var string
17
	 */
18
	private $merchantId;
19
20
	/**
21
	 * @var string
22
	 */
23
	private $payId;
24
25 2
	public function __construct(
26
		string $merchantId,
27
		string $payId
28
	)
29
	{
30 2
		Validator::checkPayId($payId);
31
32 2
		$this->merchantId = $merchantId;
33 2
		$this->payId = $payId;
34 2
	}
35
36 1
	public function send(ApiClient $apiClient): ProcessPaymentResponse
37
	{
38 1
		$response = $apiClient->get(
39 1
			'payment/process/{merchantId}/{payId}/{dttm}/{signature}',
40
			[
41 1
				'merchantId' => $this->merchantId,
42 1
				'payId' => $this->payId,
43
			],
44 1
			new SignatureDataFormatter([
45 1
				'merchantId' => null,
46
				'payId' => null,
47
				'dttm' => null,
48
			]),
49 1
			new SignatureDataFormatter([
50 1
				'payId' => null,
51
				'dttm' => null,
52
				'resultCode' => null,
53
				'resultMessage' => null,
54
				'paymentStatus' => null,
55
				'authCode' => null,
56
			]),
57 1
			function (Response $response): void {
58
				// This handles edge case when provided payId is missing or already expired on gateway
59
				// In this case gateway responds with HTTP 200 and HTML content. Bad API.
60
				// See https://github.com/csob/paymentgateway/issues/135
61
				if ($response->getResponseCode()->equalsValue(ResponseCode::S200_OK)) {
62
					throw new InvalidPaymentException($this, $response, $this->payId);
63
				}
64 1
			}
65
		);
66
67 1
		return new ProcessPaymentResponse($response->getHeaders()['Location']);
68
	}
69
70
}
71