1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodePhix\Asaas; |
4
|
|
|
|
5
|
|
|
use CodePhix\Asaas\Connection; |
6
|
|
|
use CodePhix\Asaas\Exceptions\ClienteException; |
7
|
|
|
use Exception; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Cliente |
11
|
|
|
* @package app\Asaas |
12
|
|
|
* |
13
|
|
|
* |
14
|
|
|
\"name\": \"".((!empty($data['name'])) ? $data['name'] : '')."\", |
15
|
|
|
\"email\": \"".((!empty($data['email'])) ? $data['email'] : '')."\", |
16
|
|
|
\"company\": \"".((!empty($data['company'])) ? $data['company'] : '')."\", |
17
|
|
|
\"phone\": \"".((!empty($data['phone'])) ? $data['phone'] : '')."\", |
18
|
|
|
\"mobilePhone\": \"".((!empty($data['mobilePhone'])) ? $data['mobilePhone'] : '')."\", |
19
|
|
|
\"postalCode\": \"".((!empty($data['postalCode'])) ? $data['postalCode'] : '')."\", |
20
|
|
|
\"address\": \"".((!empty($data['address'])) ? $data['address'] : '')."\", |
21
|
|
|
\"addressNumber\": \"".((!empty($data['addressNumber'])) ? $data['addressNumber'] : '')."\", |
22
|
|
|
\"complement\": \"".((!empty($data['complement'])) ? $data['complement'] : '')."\", |
23
|
|
|
\"province\": \"".((!empty($data['province'] )) ? $data['province'] : '')."\", |
24
|
|
|
\"city\": \"".((!empty($data['city'])) ? $data['city'] : '')."\", |
25
|
|
|
\"state\": \"".((!empty($data['state'])) ? $data['state'] : '')."\", |
26
|
|
|
\"cpfCnpj\": \"".((!empty($data['cpfCnpj'])) ? $data['cpfCnpj'] : '')."\", |
27
|
|
|
\"additionalEmails\": \"".((!empty($data['additionalEmails'])) ? $data['additionalEmails'] : '')."\", |
28
|
|
|
\"notificationDisabled\": ".((!empty($data['notificationDisabled']) && $data['notificationDisabled'] == 1) ? 'true' : 'false').", |
29
|
|
|
\"externalReference\": \"".((!empty($data['externalReference'])) ? $data['externalReference'] : '')."\" |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
class Cliente |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
public $http; |
36
|
|
|
protected $cliente; |
37
|
|
|
|
38
|
|
|
public $cli; |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
public function __construct() |
42
|
|
|
{ |
43
|
|
|
$this->http = new Connection; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Retorna array de clientes. |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
public function index() |
51
|
|
|
{ |
52
|
|
|
return $this->http->get('/customers'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// Retorna a listagem de clientes |
56
|
|
|
public function getAll($filtros = false){ |
|
|
|
|
57
|
|
|
return $this->http->get('/customers'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Retorna os dados do cliente de acordo com o Id |
61
|
|
|
public function getById($id){ |
62
|
|
|
return $this->http->get('/customers/'.$id); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Retorna os dados do cliente de acordo com o Email |
66
|
|
|
public function getByEmail($email){ |
67
|
|
|
$option = 'limit=1&email=' . $email; |
68
|
|
|
return $this->http->get('/customers', $option); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Insere um novo cliente |
72
|
|
|
public function create(array $dadosCliente){ |
73
|
|
|
$dadosCliente = $this->setCliente($dadosCliente); |
74
|
|
|
if(!empty($dadosCliente['error'])){ |
75
|
|
|
return $dadosCliente; |
76
|
|
|
}else { |
77
|
|
|
return $this->http->post('/customers', $dadosCliente); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Atualiza os dados do cliente |
82
|
|
|
public function update($id, array $dadosCliente){ |
83
|
|
|
$dadosCliente = $this->setCliente($dadosCliente); |
84
|
|
|
if(!empty($dadosCliente['error'])){ |
85
|
|
|
return $dadosCliente; |
86
|
|
|
}else { |
87
|
|
|
return $this->http->post('/customers/' . $id, $dadosCliente); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// Deleta uma cliente |
92
|
|
|
public function delete($id){ |
93
|
|
|
return $this->http->get('/customers/'.$id,'','DELETE'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
|
98
|
|
|
|
99
|
|
|
public function get($client_id) { |
100
|
|
|
return $this->http->get('/customers/' . $client_id); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Cria um novo cliente no Asaas. |
105
|
|
|
* @param Array $cliente |
106
|
|
|
* @return Boolean |
107
|
|
|
*/ |
108
|
|
|
public function create2($cliente) |
109
|
|
|
{ |
110
|
|
|
// Preenche as informações do cliente |
111
|
|
|
$cliente = $this->setCliente($cliente); |
112
|
|
|
|
113
|
|
|
// Faz o post e retorna array de resposta |
114
|
|
|
return $this->http->post('/customers', ['form_params' => $cliente]); |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Faz merge nas informações do cliente. |
120
|
|
|
* |
121
|
|
|
* @see https://asaasv3.docs.apiary.io/#reference/0/clientes/criar-novo-cliente |
122
|
|
|
* @param Array $cliente |
123
|
|
|
* @return Array |
124
|
|
|
*/ |
125
|
|
|
public function setCliente($cliente) |
126
|
|
|
{ |
127
|
|
|
try { |
128
|
|
|
if ( ! $this->cliente_valid($cliente) ) { |
129
|
|
|
return ClienteException::invalidClient(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$this->cliente = array( |
133
|
|
|
'name' => '', |
134
|
|
|
'cpfCnpj' => '', |
135
|
|
|
'email' => '', |
136
|
|
|
'phone' => '', |
137
|
|
|
'mobilePhone' => '', |
138
|
|
|
'address' => '', |
139
|
|
|
'addressNumber' => '', |
140
|
|
|
'complement' => '', |
141
|
|
|
'province' => '', |
142
|
|
|
'postalCode' => '', |
143
|
|
|
'externalReference' => '', |
144
|
|
|
'notificationDisabled' => '', |
145
|
|
|
'additionalEmails' => '' |
146
|
|
|
); |
147
|
|
|
|
148
|
|
|
$this->cliente = array_merge($this->cliente, $cliente); |
149
|
|
|
return $this->cliente; |
150
|
|
|
|
151
|
|
|
} catch (Exception $e) { |
152
|
|
|
return 'Erro ao definir o cliente. - ' . $e->getMessage(); |
|
|
|
|
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Verifica se os dados do cliente são válidos. |
158
|
|
|
* |
159
|
|
|
* @param array $cliente |
160
|
|
|
* @return Boolean |
161
|
|
|
*/ |
162
|
|
|
public function cliente_valid($cliente) |
163
|
|
|
{ |
164
|
|
|
return ! ( (empty($cliente['name']) OR empty($cliente['cpfCnpj']) OR empty($cliente['email'])) ? 1 : '' ); |
165
|
|
|
} |
166
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.