1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Moip\Resource; |
4
|
|
|
|
5
|
|
|
use stdClass; |
6
|
|
|
use UnexpectedValueException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Customer. |
10
|
|
|
*/ |
11
|
|
|
class Customer extends MoipResource |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Path customers API. |
15
|
|
|
* |
16
|
|
|
* @const string |
17
|
|
|
*/ |
18
|
|
|
const PATH = 'customers'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Address Type. |
22
|
|
|
* |
23
|
|
|
* @const string |
24
|
|
|
*/ |
25
|
|
|
const ADDRESS_BILLING = 'BILLING'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Address Type. |
29
|
|
|
* |
30
|
|
|
* @const string |
31
|
|
|
*/ |
32
|
|
|
const ADDRESS_SHIPPING = 'SHIPPING'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Standard country . |
36
|
|
|
* |
37
|
|
|
* @const string |
38
|
|
|
*/ |
39
|
|
|
const ADDRESS_COUNTRY = 'BRA'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Standard document type. |
43
|
|
|
* |
44
|
|
|
* @const string |
45
|
|
|
*/ |
46
|
|
|
const TAX_DOCUMENT = 'CPF'; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Initialize a new instance. |
50
|
|
|
*/ |
51
|
|
|
public function initialize() |
52
|
|
|
{ |
53
|
|
|
$this->data = new stdClass(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return \Moip\Resource\CustomerCreditCard |
58
|
|
|
*/ |
59
|
|
|
public function creditCard() |
60
|
|
|
{ |
61
|
|
|
return new CustomerCreditCard($this->moip); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Add a new address to the customer. |
66
|
|
|
* |
67
|
|
|
* @param string $type Address type: SHIPPING or BILLING. |
68
|
|
|
* @param string $street Street address. |
69
|
|
|
* @param string $number Number address. |
70
|
|
|
* @param string $district Neighborhood address. |
71
|
|
|
* @param string $city City address. |
72
|
|
|
* @param string $state State address. |
73
|
|
|
* @param string $zip The zip code billing address. |
74
|
|
|
* @param string $complement Address complement. |
75
|
|
|
* @param string $country Country ISO-alpha3 format, BRA example. |
76
|
|
|
* |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
|
|
public function addAddress($type, $street, $number, $district, $city, $state, $zip, $complement = null, $country = self::ADDRESS_COUNTRY) |
80
|
|
|
{ |
81
|
|
|
$address = new stdClass(); |
82
|
|
|
$address->street = $street; |
83
|
|
|
$address->streetNumber = $number; |
84
|
|
|
$address->complement = $complement; |
85
|
|
|
$address->district = $district; |
86
|
|
|
$address->city = $city; |
87
|
|
|
$address->state = $state; |
88
|
|
|
$address->country = $country; |
89
|
|
|
$address->zipCode = $zip; |
90
|
|
|
|
91
|
|
|
switch ($type) { |
92
|
|
|
case self::ADDRESS_BILLING: |
93
|
|
|
$this->data->billingAddress = $address; |
94
|
|
|
break; |
95
|
|
|
case self::ADDRESS_SHIPPING: |
96
|
|
|
$this->data->shippingAddress = $address; |
97
|
|
|
break; |
98
|
|
|
default: |
99
|
|
|
throw new UnexpectedValueException(sprintf('%s não é um tipo de endereço válido', $type)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Create a new customer. |
107
|
|
|
* |
108
|
|
|
* @return \stdClass |
109
|
|
|
*/ |
110
|
|
|
public function create() |
111
|
|
|
{ |
112
|
|
|
return $this->createResource(sprintf('/%s/%s/', MoipResource::VERSION, self::PATH)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Find a customer. |
117
|
|
|
* |
118
|
|
|
* @param string $moip_id |
119
|
|
|
* |
120
|
|
|
* @return \Moip\Resource\Customer|stdClass |
121
|
|
|
*/ |
122
|
|
|
public function get($moip_id) |
123
|
|
|
{ |
124
|
|
|
return $this->getByPath(sprintf('/%s/%s/%s', MoipResource::VERSION, self::PATH, $moip_id)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get customer id. |
129
|
|
|
* |
130
|
|
|
* @return string The buyer id. |
131
|
|
|
*/ |
132
|
|
|
public function getId() |
133
|
|
|
{ |
134
|
|
|
return $this->getIfSet('id'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get customer address. |
139
|
|
|
* |
140
|
|
|
* @return \stdClass Customer's address. |
141
|
|
|
*/ |
142
|
|
|
public function getBillingAddress() |
143
|
|
|
{ |
144
|
|
|
return $this->getIfSet('billingAddress'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get customer address. |
149
|
|
|
* |
150
|
|
|
* @return \stdClass Customer's address. |
151
|
|
|
*/ |
152
|
|
|
public function getShippingAddress() |
153
|
|
|
{ |
154
|
|
|
return $this->getIfSet('shippingAddress'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get customer fullname. |
159
|
|
|
* |
160
|
|
|
* @return string Customer's full name. |
161
|
|
|
*/ |
162
|
|
|
public function getFullname() |
163
|
|
|
{ |
164
|
|
|
return $this->getIfSet('fullname'); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get funding instrument from customer. |
169
|
|
|
* |
170
|
|
|
* @return \stdClass Structure that is the means of payment. |
171
|
|
|
*/ |
172
|
|
|
public function getFundingInstrument() |
173
|
|
|
{ |
174
|
|
|
return $this->getIfSet('fundingInstrument'); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Get birth date from customer. |
179
|
|
|
* |
180
|
|
|
* @return \DateTime|null Date of birth of the credit card holder. |
181
|
|
|
*/ |
182
|
|
|
public function getBirthDate() |
183
|
|
|
{ |
184
|
|
|
return $this->getIfSetDate('birthDate'); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get phone area code from customer. |
189
|
|
|
* |
190
|
|
|
* @return int DDD telephone. |
191
|
|
|
*/ |
192
|
|
|
public function getPhoneAreaCode() |
193
|
|
|
{ |
194
|
|
|
return $this->getIfSet('areaCode', $this->data->phone); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Get phone country code from customer. |
199
|
|
|
* |
200
|
|
|
* @return int Country code. |
201
|
|
|
*/ |
202
|
|
|
public function getPhoneCountryCode() |
203
|
|
|
{ |
204
|
|
|
return $this->getIfSet('countryCode', $this->data->phone); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Get phone number from customer. |
209
|
|
|
* |
210
|
|
|
* @return int Telephone number. |
211
|
|
|
*/ |
212
|
|
|
public function getPhoneNumber() |
213
|
|
|
{ |
214
|
|
|
return $this->getIfSet('number', $this->data->phone); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Get tax document type from customer. |
219
|
|
|
* |
220
|
|
|
* @return string Type of value: CPF and CNPJ |
221
|
|
|
*/ |
222
|
|
|
public function getTaxDocumentType() |
223
|
|
|
{ |
224
|
|
|
return $this->getIfSet('type', $this->data->taxDocument); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Get tax document number from customer. |
229
|
|
|
* |
230
|
|
|
* @return string Document Number. |
231
|
|
|
*/ |
232
|
|
|
public function getTaxDocumentNumber() |
233
|
|
|
{ |
234
|
|
|
return $this->getIfSet('number', $this->data->taxDocument); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Mount the buyer structure from customer. |
239
|
|
|
* |
240
|
|
|
* @param \stdClass $response |
241
|
|
|
* |
242
|
|
|
* @return Customer Customer information. |
243
|
|
|
*/ |
244
|
|
|
protected function populate(stdClass $response) |
245
|
|
|
{ |
246
|
|
|
$customer = clone $this; |
247
|
|
|
$customer->data = new stdClass(); |
248
|
|
|
$customer->data->id = $this->getIfSet('id', $response); |
249
|
|
|
$customer->data->ownId = $this->getIfSet('ownId', $response); |
250
|
|
|
$customer->data->fullname = $this->getIfSet('fullname', $response); |
251
|
|
|
$customer->data->email = $this->getIfSet('email', $response); |
252
|
|
|
$customer->data->phone = new stdClass(); |
253
|
|
|
|
254
|
|
|
$phone = $this->getIfSet('phone', $response); |
255
|
|
|
|
256
|
|
|
$customer->data->phone->countryCode = $this->getIfSet('countryCode', $phone); |
257
|
|
|
$customer->data->phone->areaCode = $this->getIfSet('areaCode', $phone); |
258
|
|
|
$customer->data->phone->number = $this->getIfSet('number', $phone); |
259
|
|
|
$customer->data->birthDate = $this->getIfSet('birthDate', $response); |
260
|
|
|
$customer->data->taxDocument = new stdClass(); |
261
|
|
|
$customer->data->taxDocument->type = $this->getIfSet('type', $this->getIfSet('taxDocument', $response)); |
262
|
|
|
$customer->data->taxDocument->number = $this->getIfSet('number', $this->getIfSet('taxDocument', $response)); |
263
|
|
|
$customer->data->addresses = []; |
264
|
|
|
$customer->data->shippingAddress = $this->getIfSet('shippingAddress', $response); |
265
|
|
|
$customer->data->billingAddress = $this->getIfSet('billingAddress', $response); |
266
|
|
|
$customer->data->fundingInstrument = $this->getIfSet('fundingInstrument', $response); |
267
|
|
|
|
268
|
|
|
$customer->data->_links = $this->getIfSet('_links', $response); |
269
|
|
|
|
270
|
|
|
return $customer; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Set Own id from customer. |
275
|
|
|
* |
276
|
|
|
* @param string $ownId Customer's own id. external reference. |
277
|
|
|
* |
278
|
|
|
* @return $this |
279
|
|
|
*/ |
280
|
|
|
public function setOwnId($ownId) |
281
|
|
|
{ |
282
|
|
|
$this->data->ownId = $ownId; |
283
|
|
|
|
284
|
|
|
return $this; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Set fullname from customer. |
289
|
|
|
* |
290
|
|
|
* @param string $fullname Customer's full name. |
291
|
|
|
* |
292
|
|
|
* @return $this |
293
|
|
|
*/ |
294
|
|
|
public function setFullname($fullname) |
295
|
|
|
{ |
296
|
|
|
$this->data->fullname = $fullname; |
297
|
|
|
|
298
|
|
|
return $this; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Set e-mail from customer. |
303
|
|
|
* |
304
|
|
|
* @param string $email Email customer. |
305
|
|
|
* |
306
|
|
|
* @return $this |
307
|
|
|
*/ |
308
|
|
|
public function setEmail($email) |
309
|
|
|
{ |
310
|
|
|
$this->data->email = $email; |
311
|
|
|
|
312
|
|
|
return $this; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Set credit card from customer. |
317
|
|
|
* |
318
|
|
|
* @param int $expirationMonth Card expiration month. |
319
|
|
|
* @param int $expirationYear Year card expiration. |
320
|
|
|
* @param int $number Card number. |
321
|
|
|
* @param int $cvc Card Security Code. |
322
|
|
|
* @param \Moip\Resource\Customer|null $holder Cardholder. |
323
|
|
|
* |
324
|
|
|
* @return $this |
325
|
|
|
*/ |
326
|
|
|
public function setCreditCard($expirationMonth, $expirationYear, $number, $cvc, Holder $holder = null) |
327
|
|
|
{ |
328
|
|
|
if ($holder === null) { |
329
|
|
|
$holder = $this; |
330
|
|
|
} |
331
|
|
|
$birthdate = $holder->getBirthDate(); |
332
|
|
|
if ($birthdate instanceof \DateTime) { |
333
|
|
|
$birthdate = $birthdate->format('Y-m-d'); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
$this->data->fundingInstrument = new stdClass(); |
337
|
|
|
$this->data->fundingInstrument->method = Payment::METHOD_CREDIT_CARD; |
338
|
|
|
$this->data->fundingInstrument->creditCard = new stdClass(); |
339
|
|
|
$this->data->fundingInstrument->creditCard->expirationMonth = $expirationMonth; |
340
|
|
|
$this->data->fundingInstrument->creditCard->expirationYear = $expirationYear; |
341
|
|
|
$this->data->fundingInstrument->creditCard->number = $number; |
342
|
|
|
$this->data->fundingInstrument->creditCard->cvc = $cvc; |
343
|
|
|
$this->data->fundingInstrument->creditCard->holder = new stdClass(); |
344
|
|
|
$this->data->fundingInstrument->creditCard->holder->fullname = $holder->getFullname(); |
345
|
|
|
$this->data->fundingInstrument->creditCard->holder->birthdate = $birthdate; |
346
|
|
|
$this->data->fundingInstrument->creditCard->holder->taxDocument = new stdClass(); |
347
|
|
|
$this->data->fundingInstrument->creditCard->holder->taxDocument->type = $holder->getTaxDocumentType(); |
348
|
|
|
$this->data->fundingInstrument->creditCard->holder->taxDocument->number = $holder->getTaxDocumentNumber(); |
349
|
|
|
$this->data->fundingInstrument->creditCard->holder->phone = new stdClass(); |
350
|
|
|
$this->data->fundingInstrument->creditCard->holder->phone->countryCode = $holder->getPhoneCountryCode(); |
351
|
|
|
$this->data->fundingInstrument->creditCard->holder->phone->areaCode = $holder->getPhoneAreaCode(); |
352
|
|
|
$this->data->fundingInstrument->creditCard->holder->phone->number = $holder->getPhoneNumber(); |
353
|
|
|
|
354
|
|
|
return $this; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Set birth date from customer. |
359
|
|
|
* |
360
|
|
|
* @param \DateTime|string $birthDate Date of birth of the credit card holder. |
361
|
|
|
* |
362
|
|
|
* @return $this |
363
|
|
|
*/ |
364
|
|
View Code Duplication |
public function setBirthDate($birthDate) |
|
|
|
|
365
|
|
|
{ |
366
|
|
|
if ($birthDate instanceof \DateTime) { |
367
|
|
|
$birthDate = $birthDate->format('Y-m-d'); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
$this->data->birthDate = $birthDate; |
371
|
|
|
|
372
|
|
|
return $this; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Set tax document from customer. |
377
|
|
|
* |
378
|
|
|
* @param string $number Document number. |
379
|
|
|
* @param string $type Document type. |
380
|
|
|
* |
381
|
|
|
* @return $this |
382
|
|
|
*/ |
383
|
|
|
public function setTaxDocument($number, $type = self::TAX_DOCUMENT) |
384
|
|
|
{ |
385
|
|
|
$this->data->taxDocument = new stdClass(); |
386
|
|
|
$this->data->taxDocument->type = $type; |
387
|
|
|
$this->data->taxDocument->number = $number; |
388
|
|
|
|
389
|
|
|
return $this; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* Set phone from customer. |
394
|
|
|
* |
395
|
|
|
* @param int $areaCode DDD telephone. |
396
|
|
|
* @param int $number Telephone number. |
397
|
|
|
* @param int $countryCode Country code. |
398
|
|
|
* |
399
|
|
|
* @return $this |
400
|
|
|
*/ |
401
|
|
View Code Duplication |
public function setPhone($areaCode, $number, $countryCode = 55) |
|
|
|
|
402
|
|
|
{ |
403
|
|
|
$this->data->phone = new stdClass(); |
404
|
|
|
$this->data->phone->countryCode = $countryCode; |
405
|
|
|
$this->data->phone->areaCode = $areaCode; |
406
|
|
|
$this->data->phone->number = $number; |
407
|
|
|
|
408
|
|
|
return $this; |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|
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.