-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNfseClient.php
More file actions
262 lines (219 loc) · 8.34 KB
/
NfseClient.php
File metadata and controls
262 lines (219 loc) · 8.34 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
// SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace LibreCodeCoop\NfsePHP\Http;
use LibreCodeCoop\NfsePHP\Config\CertConfig;
use LibreCodeCoop\NfsePHP\Config\EnvironmentConfig;
use LibreCodeCoop\NfsePHP\Contracts\NfseClientInterface;
use LibreCodeCoop\NfsePHP\Contracts\SecretStoreInterface;
use LibreCodeCoop\NfsePHP\Contracts\XmlSignerInterface;
use LibreCodeCoop\NfsePHP\Dto\DpsData;
use LibreCodeCoop\NfsePHP\Dto\ReceiptData;
use LibreCodeCoop\NfsePHP\Exception\CancellationException;
use LibreCodeCoop\NfsePHP\Exception\IssuanceException;
use LibreCodeCoop\NfsePHP\Exception\NetworkException;
use LibreCodeCoop\NfsePHP\Exception\NfseErrorCode;
use LibreCodeCoop\NfsePHP\Exception\QueryException;
use LibreCodeCoop\NfsePHP\Xml\DpsSigner;
use LibreCodeCoop\NfsePHP\Xml\XmlBuilder;
/**
* HTTP client for the SEFIN Nacional NFS-e REST API.
*
* Communicates with the SEFIN gateway to issue, query, and cancel NFS-e.
* All requests carry a signed DPS XML payload.
*/
class NfseClient implements NfseClientInterface
{
private readonly string $baseUrl;
private readonly XmlSignerInterface $signer;
public function __construct(
private readonly EnvironmentConfig $environment,
private readonly CertConfig $cert,
private readonly SecretStoreInterface $secretStore,
?XmlSignerInterface $signer = null,
) {
$this->baseUrl = $environment->baseUrl;
$this->signer = $signer ?? new DpsSigner($secretStore);
}
public function emit(DpsData $dps): ReceiptData
{
$xml = (new XmlBuilder())->buildDps($dps);
$signed = $this->signer->sign($xml, $dps->cnpjPrestador);
[$httpStatus, $body] = $this->post('/nfse', $signed);
if ($httpStatus >= 400) {
throw new IssuanceException(
'SEFIN gateway rejected issuance (HTTP ' . $httpStatus . ')',
NfseErrorCode::IssuanceRejected,
$httpStatus,
$body,
);
}
return $this->parseReceiptResponse($body);
}
public function query(string $chaveAcesso): ReceiptData
{
[$httpStatus, $body] = $this->get('/nfse/' . $chaveAcesso);
if ($httpStatus >= 400) {
throw new QueryException(
'SEFIN gateway returned error for query (HTTP ' . $httpStatus . ')',
NfseErrorCode::QueryFailed,
$httpStatus,
$body,
);
}
return $this->parseReceiptResponse($body);
}
public function cancel(string $chaveAcesso, string $motivo): bool
{
[$httpStatus, $body] = $this->delete('/dps/' . $chaveAcesso, $motivo);
if ($httpStatus >= 400) {
throw new CancellationException(
'SEFIN gateway rejected cancellation (HTTP ' . $httpStatus . ')',
NfseErrorCode::CancellationRejected,
$httpStatus,
$body,
);
}
return true;
}
// -------------------------------------------------------------------------
// Internal HTTP helpers
// -------------------------------------------------------------------------
/**
* @return array{int, array<string, mixed>}
*/
private function post(string $path, string $xmlPayload): array
{
$compressedPayload = gzencode($xmlPayload);
if ($compressedPayload === false) {
throw new NetworkException('Failed to compress DPS XML payload before transmission.');
}
$payload = json_encode([
'dpsXmlGZipB64' => base64_encode($compressedPayload),
], JSON_THROW_ON_ERROR);
$context = stream_context_create([
'http' => [
'method' => 'POST',
'header' => "Content-Type: application/json\r\nAccept: application/json\r\n",
'content' => $payload,
'ignore_errors' => true,
],
'ssl' => $this->sslContextOptions(),
]);
return $this->fetchAndDecode($path, $context);
}
/**
* @return array{int, array<string, mixed>}
*/
private function get(string $path): array
{
$context = stream_context_create([
'http' => [
'method' => 'GET',
'header' => "Accept: application/json\r\n",
'ignore_errors' => true,
],
'ssl' => $this->sslContextOptions(),
]);
return $this->fetchAndDecode($path, $context);
}
/**
* @return array{int, array<string, mixed>}
*/
private function delete(string $path, string $motivo): array
{
$payload = json_encode(['motivo' => $motivo], JSON_THROW_ON_ERROR);
$context = stream_context_create([
'http' => [
'method' => 'DELETE',
'header' => "Content-Type: application/json\r\nAccept: application/json\r\n",
'content' => $payload,
'ignore_errors' => true,
],
'ssl' => $this->sslContextOptions(),
]);
return $this->fetchAndDecode($path, $context);
}
/**
* @return array<string, bool|string>
*/
private function sslContextOptions(): array
{
$options = [
'verify_peer' => true,
'verify_peer_name' => true,
];
if ($this->cert->transportCertificatePath !== null && $this->cert->transportPrivateKeyPath !== null) {
$options['local_cert'] = $this->cert->transportCertificatePath;
$options['local_pk'] = $this->cert->transportPrivateKeyPath;
}
return $options;
}
/**
* Perform the raw HTTP request and decode the JSON body.
*
* PHP sets $http_response_header in the calling scope when file_get_contents
* uses an HTTP wrapper. We initialize it to [] so static analysers have a
* typed baseline; the HTTP wrapper will overwrite it on a successful
* connection, even when the server responds with 4xx/5xx.
*
* @return array{int, array<string, mixed>}
*/
private function fetchAndDecode(string $path, mixed $context): array
{
$url = $this->baseUrl . $path;
$http_response_header = [];
$body = file_get_contents($url, false, $context);
$httpStatus = $this->parseHttpStatus($http_response_header);
if ($body === false) {
throw new NetworkException('Failed to connect to SEFIN gateway at ' . $url);
}
$decoded = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
if (!is_array($decoded)) {
throw new NetworkException(
'Unexpected response format from SEFIN gateway',
NfseErrorCode::InvalidResponse,
);
}
return [$httpStatus, $decoded];
}
/**
* Extract the HTTP status code from the first response header line.
*
* @param list<string> $headers
*/
private function parseHttpStatus(array $headers): int
{
if (!isset($headers[0])) {
return 0;
}
if (preg_match('/HTTP\/[\d.]+ (\d{3})/', $headers[0], $m)) {
return (int) $m[1];
}
return 0;
}
/**
* @param array<string, mixed> $response
*/
private function parseReceiptResponse(array $response): ReceiptData
{
$rawXml = null;
if (isset($response['nfseXmlGZipB64']) && is_string($response['nfseXmlGZipB64'])) {
$decodedXml = base64_decode($response['nfseXmlGZipB64'], true);
if ($decodedXml !== false) {
$inflatedXml = gzdecode($decodedXml);
if ($inflatedXml !== false) {
$rawXml = $inflatedXml;
}
}
}
return new ReceiptData(
nfseNumber: (string) ($response['nNFSe'] ?? $response['numero'] ?? ''),
chaveAcesso: (string) ($response['chaveAcesso'] ?? ''),
dataEmissao: (string) ($response['dhEmi'] ?? $response['dataHoraProcessamento'] ?? $response['dataEmissao'] ?? ''),
codigoVerificacao: isset($response['codigoVerificacao']) ? (string) $response['codigoVerificacao'] : null,
rawXml: $rawXml,
);
}
}