Passed
Push — master ( 3e5289...db9510 )
by Pavel
02:34
created

TerminalResponse::setRrn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 1
    public function isSuccess(): bool
95
    {
96 1
        return $this->success;
97
    }
98
99 1
    public function getOrderId(): string
100
    {
101 1
        return $this->orderId;
102
    }
103
104 1
    public function getAmount(): int
105
    {
106 1
        return $this->amount;
107
    }
108
109 1
    public function setAmount(int $amount): void
110
    {
111 1
        $this->amount = $amount;
112 1
    }
113
114 1
    public function getRrn(): ?string
115
    {
116 1
        return $this->rrn;
117
    }
118
119 1
    public function getSessionId(): string
120
    {
121 1
        return $this->sessionId;
122
    }
123
124 1
    public function setSessionId(string $sessionId): void
125
    {
126 1
        $this->sessionId = $sessionId;
127 1
    }
128
129 3
    public function getErrorCode(): string
130
    {
131 3
        return $this->errorCode ?: 'ErrCode undefined';
132
    }
133
134 3
    public function setErrorCode(string $errorCode): void
135
    {
136 3
        if ($errorCode === self::ERROR_NONE) {
137
            /* Payture hack
138
             * If error code equal NONE, operation was completed without errors!?
139
             * @link http://payture.com/integration/api/#error-codes_
140
             */
141 1
            $this->success = true;
142 1
            $this->errorCode = '';
143 1
            $this->state = self::STATE_PENDING;
144
145 1
            return;
146
        }
147
148 3
        $this->success = false;
149 3
        $this->errorCode = $errorCode;
150 3
    }
151
152 1
    public function setState(string $state): void
153
    {
154 1
        $this->state = $state;
155 1
    }
156
157 1
    public function setRrn(string $rrn): void
158
    {
159 1
        $this->rrn = $rrn;
160 1
    }
161
162 1
    public function isNewState(): bool
163
    {
164 1
        return $this->isStateEqual(static::STATE_NEW);
165
    }
166
167 1
    public function isPreAuthorized3DSState(): bool
168
    {
169 1
        return $this->isStateEqual(static::STATE_PREAUTH_3DS);
170
    }
171
172 1
    public function isPreAuthorizedAFState(): bool
173
    {
174 1
        return $this->isStateEqual(static::STATE_PREAUTH_AF);
175
    }
176
177 1
    public function isAuthorizedState(): bool
178
    {
179 1
        return $this->isStateEqual(static::STATE_AUTHORIZED);
180
    }
181
182 1
    public function isVoidedState(): bool
183
    {
184 1
        return $this->isStateEqual(static::STATE_VOIDED);
185
    }
186
187 1
    public function isChargedState(): bool
188
    {
189 1
        return $this->isStateEqual(static::STATE_CHARGED);
190
    }
191
192 1
    public function isRefundedState(): bool
193
    {
194 1
        return $this->isStateEqual(static::STATE_REFUNDED);
195
    }
196
197 1
    public function isForwardedState(): bool
198
    {
199 1
        return $this->isStateEqual(static::STATE_FORWARDED);
200
    }
201
202 1
    public function isErrorState(): bool
203
    {
204 1
        return $this->isStateEqual(static::STATE_ERROR);
205
    }
206
207 1
    public function isTimeout(): bool
208
    {
209 1
        return !$this->success && $this->getErrorCode() === static::ERROR_ORDER_TIME_OUT;
210
    }
211
212 1
    public function isIllegalOrderState(): bool
213
    {
214 1
        return !$this->success && $this->getErrorCode() === static::ERROR_ILLEGAL_ORDER_STATE;
215
    }
216
217 1
    public function isProcessingError(): bool
218
    {
219 1
        return !$this->success && $this->getErrorCode() === self::ERROR_PROCESSING;
220
    }
221
222 1
    public function isFraudError(): bool
223
    {
224 1
        return $this->getErrorCode() === self::ERROR_PROCESSING_FRAUD;
225
    }
226
227 1
    public function isAmountError(): bool
228
    {
229 1
        return $this->getErrorCode() === self::ERROR_AMOUNT;
230
    }
231
232 1
    public function isIssuerFail(): bool
233
    {
234 1
        return !$this->success && $this->getErrorCode() === self::ERROR_ISSUER_FAIL;
235
    }
236
237 1
    private function isStateEqual($expectedState): bool
238
    {
239 1
        return mb_strtolower($this->state) === mb_strtolower($expectedState);
240
    }
241
}
242