Order::getExpiry()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Ipag\Classes;
4
5
use Ipag\Classes\Contracts\Emptiable;
6
use Ipag\Classes\Contracts\ObjectSerializable;
7
use Ipag\Classes\Traits\EmptiableTrait;
8
9
final class Order extends BaseResource implements Emptiable, ObjectSerializable
10
{
11
    use EmptiableTrait;
12
13
    /**
14
     * @var string
15
     */
16
    private $orderId;
17
18
    /**
19
     * @var string
20
     */
21
    private $operation;
22
23
    /**
24
     * @var string
25
     */
26
    private $callbackUrl;
27
28
    /**
29
     * @var string
30
     */
31
    private $redirectUrl;
32
33
    /**
34
     * @var float
35
     */
36
    private $amount;
37
38
    /**
39
     * @var int
40
     */
41
    private $installments;
42
43
    /**
44
     * @var string
45
     */
46
    private $expiry;
47
48
    /**
49
     * @var string
50
     */
51
    private $fingerprint;
52
53
    /**
54
     * @var string
55
     */
56
    private $deviceFingerprint;
57
58
    /**
59
     * @var string
60
     */
61
    private $acquirerToken;
62
63
    /**
64
     * @var string
65
     */
66
    private $visitorId;
67
68
    /**
69
     * @var string
70
     */
71
    private $internetProtocol;
72
73
    /**
74
     * @var bool
75
     */
76
    private $antifraud = true;
77
78
    /**
79
     * @var Payment
80
     */
81
    private $payment;
82
83
    /**
84
     * @var Cart
85
     */
86
    private $cart;
87
88
    /**
89
     * @var Pix
90
     */
91
    private $pix;
92
93
    /**
94
     * @var Customer
95
     */
96
    private $customer;
97
98
    /**
99
     * @var Subscription
100
     */
101
    private $subscription;
102
103
    /**
104
     * @var string
105
     */
106
    private $capture;
107
108
    /**
109
     * @return string
110
     */
111
    public function getOrderId()
112
    {
113
        return $this->orderId;
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public function getOperation()
120
    {
121
        return $this->operation;
122
    }
123
124
    /**
125
     * @return string
126
     */
127
    public function getCallbackUrl()
128
    {
129
        return $this->callbackUrl;
130
    }
131
132
    /**
133
     * @return string
134
     */
135
    public function getRedirectUrl()
136
    {
137
        return $this->redirectUrl;
138
    }
139
140
    /**
141
     * @return float
142
     */
143
    public function getAmount()
144
    {
145
        return $this->amount;
146
    }
147
148
    /**
149
     * @return int
150
     */
151
    public function getInstallments()
152
    {
153
        return $this->installments;
154
    }
155
156
    /**
157
     * @param string $orderId
158
     */
159
    public function setOrderId($orderId)
160
    {
161
        $this->orderId = substr((string) $orderId, 0, 20);
162
163
        return $this;
164
    }
165
166
    /**
167
     * @param string $operation
168
     */
169
    public function setOperation($operation)
170
    {
171
        $this->operation = $operation;
172
173
        return $this;
174
    }
175
176
    /**
177
     * @param string $callbackUrl
178
     */
179
    public function setCallbackUrl($callbackUrl)
180
    {
181
        $this->callbackUrl = substr((string) $callbackUrl, 0, 255);
182
183
        return $this;
184
    }
185
186
    /**
187
     * @param string $redirectUrl
188
     */
189
    public function setRedirectUrl($redirectUrl)
190
    {
191
        $this->redirectUrl = substr((string) $redirectUrl, 0, 255);
192
193
        return $this;
194
    }
195
196
    /**
197
     * @param float $amount
198
     */
199
    public function setAmount($amount)
200
    {
201
        $this->amount = $this->getNumberUtil()->convertToDouble($amount);
202
203
        return $this;
204
    }
205
206
    /**
207
     * @param int $installments
208
     */
209
    public function setInstallments($installments)
210
    {
211
        $this->installments = $this->checkIfInstallmentsIsValidAndReturn($installments);
212
213
        return $this;
214
    }
215
216
    /**
217
     * @return string
218
     */
219
    public function getExpiry()
220
    {
221
        return $this->expiry;
222
    }
223
224
    /**
225
     * @param string $expiry
226
     */
227
    public function setExpiry($expiry)
228
    {
229
        if (!$this->getDateUtil()->isValid($expiry)) {
230
            throw new \UnexpectedValueException(
231
                'A data de vencimento não é valida ou está em formato incorreto, deve ser informada utilizando o formato dd/mm/aaaa'
232
            );
233
        }
234
        $this->expiry = $expiry;
235
236
        return $this;
237
    }
238
239
    /**
240
     * @return string
241
     */
242
    public function getFingerprint()
243
    {
244
        return $this->fingerprint;
245
    }
246
247
    /**
248
     * @param string $fingerprint
249
     */
250
    public function setFingerprint($fingerprint)
251
    {
252
        $this->fingerprint = substr((string) $fingerprint, 0, 120);
253
254
        return $this;
255
    }
256
257
    /**
258
     * @return string
259
     */
260
    public function getDeviceFingerprint()
261
    {
262
        return $this->deviceFingerprint;
263
    }
264
265
    /**
266
     * @param string $fingerprint
267
     */
268
    public function setDeviceFingerprint($deviceFingerprint)
269
    {
270
        $this->deviceFingerprint = (string) $deviceFingerprint;
271
272
        return $this;
273
    }
274
275
    /**
276
     * @return string
277
     */
278
    public function getAcquirerToken()
279
    {
280
        return $this->acquirerToken;
281
    }
282
283
    /**
284
     * @param string $acquirerToken
285
     */
286
    public function setAcquirerToken($acquirerToken)
287
    {
288
        $this->acquirerToken = substr((string) $acquirerToken, 0, 120);
289
290
        return $this;
291
    }
292
293
    /**
294
     * @return string
295
     */
296
    public function getIp()
297
    {
298
        return $this->internetProtocol;
299
    }
300
301
    /**
302
     * @param string $internetProtocol
303
     */
304
    public function setIp($internetProtocol)
305
    {
306
        if (filter_var(trim($internetProtocol), FILTER_VALIDATE_IP)) {
307
            $this->internetProtocol = trim($internetProtocol);
308
        }
309
310
        return $this;
311
    }
312
313
    private function checkIfInstallmentsIsValidAndReturn($installments)
314
    {
315
        if (empty($installments) || $installments < 1) {
316
            $installments = 1;
317
        } elseif ($installments > 12) {
318
            throw new \UnexpectedValueException(
319
                'O parcelamento não pode ser maior que 12 (doze)'
320
            );
321
        }
322
323
        return (int) $installments;
324
    }
325
326
    /**
327
     * @return Payment
328
     */
329
    public function getPayment()
330
    {
331
        if (is_null($this->payment)) {
332
            $this->payment = new Payment();
333
        }
334
335
        return $this->payment;
336
    }
337
338
    /**
339
     * @param Payment $payment
340
     */
341
    public function setPayment(Payment $payment)
342
    {
343
        $this->payment = $payment;
344
345
        return $this;
346
    }
347
348
    /**
349
     * @return Cart
350
     */
351
    public function getCart()
352
    {
353
        if (is_null($this->cart)) {
354
            $this->cart = new Cart();
355
        }
356
357
        return $this->cart;
358
    }
359
360
    /**
361
     * @param Cart $cart
362
     */
363
    public function setCart(Cart $cart)
364
    {
365
        $this->cart = $cart;
366
367
        return $this;
368
    }
369
370
    /**
371
     * @return Pix
372
     */
373
    public function getPix()
374
    {
375
        if (is_null($this->pix)) {
376
            $this->pix = new Pix();
377
        }
378
379
        return $this->pix;
380
    }
381
382
    /**
383
     * @param Pix $pix
384
     */
385
    public function setPix(Pix $pix)
386
    {
387
        $this->pix = $pix;
388
389
        return $this;
390
    }
391
392
    /**
393
     * @return Customer
394
     */
395
    public function getCustomer()
396
    {
397
        if (is_null($this->customer)) {
398
            $this->customer = new Customer();
399
        }
400
401
        return $this->customer;
402
    }
403
404
    /**
405
     * @param Customer $customer
406
     */
407
    public function setCustomer(Customer $customer)
408
    {
409
        $this->customer = $customer;
410
411
        return $this;
412
    }
413
414
    /**
415
     * @return Subscription
416
     */
417
    public function getSubscription()
418
    {
419
        if (is_null($this->subscription)) {
420
            $this->subscription = new Subscription();
421
        }
422
423
        return $this->subscription;
424
    }
425
426
    /**
427
     * @param Subscription $subscription
428
     */
429
    public function setSubscription(Subscription $subscription)
430
    {
431
        $this->subscription = $subscription;
432
433
        return $this;
434
    }
435
436
    public function serialize()
437
    {
438
        if ($this->isEmpty()) {
439
            throw new \Exception('É necessário informar os dados do Pedido (Order)');
440
        }
441
442
        $_order = [
443
            'pedido'                => urlencode((string) $this->getOrderId()),
444
            'operacao'              => urlencode((string) $this->getOperation()),
445
            'url_retorno'           => urlencode((string) $this->getCallbackUrl()),
446
            'url_redirect'          => urlencode((string) $this->getRedirectUrl()),
447
            'valor'                 => urlencode((string) $this->getAmount()),
448
            'parcelas'              => urlencode((string) $this->getInstallments()),
449
            'vencto'                => urlencode((string) $this->getExpiry()),
450
            'stelo_fingerprint'     => urlencode((string) $this->getFingerprint()),
451
            'device_fingerprint'    => urlencode((string) $this->getDeviceFingerprint()),
452
            'acquirerToken'         => urlencode((string) $this->getAcquirerToken()),
453
            'ip'                    => urlencode((string) $this->getIp()),
454
            'antifraude'            => urlencode((string) $this->getAntifraud()),
455
            'visitorId'             => urlencode((string) $this->getVisitorId()),
456
            'captura'               => urlencode((string) $this->getCapture()),
457
        ];
458
459
        return array_merge(
460
            $_order,
461
            $this->getPayment()->serialize(),
462
            $this->getCart()->serialize(),
463
            $this->getCustomer()->serialize(),
464
            $this->getSubscription()->serialize(),
465
            $this->getPix()->serialize()
466
        );
467
    }
468
469
    /**
470
     * @return string
471
     */
472
    public function getAntifraud()
473
    {
474
        return $this->antifraud ? '1' : '0';
475
    }
476
477
    /**
478
     * @param bool $antifraud
479
     *
480
     * @return self
481
     */
482
    public function setAntifraud($antifraud)
483
    {
484
        $this->antifraud = (bool) $antifraud;
485
486
        return $this;
487
    }
488
489
    /**
490
     * @return string
491
     */
492
    public function getVisitorId()
493
    {
494
        return $this->visitorId;
495
    }
496
497
    /**
498
     * @param string $visitorId
499
     *
500
     * @return self
501
     */
502
    public function setVisitorId($visitorId)
503
    {
504
        $this->visitorId = trim($visitorId);
505
506
        return $this;
507
    }
508
509
    /**
510
     * @return string
511
     */
512
    public function getCapture()
513
    {
514
        if (empty($this->capture)) {
515
            $this->capture = 'p';
516
        }
517
518
        return $this->capture;
519
    }
520
521
    /**
522
     * @param string $capture
523
     *
524
     * @return self
525
     */
526
    public function setCapture($capture)
527
    {
528
        switch ($capture) {
529
            case 'c':
530
                $this->capture = 'c';
531
                break;
532
            case 'a':
533
                $this->capture = 'a';
534
                break;
535
            default:
536
                $this->capture = 'p';
537
                break;
538
        }
539
540
        return $this;
541
    }
542
}
543