1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ipag\Classes; |
4
|
|
|
|
5
|
|
|
use Ipag\Classes\Contracts\Emptiable; |
6
|
|
|
use Ipag\Classes\Traits\EmptiableTrait; |
7
|
|
|
|
8
|
|
|
final class Order extends BaseResource implements Emptiable |
9
|
|
|
{ |
10
|
|
|
use EmptiableTrait; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $orderId; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $operation; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $callbackUrl; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var float |
29
|
|
|
*/ |
30
|
|
|
private $amount; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
private $installments; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $expiry; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $fingerprint; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Payment |
49
|
|
|
*/ |
50
|
|
|
private $payment; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var Cart |
54
|
|
|
*/ |
55
|
|
|
private $cart; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var Customer |
59
|
|
|
*/ |
60
|
|
|
private $customer; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var Subscription |
64
|
|
|
*/ |
65
|
|
|
private $subscription; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function getOrderId() |
71
|
|
|
{ |
72
|
|
|
return $this->orderId; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public function getOperation() |
79
|
|
|
{ |
80
|
|
|
return $this->operation; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function getCallbackUrl() |
87
|
|
|
{ |
88
|
|
|
return $this->callbackUrl; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return float |
93
|
|
|
*/ |
94
|
|
|
public function getAmount() |
95
|
|
|
{ |
96
|
|
|
return $this->amount; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return int |
101
|
|
|
*/ |
102
|
|
|
public function getInstallments() |
103
|
|
|
{ |
104
|
|
|
return $this->installments; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $orderId |
109
|
|
|
*/ |
110
|
|
|
public function setOrderId($orderId) |
111
|
|
|
{ |
112
|
|
|
$this->orderId = substr((string) $orderId, 0, 20); |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $operation |
119
|
|
|
*/ |
120
|
|
|
public function setOperation($operation) |
121
|
|
|
{ |
122
|
|
|
$this->operation = $operation; |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $callbackUrl |
129
|
|
|
*/ |
130
|
|
|
public function setCallbackUrl($callbackUrl) |
131
|
|
|
{ |
132
|
|
|
$this->callbackUrl = substr((string) $callbackUrl, 0, 255); |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param float $amount |
139
|
|
|
*/ |
140
|
|
|
public function setAmount($amount) |
141
|
|
|
{ |
142
|
|
|
$this->amount = $this->getNumberUtil()->convertToDouble($amount); |
143
|
|
|
|
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param int $installments |
149
|
|
|
*/ |
150
|
|
|
public function setInstallments($installments) |
151
|
|
|
{ |
152
|
|
|
$this->installments = $this->checkIfInstallmentsIsValidAndReturn($installments); |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
public function getExpiry() |
161
|
|
|
{ |
162
|
|
|
return $this->expiry; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string $expiry |
167
|
|
|
*/ |
168
|
|
|
public function setExpiry($expiry) |
169
|
|
|
{ |
170
|
|
|
if (!$this->isValidExpiryFormatDate($expiry)) { |
171
|
|
|
throw new \UnexpectedValueException( |
172
|
|
|
'A data de vencimento não é valida ou está em formato incorreto, deve ser informada utilizando o formato dd/mm/aaaa' |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
$this->expiry = $expiry; |
176
|
|
|
|
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
|
|
public function getFingerprint() |
184
|
|
|
{ |
185
|
|
|
return $this->fingerprint; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param string $fingerprint |
190
|
|
|
*/ |
191
|
|
|
public function setFingerprint($fingerprint) |
192
|
|
|
{ |
193
|
|
|
$this->fingerprint = substr((string) $fingerprint, 0, 120); |
194
|
|
|
|
195
|
|
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
private function checkIfInstallmentsIsValidAndReturn($installments) |
199
|
|
|
{ |
200
|
|
|
if (empty($installments) || $installments < 1) { |
201
|
|
|
$installments = 1; |
202
|
|
|
} elseif ($installments > 12) { |
203
|
|
|
throw new \UnexpectedValueException( |
204
|
|
|
'O parcelamento não pode ser maior que 12 (doze)' |
205
|
|
|
); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return (int) $installments; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
View Code Duplication |
private function isValidExpiryFormatDate($date) |
|
|
|
|
212
|
|
|
{ |
213
|
|
|
if (preg_match("/([0-9]{2})\/([0-9]{2})\/([0-9]{4})/", $date, $matches)) { |
214
|
|
|
if (checkdate($matches[2], $matches[1], $matches[3])) { |
215
|
|
|
return true; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return false; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return Payment |
224
|
|
|
*/ |
225
|
|
|
public function getPayment() |
226
|
|
|
{ |
227
|
|
|
if (is_null($this->payment)) { |
228
|
|
|
$this->payment = new Payment(); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return $this->payment; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param Payment $payment |
236
|
|
|
*/ |
237
|
|
|
public function setPayment(Payment $payment) |
238
|
|
|
{ |
239
|
|
|
$this->payment = $payment; |
240
|
|
|
|
241
|
|
|
return $this; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return Cart |
246
|
|
|
*/ |
247
|
|
|
public function getCart() |
248
|
|
|
{ |
249
|
|
|
if (is_null($this->cart)) { |
250
|
|
|
$this->cart = new Cart(); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return $this->cart; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @param Cart $cart |
258
|
|
|
*/ |
259
|
|
|
public function setCart(Cart $cart) |
260
|
|
|
{ |
261
|
|
|
$this->cart = $cart; |
262
|
|
|
|
263
|
|
|
return $this; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @return Customer |
268
|
|
|
*/ |
269
|
|
|
public function getCustomer() |
270
|
|
|
{ |
271
|
|
|
if (is_null($this->customer)) { |
272
|
|
|
$this->customer = new Customer(); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return $this->customer; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @param Customer $customer |
280
|
|
|
*/ |
281
|
|
|
public function setCustomer(Customer $customer) |
282
|
|
|
{ |
283
|
|
|
$this->customer = $customer; |
284
|
|
|
|
285
|
|
|
return $this; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @return Subscription |
290
|
|
|
*/ |
291
|
|
|
public function getSubscription() |
292
|
|
|
{ |
293
|
|
|
if (is_null($this->subscription)) { |
294
|
|
|
$this->subscription = new Subscription(); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
return $this->subscription; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @param Subscription $subscription |
302
|
|
|
*/ |
303
|
|
|
public function setSubscription(Subscription $subscription) |
304
|
|
|
{ |
305
|
|
|
$this->subscription = $subscription; |
306
|
|
|
|
307
|
|
|
return $this; |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.