Passed
Push — master ( 5c0742...53fe12 )
by João Felipe Magro
02:15 queued 22s
created

Order::setPix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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