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