1
|
|
|
<?php |
2
|
|
|
namespace Itau\API; |
3
|
|
|
|
4
|
|
|
use Itau\API\BoleCode\BoleCode; |
5
|
|
|
use Itau\API\BoleCode\BoleCodeResponse; |
6
|
|
|
use Itau\API\Boleto\Boleto; |
7
|
|
|
use Itau\API\Boleto\BoletoResponse; |
8
|
|
|
use Itau\API\Pix\Pix; |
9
|
|
|
use Itau\API\Pix\PixResponse; |
10
|
|
|
use Itau\API\Valor\Valor; |
11
|
|
|
use Itau\API\Vencimento\Vencimento; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Itau |
15
|
|
|
* |
16
|
|
|
* @package Itau\API |
17
|
|
|
*/ |
18
|
|
|
class Itau |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
private string $client_id; |
22
|
|
|
private string $client_secret; |
23
|
|
|
private string $certificate; |
24
|
|
|
private string $certificateKey; |
25
|
|
|
private $environment; |
26
|
|
|
private $authorizationToken; |
27
|
|
|
|
28
|
|
|
private $debug = false; |
29
|
|
|
|
30
|
|
|
public function __construct(string $client_id, string $client_secret, string $certificate, string $certificateKey) |
31
|
|
|
{ |
32
|
|
|
$this->setClientId($client_id); |
33
|
|
|
$this->setClientSecret($client_secret); |
34
|
|
|
$this->setCertificate($certificate); |
35
|
|
|
$this->setCertificateKey($certificateKey); |
36
|
|
|
$this->setEnvironment(Environment::production()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function getClientId() |
44
|
|
|
{ |
45
|
|
|
return $this->client_id; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* |
50
|
|
|
* @param string $client_id |
51
|
|
|
*/ |
52
|
|
|
public function setClientId($client_id) |
53
|
|
|
{ |
54
|
|
|
$this->client_id = (string) $client_id; |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
|
|
public function getClientSecret() |
64
|
|
|
{ |
65
|
|
|
return $this->client_secret; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* |
70
|
|
|
* @param mixed $client_secret |
71
|
|
|
*/ |
72
|
|
|
public function setClientSecret($client_secret) |
73
|
|
|
{ |
74
|
|
|
$this->client_secret = (string) $client_secret; |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getCertificate() |
80
|
|
|
{ |
81
|
|
|
return $this->certificate; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function setCertificate($certificate) |
85
|
|
|
{ |
86
|
|
|
$this->certificate = (string) $certificate; |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getCertificateKey() |
92
|
|
|
{ |
93
|
|
|
return $this->certificateKey; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function setCertificateKey($certificateKey) |
97
|
|
|
{ |
98
|
|
|
$this->certificateKey = (string) $certificateKey; |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function getEnvironment(): Environment |
104
|
|
|
{ |
105
|
|
|
return $this->environment; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function setEnvironment(Environment $environment): self |
109
|
|
|
{ |
110
|
|
|
$this->environment = $environment; |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* |
117
|
|
|
* @return mixed |
118
|
|
|
*/ |
119
|
|
|
public function getAuthorizationToken() |
120
|
|
|
{ |
121
|
|
|
return $this->authorizationToken; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* |
126
|
|
|
* @param mixed $authorizationToken |
127
|
|
|
*/ |
128
|
|
|
public function setAuthorizationToken($authorizationToken) |
129
|
|
|
{ |
130
|
|
|
$this->authorizationToken = (string) $authorizationToken; |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* |
137
|
|
|
* @return bool|null |
138
|
|
|
*/ |
139
|
|
|
public function getDebug() |
140
|
|
|
{ |
141
|
|
|
return $this->debug; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* |
146
|
|
|
* @param bool|null $debug |
147
|
|
|
*/ |
148
|
|
|
public function setDebug($debug = false) |
149
|
|
|
{ |
150
|
|
|
$this->debug = $debug; |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function pix(Pix $pix): PixResponse |
156
|
|
|
{ |
157
|
|
|
$pixResponse = new PixResponse(); |
158
|
|
|
try{ |
159
|
|
|
if ($this->debug) { |
160
|
|
|
print $pix->toJSON(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$request = new Request($this); |
164
|
|
|
$response = $request->post($this, "{$this->getEnvironment()->getApiPixUrl()}/cob", $pix->toJSON()); |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
// Add fields do not return in response |
168
|
|
|
$pixResponse->mapperJson($pix->toArray()); |
169
|
|
|
// Add response fields |
170
|
|
|
$pixResponse->mapperJson($response); |
171
|
|
|
$pixResponse->setStatus(BaseResponse::STATUS_CONFIRMED); |
172
|
|
|
return $pixResponse; |
173
|
|
|
|
174
|
|
|
} catch (\Exception $e) { |
175
|
|
|
return $this->generateErrorResponse($pixResponse, $e); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function boleCode(BoleCode $boleCode): BoleCodeResponse |
180
|
|
|
{ |
181
|
|
|
$boleCodeResponse = new BoleCodeResponse(); |
182
|
|
|
try{ |
183
|
|
|
if ($this->debug) { |
184
|
|
|
print $boleCode->toJSON(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$request = new Request($this); |
188
|
|
|
$response = $request->post($this, "{$this->getEnvironment()->getApiBoleCodeUrl()}/boletos_pix", $boleCode->toJSON()); |
189
|
|
|
|
190
|
|
|
// Add fields do not return in response |
191
|
|
|
$boleCodeResponse->mapperJson($boleCode->toArray()); |
192
|
|
|
// Add response fields |
193
|
|
|
$boleCodeResponse->mapperJson($response); |
194
|
|
|
$boleCodeResponse->setStatus(BaseResponse::STATUS_CONFIRMED); |
195
|
|
|
return $boleCodeResponse; |
196
|
|
|
|
197
|
|
|
} catch (\Exception $e) { |
198
|
|
|
return $this->generateErrorResponse($boleCodeResponse, $e); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function alterarVencimentoBoleto($agencia, $contaComDigito, $carteira, $nossoNumero, Vencimento $vencimento) |
203
|
|
|
{ |
204
|
|
|
$boletoResponse = new BoletoResponse(); |
205
|
|
|
|
206
|
|
|
$path = str_pad($agencia, 4, '0', STR_PAD_LEFT).str_pad($contaComDigito, 8, '0', STR_PAD_LEFT).str_pad($carteira, 3, '0', STR_PAD_LEFT).str_pad($nossoNumero, 8, '0', STR_PAD_LEFT); |
207
|
|
|
$request = new Request($this); |
208
|
|
|
$response = $request->patch($this, "{$this->getEnvironment()->getApiBoletoUrl()}/boletos/{$path}/data_vencimento", $vencimento->toJSON()); |
209
|
|
|
$boletoResponse->mapperJson($response); |
210
|
|
|
|
211
|
|
|
return $boletoResponse; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function alterarValorBoleto($agencia, $contaComDigito, $carteira, $nossoNumero, Valor $valor) |
215
|
|
|
{ |
216
|
|
|
$boletoResponse = new BoletoResponse(); |
217
|
|
|
|
218
|
|
|
$path = str_pad($agencia, 4, '0', STR_PAD_LEFT).str_pad($contaComDigito, 8, '0', STR_PAD_LEFT).str_pad($carteira, 3, '0', STR_PAD_LEFT).str_pad($nossoNumero, 8, '0', STR_PAD_LEFT); |
219
|
|
|
$request = new Request($this); |
220
|
|
|
$response = $request->patch($this, "{$this->getEnvironment()->getApiBoletoUrl()}/boletos/{$path}/valor_nominal", $valor->toJSON()); |
221
|
|
|
$boletoResponse->mapperJson($response); |
222
|
|
|
|
223
|
|
|
return $boletoResponse; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function consultarBoleto($agencia, $contaComDigito, $nossoNumero) |
227
|
|
|
{ |
228
|
|
|
$boletoResponse = new BoletoResponse(); |
229
|
|
|
|
230
|
|
|
$id_beneficiario = str_pad($agencia, 4, '0', STR_PAD_LEFT).str_pad($contaComDigito, 8, '0', STR_PAD_LEFT); |
231
|
|
|
$nosso_numero = str_pad($nossoNumero, 8, '0', STR_PAD_LEFT); |
232
|
|
|
$request = new Request($this); |
233
|
|
|
$response = $request->get($this, "{$this->getEnvironment()->getApiBoletoConsultaUrl()}/boletos?id_beneficiario={$id_beneficiario}&nosso_numero={$nosso_numero}"); |
234
|
|
|
|
235
|
|
|
// Add response fields |
236
|
|
|
$boletoResponse->mapperJson($response); |
237
|
|
|
|
238
|
|
|
return $boletoResponse; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
public function baixarBoleto($agencia, $contaComDigito, $carteira, $nossoNumero) |
242
|
|
|
{ |
243
|
|
|
$boletoResponse = new BoletoResponse(); |
244
|
|
|
|
245
|
|
|
$path = str_pad($agencia, 4, '0', STR_PAD_LEFT).str_pad($contaComDigito, 8, '0', STR_PAD_LEFT).str_pad($carteira, 3, '0', STR_PAD_LEFT).str_pad($nossoNumero, 8, '0', STR_PAD_LEFT); |
246
|
|
|
$request = new Request($this); |
247
|
|
|
$response = $request->patch($this, "{$this->getEnvironment()->getApiBoletoUrl()}/boletos/{$path}/baixa", '{}'); |
248
|
|
|
// Add response fields |
249
|
|
|
$boletoResponse->mapperJson($response); |
250
|
|
|
|
251
|
|
|
return $boletoResponse; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
private function generateErrorResponse(BaseResponse $baseResponse, $e) |
255
|
|
|
{ |
256
|
|
|
$baseResponse->mapperJson(json_decode($e->getMessage(), true)); |
257
|
|
|
|
258
|
|
|
if (empty($baseResponse->getStatus())) { |
|
|
|
|
259
|
|
|
$baseResponse->setStatus(BaseResponse::STATUS_ERROR); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
return $baseResponse; |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.