1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* MIT License |
5
|
|
|
* For full license information, please view the LICENSE file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace SprykerEco\Zed\CrefoPayApi\Business\Response\Validator; |
9
|
|
|
|
10
|
|
|
use SprykerEco\Service\CrefoPayApi\CrefoPayApiServiceInterface; |
11
|
|
|
use SprykerEco\Zed\CrefoPayApi\CrefoPayApiConfig; |
12
|
|
|
use SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponseInterface; |
13
|
|
|
|
14
|
|
|
class CrefoPayApiResponseValidator implements CrefoPayApiResponseValidatorInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \SprykerEco\Service\CrefoPayApi\CrefoPayApiServiceInterface |
18
|
|
|
*/ |
19
|
|
|
protected $service; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \SprykerEco\Zed\CrefoPayApi\CrefoPayApiConfig |
23
|
|
|
*/ |
24
|
|
|
protected $config; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param \SprykerEco\Service\CrefoPayApi\CrefoPayApiServiceInterface $service |
28
|
|
|
* @param \SprykerEco\Zed\CrefoPayApi\CrefoPayApiConfig $config |
29
|
|
|
*/ |
30
|
|
|
public function __construct( |
31
|
|
|
CrefoPayApiServiceInterface $service, |
32
|
|
|
CrefoPayApiConfig $config |
33
|
|
|
) { |
34
|
|
|
$this->service = $service; |
35
|
|
|
$this->config = $config; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param \SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponseInterface $response |
40
|
|
|
* @param array|null $responseData |
41
|
|
|
* |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
|
|
public function validateResponse( |
45
|
|
|
CrefoPayApiGuzzleResponseInterface $response, |
46
|
|
|
?array $responseData |
47
|
|
|
): bool { |
48
|
|
|
return $responseData !== null |
49
|
|
|
&& $this->isResultCodeSuccess($responseData) |
50
|
|
|
&& $this->validateMac($response); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param array $responseData |
55
|
|
|
* |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
protected function isResultCodeSuccess(array $responseData): bool |
59
|
|
|
{ |
60
|
|
|
$resultCode = $responseData[$this->config->getApiResponseFieldResultCode()] ?? null; |
61
|
|
|
|
62
|
|
|
return isset($resultCode) && ($resultCode === 0 || $resultCode === 1); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param \SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponseInterface $response |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
protected function validateMac(CrefoPayApiGuzzleResponseInterface $response): bool |
71
|
|
|
{ |
72
|
|
|
$mac = $response->getHeader($this->config->getApiHeaderMac()); |
73
|
|
|
if ($mac === null) { |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->service |
78
|
|
|
->validateMac([$response->getResponseBody()], $mac); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|