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(Connection $connection) |
||
42 | { |
||
43 | $this->http = $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 | $filtro = ''; |
||
58 | if(is_array($filtros)){ |
||
59 | if($filtros){ |
||
60 | foreach($filtros as $key => $f){ |
||
61 | if(!empty($f)){ |
||
62 | if($filtro){ |
||
63 | $filtro .= '&'; |
||
64 | } |
||
65 | $filtro .= $key.'='.$f; |
||
66 | } |
||
67 | } |
||
68 | $filtro = '?'.$filtro; |
||
69 | } |
||
70 | } |
||
71 | return $this->http->get('/customers'.$filtro); |
||
72 | } |
||
73 | |||
74 | // Retorna os dados do cliente de acordo com o Id |
||
75 | public function getById($id){ |
||
76 | return $this->http->get('/customers/'.$id); |
||
77 | } |
||
78 | |||
79 | // Retorna os dados do cliente de acordo com o Email |
||
80 | public function getByEmail($email){ |
||
81 | $option = 'limit=1&email=' . $email; |
||
82 | return $this->http->get('/customers', $option); |
||
83 | } |
||
84 | |||
85 | // Insere um novo cliente |
||
86 | public function create(array $dadosCliente){ |
||
87 | $dadosCliente = $this->setCliente($dadosCliente); |
||
88 | if(!empty($dadosCliente['error'])){ |
||
89 | return $dadosCliente; |
||
90 | }else { |
||
91 | return $this->http->post('/customers', $dadosCliente); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | // Atualiza os dados do cliente |
||
96 | public function update($id, array $dadosCliente){ |
||
97 | $dadosCliente = $this->setCliente($dadosCliente); |
||
98 | if(!empty($dadosCliente['error'])){ |
||
99 | return $dadosCliente; |
||
100 | }else { |
||
101 | return $this->http->post('/customers/' . $id, $dadosCliente); |
||
102 | } |
||
103 | } |
||
104 | |||
105 | // Deleta uma cliente |
||
106 | public function delete($id){ |
||
107 | return $this->http->get('/customers/'.$id,'','DELETE'); |
||
108 | } |
||
109 | |||
110 | |||
111 | |||
112 | |||
113 | public function get($client_id) { |
||
114 | return $this->http->get('/customers/' . $client_id); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Cria um novo cliente no Asaas. |
||
119 | * @param Array $cliente |
||
120 | * @return Boolean |
||
121 | */ |
||
122 | public function create2($cliente) |
||
123 | { |
||
124 | // Preenche as informações do cliente |
||
125 | $cliente = $this->setCliente($cliente); |
||
126 | |||
127 | // Faz o post e retorna array de resposta |
||
128 | return $this->http->post('/customers', ['form_params' => $cliente]); |
||
129 | |||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Faz merge nas informações do cliente. |
||
134 | * |
||
135 | * @see https://asaasv3.docs.apiary.io/#reference/0/clientes/criar-novo-cliente |
||
136 | * @param Array $cliente |
||
137 | * @return Array |
||
138 | */ |
||
139 | public function setCliente($cliente) |
||
140 | { |
||
141 | try { |
||
142 | if ( ! $this->cliente_valid($cliente) ) { |
||
143 | return ClienteException::invalidClient(); |
||
144 | } |
||
145 | |||
146 | $this->cliente = array( |
||
147 | 'name' => '', |
||
148 | 'cpfCnpj' => '', |
||
149 | 'email' => '', |
||
150 | 'phone' => '', |
||
151 | 'mobilePhone' => '', |
||
152 | 'address' => '', |
||
153 | 'addressNumber' => '', |
||
154 | 'complement' => '', |
||
155 | 'province' => '', |
||
156 | 'postalCode' => '', |
||
157 | 'externalReference' => '', |
||
158 | 'notificationDisabled' => '', |
||
159 | 'additionalEmails' => '' |
||
160 | ); |
||
161 | |||
162 | $this->cliente = array_merge($this->cliente, $cliente); |
||
163 | return $this->cliente; |
||
164 | |||
165 | } catch (Exception $e) { |
||
166 | return 'Erro ao definir o cliente. - ' . $e->getMessage(); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
167 | } |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Verifica se os dados do cliente são válidos. |
||
172 | * |
||
173 | * @param array $cliente |
||
174 | * @return Boolean |
||
175 | */ |
||
176 | public function cliente_valid($cliente) |
||
177 | { |
||
178 | return ! ( (empty($cliente['name']) OR empty($cliente['cpfCnpj']) OR empty($cliente['email'])) ? 1 : '' ); |
||
179 | } |
||
180 | } |