Completed
Push — master ( 5b5105...960931 )
by Kirill
10:38
created

Notification::isSuccessful()   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 0
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 isSuccessful():bool
68
    {
69
        return $this->getAttribute('Success') === true;
70
    }
71
72
    public function getPaymentId()
73
    {
74
        return $this->getAttribute('PaymentId');
75
    }
76
77
    public function getPaymentCard(): PaymentCard
78
    {
79
        if ($this->payCard === null && $id = $this->getAttribute('CardId')) {
80
            $this->payCard = PaymentCard::make(
81
                $id,
82
                $this->getAttribute('Pan'),
83
                $this->getAttribute('ExpDate')
84
            );
85
        }
86
87
        return $this->payCard;
88
    }
89
90
    public function getErrorCode()
91
    {
92
        return $this->getAttribute('ErrorCode');
93
    }
94
95
    public function getAmount()
96
    {
97
        return $this->getAttribute('Amount');
98
    }
99
100
    public function getRebillId()
101
    {
102
        return $this->getAttribute('RebillId');
103
    }
104
105
    public function getData()
106
    {
107
        return $this->getAttribute('DATA');
108
    }
109
110
    public function getStatus()
111
    {
112
        return $this->getAttribute('Status');
113
    }
114
115
    public function getToken()
116
    {
117
        return $this->getAttribute('Token');
118
    }
119
120
    public function isAuthorized(): bool
121
    {
122
        return $this->getStatus() === self::AUTHORIZED;
123
    }
124
125
    public function isConfirmed(): bool
126
    {
127
        return $this->getStatus() === self::CONFIRMED;
128
    }
129
130
    public function isReversed(): bool
131
    {
132
        return $this->getStatus() === self::REVERSED;
133
    }
134
135
    public function isRefunded(): bool
136
    {
137
        return $this->getStatus() === self::REFUNDED;
138
    }
139
140
    public function isPartialRefunded(): bool
141
    {
142
        return $this->getStatus() === self::PARTIAL_REFUNDED;
143
    }
144
145
    public function isRejected(): bool
146
    {
147
        return $this->getStatus() === self::REJECTED;
148
    }
149
}
150