1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Itau\API; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Itau |
9
|
|
|
* |
10
|
|
|
* @package Itau\API |
11
|
|
|
*/ |
12
|
|
|
class ItauCertificate |
13
|
|
|
{ |
14
|
|
|
public function requestCertificate(string $token, string $certificadoCSR) |
15
|
|
|
{ |
16
|
|
|
$endpoint = 'sts.itau.com.br/seguranca/v1/certificado/solicitacao'; |
17
|
|
|
$headers = [ |
18
|
|
|
'Content-Type: text/plain', |
19
|
|
|
'Authorization: Bearer ' . $token |
20
|
|
|
]; |
21
|
|
|
|
22
|
|
|
$curl = curl_init(); |
23
|
|
|
|
24
|
|
|
curl_setopt_array($curl, [ |
25
|
|
|
CURLOPT_URL => $endpoint, |
26
|
|
|
CURLOPT_HTTPHEADER => $headers, |
27
|
|
|
CURLOPT_RETURNTRANSFER => true, |
28
|
|
|
CURLOPT_CUSTOMREQUEST => 'POST', |
29
|
|
|
CURLOPT_POSTFIELDS => $certificadoCSR, |
30
|
|
|
]); |
31
|
|
|
|
32
|
|
|
$response = curl_exec($curl); |
33
|
|
|
|
34
|
|
|
if ($response === false) { |
35
|
|
|
$error = curl_error($curl); |
36
|
|
|
curl_close($curl); |
37
|
|
|
throw new Exception('CURL Error: ' . $error, 100); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// Verify error |
41
|
|
|
if ($response === false) { |
42
|
|
|
$errorMessage = curl_error($curl); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$statusCode = (int) curl_getinfo($curl, CURLINFO_HTTP_CODE); |
46
|
|
|
curl_close($curl); |
47
|
|
|
|
48
|
|
|
if ($statusCode >= 400) { |
49
|
|
|
// TODO see what it means code 100 |
50
|
|
|
throw new Exception($response, 100); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
// Status code 204 don't have content. That means $response will be always false |
53
|
|
|
// Provides a custom content for $response to avoid error in the next if logic |
54
|
|
|
if ($statusCode === 204) { |
55
|
|
|
return [ |
56
|
|
|
'status_code' => 204 |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (! $response) { |
61
|
|
|
throw new Exception("Empty response, curl_error: $errorMessage", $statusCode); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
return $response; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|