Completed
Push — master ( f863cd...5b5105 )
by Kirill
12:27
created

Notification::assertValidToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Personnage\Tinkoff\SDK\Merchant;
4
5
use Personnage\Tinkoff\SDK\Exception\InvalidToken;
6
use Personnage\Tinkoff\SDK\HasAttributes;
7
use function Personnage\Tinkoff\SDK\message_get_body;
8
use Personnage\Tinkoff\SDK\PaymentCard;
9
use Psr\Http\Message\RequestInterface;
10
11
final class Notification
12
{
13
    use HasAttributes, HasSignature;
14
15
    const AUTHORIZED = 'AUTHORIZED';
16
    const CONFIRMED = 'CONFIRMED';
17
    const REVERSED = 'REVERSED';
18
    const REFUNDED = 'REFUNDED';
19
    const PARTIAL_REFUNDED = 'PARTIAL_REFUNDED';
20
    const REJECTED = 'REJECTED';
21
22
    private $payCard;
23
24
    public function __construct(array $attributes)
25
    {
26
        if (isset($attributes['DATA'])) {
27
            $attributes['DATA'] = urldecode($attributes['DATA']);
28
        }
29
30
        $this->setAttributes($attributes);
31
    }
32
33
    public function __clone()
34
    {
35
        $this->payCard = null;
36
    }
37
38
    public static function fromRequest(RequestInterface $request): self
39
    {
40
        return new self(message_get_body($request));
41
    }
42
43
    /**
44
     * Throws an exception if token is invalid.
45
     *
46
     * @param string $secret
47
     *
48
     * @throws InvalidToken
49
     */
50
    public function validate(string $secret)
51
    {
52
        if ($this->getToken() !== $this->sign($this->except('Token'), $secret)) {
53
            throw new InvalidToken('Invalid token.');
54
        }
55
    }
56
57
    public function getTerminalKey()
58
    {
59
        return $this->getAttribute('TerminalKey');
60
    }
61
62
    public function getOrderId()
63
    {
64
        return $this->getAttribute('OrderId');
65
    }
66
67
    public function getPaymentId()
68
    {
69
        return $this->getAttribute('PaymentId');
70
    }
71
72
    public function getPaymentCard(): PaymentCard
73
    {
74
        if ($this->payCard === null && $id = $this->getAttribute('CardId')) {
75
            $this->payCard = PaymentCard::make(
76
                $id,
77
                $this->getAttribute('Pan'),
78
                $this->getAttribute('ExpDate')
79
            );
80
        }
81
82
        return $this->payCard;
83
    }
84
85
    public function getErrorCode()
86
    {
87
        return $this->getAttribute('ErrorCode');
88
    }
89
90
    public function getAmount()
91
    {
92
        return $this->getAttribute('Amount');
93
    }
94
95
    public function getRebillId()
96
    {
97
        return $this->getAttribute('RebillId');
98
    }
99
100
    public function getData()
101
    {
102
        return $this->getAttribute('DATA');
103
    }
104
105
    public function getStatus()
106
    {
107
        return $this->getAttribute('Status');
108
    }
109
110
    public function getToken()
111
    {
112
        return $this->getAttribute('Token');
113
    }
114
115
    public function isAuthorized(): bool
116
    {
117
        return $this->getStatus() === self::AUTHORIZED;
118
    }
119
120
    public function isConfirmed(): bool
121
    {
122
        return $this->getStatus() === self::CONFIRMED;
123
    }
124
125
    public function isReversed(): bool
126
    {
127
        return $this->getStatus() === self::REVERSED;
128
    }
129
130
    public function isRefunded(): bool
131
    {
132
        return $this->getStatus() === self::REFUNDED;
133
    }
134
135
    public function isPartialRefunded(): bool
136
    {
137
        return $this->getStatus() === self::PARTIAL_REFUNDED;
138
    }
139
140
    public function isRejected(): bool
141
    {
142
        return $this->getStatus() === self::REJECTED;
143
    }
144
}
145