Completed
Pull Request — master (#4)
by
unknown
02:26
created

TerminalResponse::isTimeout()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Lamoda\Payture\InPayClient;
4
5
final class TerminalResponse
6
{
7
    /**
8
     * Payture error codes.
9
     *
10
     * @see https://payture.com/api#error-codes_
11
     */
12
    public const ERROR_NONE = 'NONE';
13
    public const ERROR_ORDER_TIME_OUT = 'ORDER_TIME_OUT';
14
    public const ERROR_ILLEGAL_ORDER_STATE = 'ILLEGAL_ORDER_STATE';
15
    public const ERROR_PROCESSING = 'PROCESSING_ERROR';
16
    public const ERROR_ISSUER_FAIL = 'ISSUER_FAIL';
17
    public const ERROR_AMOUNT = 'AMOUNT_ERROR';
18
    public const ERROR_PROCESSING_FRAUD = 'PROCESSING_FRAUD_ERROR';
19
    public const ERROR_ISSUER_BLOCKED_CARD = 'ISSUER_BLOCKED_CARD';
20
21
    private const STATUS_SUCCESS = 'True';
22
23
    private const STATE_NEW = 'New';
24
    private const STATE_PREAUTH_3DS = 'PreAuthorized3DS';
25
    private const STATE_PREAUTH_AF = 'PreAuthorizedAF';
26
    private const STATE_AUTHORIZED = 'Authorized';
27
    private const STATE_VOIDED = 'Voided'; // locked and unlocked
28
    private const STATE_CHARGED = 'Charged';
29
    private const STATE_REFUNDED = 'Refunded';
30
    private const STATE_FORWARDED = 'Forwarded';
31
    private const STATE_ERROR = 'Error';
32
33
    // custom status, for case with error code NONE
34
    private const STATE_PENDING = 'Pending';
35
36
    /**
37
     * Operation success flag.
38
     *
39
     * @var bool
40
     */
41
    private $success;
42
43
    /**
44
     * Payment ID in Merchant system.
45
     *
46
     * @var string
47
     */
48
    private $orderId;
49
50
    /**
51
     * Operation amount.
52
     *
53
     * @var int
54
     */
55
    private $amount = 0;
56
57
    /**
58
     * Payment status.
59
     *
60
     * @var string|null
61
     */
62
    private $state;
63
64
    /**
65
     * Payment ID in Payture system.
66
     *
67
     * @var string
68
     */
69
    private $sessionId = '';
70
71
    /**
72
     * Unique transaction number assigned by the acquiring bank
73
     *
74
     * @var string|null
75
     */
76
    private $rrn;
77
78
    /** Error code.
79
     *
80
     * @var string
81
     */
82
    private $errorCode = '';
83
84
    /**
85
     * @param string $success Operation success flag
86
     * @param mixed $orderId Payment ID in Merchant system
87
     */
88 4
    public function __construct(string $success, string $orderId)
89
    {
90 4
        $this->success = mb_strtolower($success) === mb_strtolower(static::STATUS_SUCCESS);
91 4
        $this->orderId = $orderId;
92 4
    }
93
94
    /**
95
     * @return bool
96
     */
97 1
    public function isSuccess(): bool
98
    {
99 1
        return $this->success;
100
    }
101
102 1
    public function getOrderId(): string
103
    {
104 1
        return $this->orderId;
105
    }
106
107 1
    public function getAmount(): int
108
    {
109 1
        return $this->amount;
110
    }
111
112 1
    public function setAmount(int $amount): void
113
    {
114 1
        $this->amount = $amount;
115 1
    }
116
117 1
    public function getRrn(): ?string
118
    {
119 1
        return $this->rrn;
120
    }
121
122 1
    public function getSessionId(): string
123
    {
124 1
        return $this->sessionId;
125
    }
126
127 1
    public function setSessionId(string $sessionId): void
128
    {
129 1
        $this->sessionId = $sessionId;
130 1
    }
131
132 3
    public function getErrorCode(): string
133
    {
134 3
        return $this->errorCode ?: 'ErrCode undefined';
135
    }
136
137 3
    public function setErrorCode(string $errorCode): void
138
    {
139 3
        if ($errorCode === self::ERROR_NONE) {
140
            /* Payture hack
141
             * If error code equal NONE, operation was completed without errors!?
142
             * @link http://payture.com/integration/api/#error-codes_
143
             */
144 1
            $this->success = true;
145 1
            $this->errorCode = '';
146 1
            $this->state = self::STATE_PENDING;
147
148 1
            return;
149
        }
150
151 3
        $this->success = false;
152 3
        $this->errorCode = $errorCode;
153 3
    }
154
155 1
    public function setState(string $state): void
156
    {
157 1
        $this->state = $state;
158 1
    }
159
160 1
    public function setRrn(string $rrn): void
161
    {
162 1
        $this->rrn = $rrn;
163 1
    }
164
165 1
    public function isNewState(): bool
166
    {
167 1
        return $this->isStateEqual(static::STATE_NEW);
168
    }
169
170 1
    public function isPreAuthorized3DSState(): bool
171
    {
172 1
        return $this->isStateEqual(static::STATE_PREAUTH_3DS);
173
    }
174
175 1
    public function isPreAuthorizedAFState(): bool
176
    {
177 1
        return $this->isStateEqual(static::STATE_PREAUTH_AF);
178
    }
179
180 1
    public function isAuthorizedState(): bool
181
    {
182 1
        return $this->isStateEqual(static::STATE_AUTHORIZED);
183
    }
184
185 1
    public function isVoidedState(): bool
186
    {
187 1
        return $this->isStateEqual(static::STATE_VOIDED);
188
    }
189
190 1
    public function isChargedState(): bool
191
    {
192 1
        return $this->isStateEqual(static::STATE_CHARGED);
193
    }
194
195 1
    public function isRefundedState(): bool
196
    {
197 1
        return $this->isStateEqual(static::STATE_REFUNDED);
198
    }
199
200 1
    public function isForwardedState(): bool
201
    {
202 1
        return $this->isStateEqual(static::STATE_FORWARDED);
203
    }
204
205 1
    public function isErrorState(): bool
206
    {
207 1
        return $this->isStateEqual(static::STATE_ERROR);
208
    }
209
210 1
    public function isTimeout(): bool
211
    {
212 1
        return !$this->success && $this->getErrorCode() === static::ERROR_ORDER_TIME_OUT;
213
    }
214
215 1
    public function isIllegalOrderState(): bool
216
    {
217 1
        return !$this->success && $this->getErrorCode() === static::ERROR_ILLEGAL_ORDER_STATE;
218
    }
219
220 1
    public function isProcessingError(): bool
221
    {
222 1
        return !$this->success && $this->getErrorCode() === self::ERROR_PROCESSING;
223
    }
224
225 1
    public function isFraudError(): bool
226
    {
227 1
        return $this->getErrorCode() === self::ERROR_PROCESSING_FRAUD;
228
    }
229
230 1
    public function isAmountError(): bool
231
    {
232 1
        return $this->getErrorCode() === self::ERROR_AMOUNT;
233
    }
234
235 1
    public function isIssuerFail(): bool
236
    {
237 1
        return !$this->success && $this->getErrorCode() === self::ERROR_ISSUER_FAIL;
238
    }
239
240 1
    private function isStateEqual($expectedState): bool
241
    {
242 1
        return mb_strtolower($this->state) === mb_strtolower($expectedState);
243
    }
244
}
245