1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Pixidos package. |
5
|
|
|
* |
6
|
|
|
* (c) Ondra Votava <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
declare(strict_types=1); |
14
|
|
|
|
15
|
|
|
namespace Pixidos\GPWebPay\Factory; |
16
|
|
|
|
17
|
|
|
use Pixidos\GPWebPay\Config\PaymentConfigProvider; |
18
|
|
|
use Pixidos\GPWebPay\Data\Response; |
19
|
|
|
use Pixidos\GPWebPay\Data\ResponseInterface; |
20
|
|
|
use Pixidos\GPWebPay\Enum\Param; |
21
|
|
|
use Pixidos\GPWebPay\Param\ResponseParam; |
22
|
|
|
use ReflectionClass; |
23
|
|
|
|
24
|
|
|
class ResponseFactory |
25
|
|
|
{ |
26
|
6 |
|
public function __construct( |
27
|
|
|
private readonly PaymentConfigProvider $configProvider |
28
|
|
|
) { |
29
|
6 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array<string, string> $params |
33
|
|
|
*/ |
34
|
13 |
|
public function create(array $params): ResponseInterface |
35
|
|
|
{ |
36
|
13 |
|
$md = $this->getStringValue(Param::MD, $params); |
37
|
13 |
|
$gateway = $this->configProvider->getDefaultGateway(); |
38
|
|
|
|
39
|
13 |
|
if ('' !== $md) { |
40
|
11 |
|
$key = explode('|', $md, 2); |
41
|
11 |
|
if ('' !== $key[0]) { |
42
|
10 |
|
$gateway = $key[0]; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
13 |
|
$response = new Response( |
47
|
13 |
|
$this->getStringValue(Param::OPERATION, $params), |
48
|
13 |
|
$this->getStringValue(Param::ORDERNUMBER, $params), |
49
|
13 |
|
$this->getStringValue(Param::MERORDERNUM, $params), |
50
|
13 |
|
$md, |
51
|
13 |
|
$this->getIntValue(ResponseInterface::PRCODE, $params, 1000), |
52
|
13 |
|
$this->getIntValue(ResponseInterface::SRCODE, $params, 0), |
53
|
13 |
|
$this->getStringValue(ResponseInterface::RESULTTEXT, $params), |
54
|
13 |
|
$this->getStringValue(Param::DIGEST, $params), |
55
|
13 |
|
$this->getStringValue(ResponseInterface::DIGEST1, $params), |
56
|
13 |
|
$gateway |
57
|
13 |
|
); |
58
|
|
|
|
59
|
13 |
|
$paramsKeys = array_keys((new ReflectionClass(Param::class))->getConstants()); |
60
|
|
|
|
61
|
13 |
|
$paramsKeys = array_merge($paramsKeys, ResponseInterface::RESPONSE_PARAMS); |
62
|
|
|
|
63
|
13 |
|
foreach ($params as $key => $value) { |
64
|
5 |
|
if (in_array($key, $paramsKeys, true)) { |
65
|
5 |
|
$response->addParam(new ResponseParam($value, $key)); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
13 |
|
$response->sortParams(); |
70
|
|
|
|
71
|
13 |
|
return $response; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param array<string, string> $params |
76
|
|
|
*/ |
77
|
13 |
|
private function getStringValue(string $name, array &$params, string $defaultValue = ''): string |
78
|
|
|
{ |
79
|
13 |
|
$value = $defaultValue; |
80
|
13 |
|
if (isset($params[$name])) { |
81
|
13 |
|
$value = $params[$name]; |
82
|
13 |
|
unset($params[$name]); |
83
|
|
|
} |
84
|
|
|
|
85
|
13 |
|
return $value; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param array<string, string> $params |
90
|
|
|
*/ |
91
|
13 |
|
private function getIntValue(string $name, array &$params, int $defaultValue = 0): int |
92
|
|
|
{ |
93
|
13 |
|
$value = $defaultValue; |
94
|
13 |
|
if (isset($params[$name])) { |
95
|
13 |
|
$value = $params[$name]; |
96
|
13 |
|
unset($params[$name]); |
97
|
|
|
} |
98
|
|
|
|
99
|
13 |
|
return (int)$value; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|