1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DansMaCulotte\Monetico; |
4
|
|
|
|
5
|
|
|
use DansMaCulotte\Monetico\Exceptions\Exception; |
6
|
|
|
use DansMaCulotte\Monetico\Requests\AbstractRequest; |
7
|
|
|
use DansMaCulotte\Monetico\Responses\AbstractResponse; |
8
|
|
|
|
9
|
|
|
class Monetico |
10
|
|
|
{ |
11
|
|
|
/** @var string */ |
12
|
|
|
const SERVICE_VERSION = '3.0'; |
13
|
|
|
|
14
|
|
|
/** @var string */ |
15
|
|
|
const MAIN_REQUEST_URL = 'https://p.monetico-services.com'; |
16
|
|
|
|
17
|
|
|
/** @var string */ |
18
|
|
|
const MISC_REQUEST_URL = 'https://payment-api.e-i.com'; |
19
|
|
|
|
20
|
|
|
/** @var string|null */ |
21
|
|
|
private $eptCode = null; |
22
|
|
|
|
23
|
|
|
/** @var string|null */ |
24
|
|
|
private $securityKey = null; |
25
|
|
|
|
26
|
|
|
/** @var string|null */ |
27
|
|
|
private $companyCode = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Construct method |
31
|
|
|
* |
32
|
|
|
* @param string $eptCode EPT code |
33
|
|
|
* @param string $securityKey Security key |
34
|
|
|
* @param string $companyCode Company code |
35
|
|
|
* @throws Exception |
36
|
|
|
*/ |
37
|
|
|
public function __construct( |
38
|
|
|
string $eptCode, |
39
|
|
|
string $securityKey, |
40
|
|
|
string $companyCode |
41
|
|
|
) { |
42
|
|
|
if (strlen($eptCode) !== 7) { |
43
|
|
|
throw Exception::invalidEptCode($eptCode); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (strlen($securityKey) !== 40) { |
47
|
|
|
throw Exception::invalidSecurityKey(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->eptCode = $eptCode; |
51
|
|
|
$this->securityKey = self::getUsableKey($securityKey); |
52
|
|
|
$this->companyCode = $companyCode; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Transform security key for seal |
57
|
|
|
* |
58
|
|
|
* @param string $key |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public static function getUsableKey(string $key): string |
62
|
|
|
{ |
63
|
|
|
$hexStrKey = substr($key, 0, 38); |
64
|
|
|
$hexFinal = '' . substr($key, 38, 2) . '00'; |
65
|
|
|
|
66
|
|
|
$cca0 = ord($hexFinal); |
67
|
|
|
|
68
|
|
|
if ($cca0 > 70 && $cca0 < 97) { |
69
|
|
|
$hexStrKey .= chr($cca0 - 23) . $hexFinal[1]; |
70
|
|
|
} else { |
71
|
|
|
if ($hexFinal[1] === 'M') { |
72
|
|
|
$hexStrKey .= $hexFinal[0] . '0'; |
73
|
|
|
} else { |
74
|
|
|
$hexStrKey .= substr($hexFinal, 0, 2); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return pack('H*', $hexStrKey); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Return array fields required on bank interface |
83
|
|
|
* |
84
|
|
|
* @param AbstractRequest $request |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
public function getFields(AbstractRequest $request): array |
88
|
|
|
{ |
89
|
|
|
$fields = $request->fieldsToArray( |
90
|
|
|
$this->eptCode, |
|
|
|
|
91
|
|
|
$this->companyCode, |
|
|
|
|
92
|
|
|
self::SERVICE_VERSION |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$seal = $request->generateSeal( |
96
|
|
|
$this->securityKey, |
|
|
|
|
97
|
|
|
$fields |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$fields = $request->generateFields( |
101
|
|
|
$seal, |
102
|
|
|
$fields |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
return $fields; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Validate seal from response |
110
|
|
|
* |
111
|
|
|
* @param AbstractResponse $response |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
public function validate(AbstractResponse $response): bool |
115
|
|
|
{ |
116
|
|
|
$seal = $response->validateSeal( |
117
|
|
|
$this->eptCode, |
|
|
|
|
118
|
|
|
$this->securityKey |
|
|
|
|
119
|
|
|
); |
120
|
|
|
|
121
|
|
|
return $seal; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|