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