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 CreditCard extends BaseResource implements Emptiable, ObjectSerializable |
10
|
|
|
{ |
11
|
|
|
use EmptiableTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $number; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $holder; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $expiryMonth; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $expiryYear; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $cvc; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $token; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var bool |
45
|
|
|
*/ |
46
|
|
|
private $save; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var Validators\CreditCardValidator |
50
|
|
|
*/ |
51
|
|
|
private $validator; |
52
|
|
|
|
53
|
|
|
private function validator() |
54
|
|
|
{ |
55
|
|
|
if (is_null($this->validator)) { |
56
|
|
|
$this->validator = new Validators\CreditCardValidator(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this->validator; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public function getNumber() |
66
|
|
|
{ |
67
|
|
|
return $this->number; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function getHolder() |
74
|
|
|
{ |
75
|
|
|
return $this->holder; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function getExpiryMonth() |
82
|
|
|
{ |
83
|
|
|
return $this->expiryMonth; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function getExpiryYear() |
90
|
|
|
{ |
91
|
|
|
return $this->expiryYear; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function getCvc() |
98
|
|
|
{ |
99
|
|
|
return $this->cvc; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function getToken() |
106
|
|
|
{ |
107
|
|
|
return $this->token; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $number |
112
|
|
|
*/ |
113
|
|
|
public function setNumber($number) |
114
|
|
|
{ |
115
|
|
|
$this->number = $this->getNumberUtil()->getOnlyNumbers($number); |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $holder |
122
|
|
|
*/ |
123
|
|
|
public function setHolder($holder) |
124
|
|
|
{ |
125
|
|
|
$this->holder = substr((string) $holder, 0, 50); |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $expiryMonth |
132
|
|
|
*/ |
133
|
|
|
public function setExpiryMonth($expiryMonth) |
134
|
|
|
{ |
135
|
|
|
if (!$this->validator()->isValidMonth($expiryMonth)) { |
136
|
|
|
throw new \UnexpectedValueException( |
137
|
|
|
'O mês de expiração do cartão deve ser um número entre 1 e 12' |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
$this->expiryMonth = $expiryMonth; |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $expiryYear |
147
|
|
|
*/ |
148
|
|
|
public function setExpiryYear($expiryYear) |
149
|
|
|
{ |
150
|
|
|
if (!$this->validator()->isValidYear($expiryYear)) { |
151
|
|
|
throw new \UnexpectedValueException( |
152
|
|
|
'O ano de expiração do cartão deve ser um número de 2 ou 4 dígitos' |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
$this->expiryYear = sprintf('20%d', substr($expiryYear, -2, 2)); |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param string $cvc |
162
|
|
|
*/ |
163
|
|
|
public function setCvc($cvc) |
164
|
|
|
{ |
165
|
|
|
if (!$this->validator()->isValidCvc($cvc)) { |
166
|
|
|
throw new \UnexpectedValueException( |
167
|
|
|
'O código de segurança deve ser um número e deve ter 3 ou 4 dígitos' |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
$this->cvc = $cvc; |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param string $token |
177
|
|
|
*/ |
178
|
|
|
public function setToken($token) |
179
|
|
|
{ |
180
|
|
|
$this->token = (string) $token; |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return bool |
187
|
|
|
*/ |
188
|
|
|
public function hasSave() |
189
|
|
|
{ |
190
|
|
|
return (bool) $this->save; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param bool $save |
195
|
|
|
*/ |
196
|
|
|
public function setSave($save) |
197
|
|
|
{ |
198
|
|
|
$this->save = (bool) $save; |
199
|
|
|
|
200
|
|
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return bool |
205
|
|
|
*/ |
206
|
|
|
public function hasToken() |
207
|
|
|
{ |
208
|
|
|
return (bool) !empty($this->token); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @return bool |
213
|
|
|
*/ |
214
|
|
|
public function hasCvc() |
215
|
|
|
{ |
216
|
|
|
return (bool) !empty($this->cvc); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function hide() |
220
|
|
|
{ |
221
|
|
|
$this->number = preg_replace('/^(\d{6})(\d+)(\d{4})$/', '$1******$3', $this->number); |
222
|
|
|
$this->cvc = preg_replace('/\d/', '*', $this->cvc); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function serialize() |
226
|
|
|
{ |
227
|
|
|
if ($this->isEmpty()) { |
228
|
|
|
return []; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
if ($this->hasToken()) { |
232
|
|
|
return $this->serializeCreditCardWithToken(); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return $this->serializeCreditCardWithNumber(); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
private function serializeCreditCardWithNumber() |
239
|
|
|
{ |
240
|
|
|
$_creditCard = [ |
241
|
|
|
'num_cartao' => urlencode($this->getNumber()), |
242
|
|
|
'nome_cartao' => urlencode($this->getHolder()), |
243
|
|
|
'mes_cartao' => urlencode($this->getExpiryMonth()), |
244
|
|
|
'ano_cartao' => urlencode($this->getExpiryYear()), |
245
|
|
|
]; |
246
|
|
|
|
247
|
|
|
if ($this->hasCvc()) { |
248
|
|
|
$_creditCard['cvv_cartao'] = urlencode($this->getCvc()); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
if ($this->hasSave()) { |
252
|
|
|
$_creditCard['gera_token_cartao'] = $this->hasSave(); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
return $_creditCard; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
private function serializeCreditCardWithToken() |
259
|
|
|
{ |
260
|
|
|
return [ |
261
|
|
|
'token_cartao' => urlencode($this->getToken()), |
262
|
|
|
]; |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|