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