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