-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathCncClient.php
More file actions
172 lines (146 loc) · 5.3 KB
/
CncClient.php
File metadata and controls
172 lines (146 loc) · 5.3 KB
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
<?php
namespace Nfse\Http\Client;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions;
use Nfse\Dto\Http\MensagemProcessamentoDto;
use Nfse\Enums\TipoAmbiente;
use Nfse\Http\Exceptions\NfseApiException;
use Nfse\Http\NfseContext;
class CncClient
{
private const URL_PRODUCTION = 'https://adn.nfse.gov.br/cnc';
private const URL_HOMOLOGATION = 'https://adn.producaorestrita.nfse.gov.br/cnc';
private Client $httpClient;
private ?string $tempCertFile = null;
public function __construct(private NfseContext $context)
{
$this->httpClient = $this->createHttpClient();
}
public function __destruct()
{
if ($this->tempCertFile !== null && file_exists($this->tempCertFile)) {
unlink($this->tempCertFile);
}
}
private function resolveCertificatePath(): string
{
if ($this->context->certificatePath !== null) {
return $this->context->certificatePath;
}
$this->tempCertFile = tempnam(sys_get_temp_dir(), 'nfse_cert_');
file_put_contents($this->tempCertFile, $this->context->certificateContent);
return $this->tempCertFile;
}
private function createHttpClient(): Client
{
$baseUrl = $this->context->ambiente === TipoAmbiente::Producao
? self::URL_PRODUCTION
: self::URL_HOMOLOGATION;
return new Client([
'base_uri' => $baseUrl,
'curl' => [
CURLOPT_SSLCERTTYPE => 'P12',
CURLOPT_SSLCERT => $this->resolveCertificatePath(),
CURLOPT_SSLCERTPASSWD => $this->context->certificatePassword,
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_TIMEOUT => 60,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
],
RequestOptions::HEADERS => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
],
]);
}
private function get(string $endpoint): array
{
try {
$response = $this->httpClient->get($endpoint);
$content = $response->getBody()->getContents();
$decoded = json_decode($content, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw NfseApiException::responseError('Resposta inválida (não é JSON): '.$content);
}
return $decoded;
} catch (GuzzleException $e) {
$this->handleException($e);
}
}
private function post(string $endpoint, array $data): array
{
try {
$response = $this->httpClient->post($endpoint, [
RequestOptions::JSON => $data,
]);
$content = $response->getBody()->getContents();
$decoded = json_decode($content, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw NfseApiException::responseError('Resposta inválida (não é JSON): '.$content);
}
return $decoded;
} catch (GuzzleException $e) {
$this->handleException($e);
}
}
/**
* CNC Consulta - Consulta dados atuais de um contribuinte
*/
public function consultarContribuinte(string $cpfCnpj): array
{
return $this->get("/consulta/cad/{$cpfCnpj}");
}
/**
* CNC Município - Baixa alterações no cadastro nacional via NSU
*/
public function baixarAlteracoesCadastro(int $nsu): array
{
return $this->get("/municipio/cad/{$nsu}");
}
/**
* CNC Recepção - Cadastra ou atualiza um contribuinte no CNC
*/
public function atualizarContribuinte(array $dados): array
{
return $this->post('', $dados);
}
private function mapMensagens(array $mensagens): array
{
return array_map(fn ($m) => new MensagemProcessamentoDto([
'mensagem' => $m['Mensagem'] ?? $m['mensagem'] ?? null,
'parametros' => $m['Parametros'] ?? $m['parametros'] ?? null,
'codigo' => $m['Codigo'] ?? $m['codigo'] ?? null,
'descricao' => $m['Descricao'] ?? $m['descricao'] ?? null,
'complemento' => $m['Complemento'] ?? $m['complemento'] ?? null,
]), $mensagens);
}
/**
* @param \Exception|GuzzleException $e
* @param array $decoded
* @return mixed
* @throws NfseApiException
*/
private function handleException(\Exception|GuzzleException $e)
{
$errorBody = '';
if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) {
$errorBody = $e->getResponse()->getBody()->getContents();
}
$parsedErrors = [];
if ($errorBody) {
$decoded = json_decode($errorBody, true);
if (is_array($decoded) && isset($decoded['erros']) && is_array($decoded['erros'])) {
$parsedErrors = $this->mapMensagens($decoded['erros']);
}
}
throw NfseApiException::requestError(
$e->getMessage().($errorBody ? "\nResposta: ".$errorBody : ''),
$e->getCode(),
$errorBody ?: null,
$parsedErrors
);
}
}