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