-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBancoInter.class.php
211 lines (154 loc) · 6.6 KB
/
BancoInter.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
class BancoInter
{
private $contaCorrente, $clientId, $clientSecret, $certPath, $keyPath, $tokenPath, $baseUrl, $scope, $cpfCnpj;
public function __construct(string $type = '')
{
$pathFiles = '/home/account/keys/';
$this->clientId = '5';
$this->clientSecret = '';
$this->certPath = $pathFiles . 'your-file.crt';
$this->keyPath = $pathFiles . 'your-file.key';
$this->tokenPath = $pathFiles . 'bearer.token';
$this->cpfCnpj = '123.123.0001/09';
$this->contaCorrente = '1234567';
$this->baseUrl = 'https://cdpj.partners.bancointer.com.br';
$this->scope = 'boleto-cobranca.read boleto-cobranca.write';
}
function getBearerToken()
{
if (!file_exists($this->tokenPath) || file_exists($this->tokenPath) && (time() - filemtime($this->tokenPath) > 3000))
return $this->generateBearerToken();
return file_get_contents($this->tokenPath);
}
function generateBearerToken()
{
$response = $this->request('POST', 'oauth/v2/token', [
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'scope' => $this->scope,
'grant_type' => 'client_credentials',
], true);
file_put_contents($this->tokenPath, $response['access_token']);
return $response['access_token'];
}
function request(string $method, string $endpoint, array $data = [], bool $noAuth = false)
{
if ($noAuth === false)
$BearerToken = $this->getBearerToken();
$ch = curl_init();
$url = $this->baseUrl . '/' . ltrim($endpoint, '/');
$method = strtoupper($method);
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_SSLCERT => $this->certPath,
CURLOPT_SSLKEY => $this->keyPath,
CURLOPT_RETURNTRANSFER => true,
]);
$headers = $noAuth === true ? array('Content-Type: application/x-www-form-urlencoded') : array('Authorization: Bearer ' . $BearerToken, 'x-conta-corrente: ' . $this->contaCorrente, 'Content-Type: application/json');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
if ($method === 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $noAuth === true ? http_build_query($data) : json_encode($data));
} elseif (in_array($method, ['PUT', 'DELETE'], true)) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_POSTFIELDS, $method === 'DELETE' ? http_build_query($data) : json_encode($data));
} elseif ($method === 'GET' && !empty($data)) {
$url .= '?' . http_build_query($data);
curl_setopt($ch, CURLOPT_URL, $url);
}
$serverResponse = curl_exec($ch);
if ($serverResponse === false) {
$error = curl_error($ch);
curl_close($ch);
throw new Exception("Erro cURL: $error");
}
curl_close($ch);
$response = json_decode($serverResponse, true);
if ($serverResponse != '' && (json_last_error() !== JSON_ERROR_NONE)) {
$serverResponseJson = json_encode($serverResponse);
throw new Exception("Erro ao decodificar JSON: " . json_last_error_msg() . " - Mensagem recebida:" . $serverResponseJson);
}
return $response;
}
function webhookGet()
{
$result = $this->request('GET', 'cobranca/v3/cobrancas/webhook');
if ($result !== NULL && !isset($result['webhookUrl']))
throw new Exception("Erro ao recuperar webhook cadastrado: " . json_encode($result));
return $result;
}
function webhookSet($url)
{
$result = $this->request('PUT', 'cobranca/v3/cobrancas/webhook', ['webhookUrl' => $url]);
if (is_array($result))
throw new Exception("Erro ao cadastrar webhook: " . json_encode($result));
return true;
}
function webhookDelete()
{
$result = $this->request('DELETE', 'cobranca/v3/cobrancas/webhook');
if (is_array($result))
throw new Exception("Erro ao deletar webhook: " . json_encode($result));
return true;
}
function webhookCallbacks($params)
{
$result = $this->request('GET', 'cobranca/v3/cobrancas/webhook/callbacks', $params);
if (!isset($result['totalElementos']))
throw new Exception("Erro ao recuperar webhook cadastrado: " . json_encode($result));
return $result;
}
function cobrancaSet(array $params)
{
$params['mensagem'] = [
"linha1" => "",
"linha2" => "",
"linha3" => "",
"linha4" => "",
"linha5" => ""
];
$params['beneficiarioFinal'] = [
"cpfCnpj" => $this->cpfCnpj,
"tipoPessoa" => "JURIDICA",
"nome" => "Nome do beneficiário",
"endereco" => "Avenida Brasil, 1200",
"bairro" => "Centro",
"cidade" => "Belo Horizonte",
"uf" => "MG",
"cep" => "30110000"
];
$result = $this->request('POST', 'cobranca/v3/cobrancas', $params);
if (!isset($result['codigoSolicitacao']))
throw new Exception("Erro ao gerar cobrança: " . json_encode($result));
return $result['codigoSolicitacao'];
}
function cobrancaGet(string $codigoSolicitacao)
{
$result = $this->request('GET', 'cobranca/v3/cobrancas/' . $codigoSolicitacao);
if (!isset($result['cobranca']))
throw new Exception("Erro ao recuperar cobrança: " . json_encode($result));
return $result;
}
function cobrancaGetPdf(string $codigoSolicitacao)
{
$result = $this->request('GET', 'cobranca/v3/cobrancas/' . $codigoSolicitacao . '/pdf');
if (!isset($result['pdf']))
throw new Exception("Erro ao recuperar cobrança: " . json_encode($result));
return $result['pdf'];
}
function cobrancaCancel(string $codigoSolicitacao, string $motive)
{
$result = $this->request('POST', 'cobranca/v3/cobrancas/' . $codigoSolicitacao . '/cancelar', ['motivoCancelamento' => $motive]);
if (is_array($result))
throw new Exception("Erro ao recuperar cobrança: " . json_encode($result));
return true;
}
function cobrancaList(array $params = [])
{
$result = $this->request('GET', 'cobranca/v3/cobrancas', $params);
if (!isset($result['cobrancas']))
throw new Exception("Erro ao listar cobranças: " . json_encode($result));
return $result;
}
}