1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Personnage\Tinkoff\SDK\Merchant; |
4
|
|
|
|
5
|
|
|
use function Personnage\Tinkoff\SDK\message_get_body; |
6
|
|
|
use Personnage\Tinkoff\SDK\Exception\InvalidToken; |
7
|
|
|
use Personnage\Tinkoff\SDK\PaymentCard; |
8
|
|
|
use Psr\Http\Message\RequestInterface; |
9
|
|
|
|
10
|
|
|
final class Notification implements \ArrayAccess, \IteratorAggregate, \JsonSerializable |
11
|
|
|
{ |
12
|
|
|
use HasSignature; |
13
|
|
|
|
14
|
|
|
const AUTHORIZED = 'AUTHORIZED'; |
15
|
|
|
const CONFIRMED = 'CONFIRMED'; |
16
|
|
|
const PARTIAL_REFUNDED = 'PARTIAL_REFUNDED'; |
17
|
|
|
const REFUNDED = 'REFUNDED'; |
18
|
|
|
const REJECTED = 'REJECTED'; |
19
|
|
|
const REVERSED = 'REVERSED'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $values; |
25
|
|
|
|
26
|
|
|
public function __construct(array $values) |
27
|
|
|
{ |
28
|
|
|
$this->values = $values; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create a new notification from request instance. |
33
|
|
|
* |
34
|
|
|
* @param RequestInterface $request |
35
|
|
|
* @return Notification |
36
|
|
|
*/ |
37
|
|
|
public static function fromRequest(RequestInterface $request) |
38
|
|
|
{ |
39
|
|
|
return new self(message_get_body($request)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Throws an exception if token is invalid. |
44
|
|
|
* |
45
|
|
|
* @param string $secret |
46
|
|
|
* @throws InvalidToken |
47
|
|
|
*/ |
48
|
|
|
public function validate(string $secret) |
49
|
|
|
{ |
50
|
|
|
$new = $this->except('Token'); |
51
|
|
|
if ($this->getToken() !== $this->sign($new->toArray(), $secret)) { |
52
|
|
|
throw new InvalidToken('Invalid token.'); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get a payment card instance if card id is exists. |
58
|
|
|
* |
59
|
|
|
* @return PaymentCard|null |
60
|
|
|
*/ |
61
|
|
|
public function getPaymentCard() |
62
|
|
|
{ |
63
|
|
|
if ($this->offsetExists('CardId')) { |
64
|
|
|
return PaymentCard::make( |
65
|
|
|
$this->offsetGet('CardId'), |
66
|
|
|
$this->offsetGet('Pan'), |
67
|
|
|
$this->offsetGet('ExpDate') |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get a subset of the values. |
74
|
|
|
* |
75
|
|
|
* @param array ...$values |
76
|
|
|
* @return self |
77
|
|
|
*/ |
78
|
|
|
public function only(...$values) |
79
|
|
|
{ |
80
|
|
|
$results = []; |
81
|
|
|
foreach ($values as $value) { |
82
|
|
|
$results[$value] = $this->offsetGet($value); |
83
|
|
|
} |
84
|
|
|
return new self($results); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get all of the given instance except for a specified array of keys. |
89
|
|
|
* |
90
|
|
|
* @param array ...$values |
91
|
|
|
* @return self |
92
|
|
|
*/ |
93
|
|
|
public static function except(...$values) |
94
|
|
|
{ |
95
|
|
|
$new = clone $this; |
|
|
|
|
96
|
|
|
foreach ($values as $value) { |
97
|
|
|
$new->offsetUnset($value); |
98
|
|
|
} |
99
|
|
|
return $new; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get an iterator for the values. |
104
|
|
|
* |
105
|
|
|
* @return \ArrayIterator |
106
|
|
|
*/ |
107
|
|
|
public function getIterator() |
108
|
|
|
{ |
109
|
|
|
return new \ArrayIterator($this->values); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Determine if a value exists at an offset. |
114
|
|
|
* |
115
|
|
|
* @param mixed $key |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
public function offsetExists($key) |
119
|
|
|
{ |
120
|
|
|
return array_key_exists($key, $this->values); |
121
|
|
|
} |
122
|
|
|
/** |
123
|
|
|
* Get a value at a given offset. |
124
|
|
|
* |
125
|
|
|
* @param mixed $key |
126
|
|
|
* @return mixed |
127
|
|
|
*/ |
128
|
|
|
public function offsetGet($key) |
129
|
|
|
{ |
130
|
|
|
return $this->values[$key]; |
131
|
|
|
} |
132
|
|
|
/** |
133
|
|
|
* Set the value at a given offset. |
134
|
|
|
* |
135
|
|
|
* @param mixed $key |
136
|
|
|
* @param mixed $value |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
|
|
public function offsetSet($key, $value) |
140
|
|
|
{ |
141
|
|
|
if (null === $value) { |
142
|
|
|
$this->offsetUnset($key); |
143
|
|
|
} else { |
144
|
|
|
$this->values[$key] = $value; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
/** |
148
|
|
|
* Unset the value at a given offset. |
149
|
|
|
* |
150
|
|
|
* @param string $key |
151
|
|
|
* @return void |
152
|
|
|
*/ |
153
|
|
|
public function offsetUnset($key) |
154
|
|
|
{ |
155
|
|
|
unset($this->values[$key]); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
|
|
public function jsonSerialize() |
162
|
|
|
{ |
163
|
|
|
return $this->toArray(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get all values as JSON. |
168
|
|
|
* |
169
|
|
|
* @param int $options |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
public function toJson($options = 0) |
173
|
|
|
{ |
174
|
|
|
return json_encode($this->jsonSerialize(), $options); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Get all values as a plain array. |
179
|
|
|
* |
180
|
|
|
* @return array |
181
|
|
|
*/ |
182
|
|
|
public function toArray() |
183
|
|
|
{ |
184
|
|
|
return $this->values; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.