|
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 |
|
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
|
|
|
/** |
|
27
|
|
|
* @var PaymentCard |
|
28
|
|
|
*/ |
|
29
|
|
|
private $paymentCard; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Create a new instance. |
|
33
|
|
|
* |
|
34
|
|
|
* @param array $values |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(array $values) |
|
37
|
|
|
{ |
|
38
|
|
|
if (isset($values['DATA'])) { |
|
39
|
|
|
$values['DATA'] = urldecode($values['DATA']); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$this->values = $values; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Create a new instance from request instance. |
|
47
|
|
|
* |
|
48
|
|
|
* @param RequestInterface $request |
|
49
|
|
|
* @return self |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function fromRequest(RequestInterface $request): self |
|
52
|
|
|
{ |
|
53
|
|
|
return new self(message_get_body($request)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Throws an exception if token is invalid. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $secret |
|
60
|
|
|
* @throws InvalidToken |
|
61
|
|
|
*/ |
|
62
|
|
|
public function validate(string $secret) |
|
63
|
|
|
{ |
|
64
|
|
|
if ($this->sign($this->values, $secret) !== $this->get('Token')) { |
|
65
|
|
|
throw new InvalidToken('Invalid token.'); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Is request successful? |
|
71
|
|
|
* |
|
72
|
|
|
* @return bool |
|
73
|
|
|
*/ |
|
74
|
|
|
public function isSuccessful(): bool |
|
75
|
|
|
{ |
|
76
|
|
|
return 'true' === $this->get('Success'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Is request authorized? |
|
81
|
|
|
* |
|
82
|
|
|
* @return bool |
|
83
|
|
|
*/ |
|
84
|
|
|
public function isAuthorized(): bool |
|
85
|
|
|
{ |
|
86
|
|
|
return self::AUTHORIZED === $this->get('Status'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Is request confirmed? |
|
91
|
|
|
* |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
|
|
public function isConfirmed(): bool |
|
95
|
|
|
{ |
|
96
|
|
|
return self::CONFIRMED === $this->get('Status'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Is request reversed? |
|
101
|
|
|
* |
|
102
|
|
|
* @return bool |
|
103
|
|
|
*/ |
|
104
|
|
|
public function isReversed(): bool |
|
105
|
|
|
{ |
|
106
|
|
|
return self::REVERSED === $this->get('Status'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Is request refunded? |
|
111
|
|
|
* |
|
112
|
|
|
* @return bool |
|
113
|
|
|
*/ |
|
114
|
|
|
public function isRefunded(): bool |
|
115
|
|
|
{ |
|
116
|
|
|
return self::REFUNDED === $this->get('Status'); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Is request refunded? |
|
121
|
|
|
* |
|
122
|
|
|
* @return bool |
|
123
|
|
|
*/ |
|
124
|
|
|
public function isPartialRefunded(): bool |
|
125
|
|
|
{ |
|
126
|
|
|
return self::PARTIAL_REFUNDED === $this->get('Status'); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Is request rejected? |
|
131
|
|
|
* |
|
132
|
|
|
* @return bool |
|
133
|
|
|
*/ |
|
134
|
|
|
public function isRejected(): bool |
|
135
|
|
|
{ |
|
136
|
|
|
return self::REJECTED === $this->get('Status'); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Get a payment card instance if card id exists. |
|
141
|
|
|
* |
|
142
|
|
|
* @return PaymentCard|null |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getPaymentCard() |
|
145
|
|
|
{ |
|
146
|
|
|
if ($this->paymentCard || $this->get('CardId')) { |
|
147
|
|
|
$this->paymentCard = PaymentCard::make($this->get('CardId'), $this->get('Pan'), $this->get('ExpDate')); |
|
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return $this->paymentCard; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Get same value by key. |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $key |
|
157
|
|
|
* @return string|null |
|
158
|
|
|
*/ |
|
159
|
|
|
public function get(string $key) |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->values[$key] ?? null; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Get all values as JSON. |
|
166
|
|
|
* |
|
167
|
|
|
* @param int $options |
|
168
|
|
|
* @return string |
|
169
|
|
|
*/ |
|
170
|
|
|
public function toJson($options = 0): string |
|
171
|
|
|
{ |
|
172
|
|
|
return json_encode($this->toArray(), $options); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Get all values as a plain array. |
|
177
|
|
|
* |
|
178
|
|
|
* @return array |
|
179
|
|
|
*/ |
|
180
|
|
|
public function toArray(): array |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->values; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..