|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Itau\API; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Itau\API\Exception\ItauException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class ItauCertificate |
|
10
|
|
|
* |
|
11
|
|
|
* @package Itau\API |
|
12
|
|
|
*/ |
|
13
|
|
|
class ItauCertificate |
|
14
|
|
|
{ |
|
15
|
|
|
private string $endpoint = 'https://sts.itau.com.br/seguranca/v1/certificado/solicitacao'; |
|
16
|
|
|
|
|
17
|
|
|
public function criarCertificado(string $token, string $certificadoCSR) |
|
18
|
|
|
{ |
|
19
|
|
|
return $this->executeRequest($token, $certificadoCSR); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function renovarCertificado(Itau $credentials, string $certificadoCSR) |
|
23
|
|
|
{ |
|
24
|
|
|
echo "<h1>API</h1>"; |
|
25
|
|
|
var_dump($credentials->getAuthorizationToken()); |
|
|
|
|
|
|
26
|
|
|
if (!$credentials->getAuthorizationToken()) { |
|
27
|
|
|
echo '<hr>'; |
|
28
|
|
|
var_dump($credentials); |
|
29
|
|
|
new Request($credentials); |
|
30
|
|
|
} |
|
31
|
|
|
$token = $credentials->getAuthorizationToken(); |
|
32
|
|
|
echo '<hr>'; |
|
33
|
|
|
var_dump($token); |
|
34
|
|
|
return $this->executeRequest($token, $certificadoCSR); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
private function executeRequest(string $token, string $certificadoCSR) |
|
38
|
|
|
{ |
|
39
|
|
|
$headers = [ |
|
40
|
|
|
'Content-Type: text/plain', |
|
41
|
|
|
'Authorization: Bearer ' . $token |
|
42
|
|
|
]; |
|
43
|
|
|
|
|
44
|
|
|
$curl = curl_init(); |
|
45
|
|
|
|
|
46
|
|
|
curl_setopt_array($curl, [ |
|
47
|
|
|
CURLOPT_URL => $this->endpoint, |
|
48
|
|
|
CURLOPT_HTTPHEADER => $headers, |
|
49
|
|
|
CURLOPT_RETURNTRANSFER => true, |
|
50
|
|
|
CURLOPT_CUSTOMREQUEST => 'POST', |
|
51
|
|
|
CURLOPT_POSTFIELDS => $certificadoCSR, |
|
52
|
|
|
]); |
|
53
|
|
|
|
|
54
|
|
|
try { |
|
55
|
|
|
$response = curl_exec($curl); |
|
56
|
|
|
} catch (Exception $e) { |
|
57
|
|
|
curl_close($curl); |
|
58
|
|
|
throw new ItauException($e->getMessage(), 100); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if ($response === false) { |
|
62
|
|
|
$error = curl_error($curl); |
|
63
|
|
|
curl_close($curl); |
|
64
|
|
|
throw new ItauException('CURL Error: ' . $error, 100); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$statusCode = (int) curl_getinfo($curl, CURLINFO_HTTP_CODE); |
|
68
|
|
|
curl_close($curl); |
|
69
|
|
|
|
|
70
|
|
|
// Verifica status HTTP |
|
71
|
|
|
if ($statusCode >= 400) { |
|
72
|
|
|
$obj = json_decode($response); |
|
|
|
|
|
|
73
|
|
|
$acao = $obj->acao ?? ''; |
|
74
|
|
|
$mensagem = $obj->mensagem ?? 'Erro desconhecido'; |
|
75
|
|
|
$acaoText = $acao ? "- {$acao}" : ''; |
|
76
|
|
|
throw new ItauException("HTTP Error: $statusCode - $mensagem {$acaoText}", $statusCode); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// Lógica para 204 |
|
80
|
|
|
if ($statusCode === 204) { |
|
81
|
|
|
return [ |
|
82
|
|
|
'status_code' => 204 |
|
83
|
|
|
]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// Verifica resposta vazia |
|
87
|
|
|
if (empty($response)) { |
|
88
|
|
|
throw new ItauException('Empty response received from server.', $statusCode); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $response; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|