Skip to content

Commit 4d39411

Browse files
committed
fix(http): cancel nfse via eventos endpoint
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent ae8519f commit 4d39411

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

src/Http/NfseClient.php

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,19 @@ public function query(string $chaveAcesso): ReceiptData
8080

8181
public function cancel(string $chaveAcesso, string $motivo): bool
8282
{
83-
[$httpStatus, $body] = $this->delete('/dps/' . $chaveAcesso, $motivo);
83+
$eventoXml = $this->buildCancelEventXml($chaveAcesso, $motivo);
84+
$signedEventoXml = $this->signer->sign($eventoXml, $this->cert->cnpj);
85+
86+
$compressedEventoXml = gzencode($signedEventoXml);
87+
88+
if ($compressedEventoXml === false) {
89+
throw new NetworkException('Failed to compress cancellation event XML payload before transmission.');
90+
}
91+
92+
[$httpStatus, $body] = $this->postEvento(
93+
'/nfse/' . $chaveAcesso . '/eventos',
94+
base64_encode($compressedEventoXml),
95+
);
8496

8597
if ($httpStatus >= 400) {
8698
throw new CancellationException(
@@ -146,12 +158,15 @@ private function get(string $path): array
146158
/**
147159
* @return array{int, array<string, mixed>}
148160
*/
149-
private function delete(string $path, string $motivo): array
161+
private function postEvento(string $path, string $eventoXmlGZipB64): array
150162
{
151-
$payload = json_encode(['motivo' => $motivo], JSON_THROW_ON_ERROR);
163+
$payload = json_encode([
164+
'pedidoRegistroEventoXmlGZipB64' => $eventoXmlGZipB64,
165+
], JSON_THROW_ON_ERROR);
166+
152167
$context = stream_context_create([
153168
'http' => [
154-
'method' => 'DELETE',
169+
'method' => 'POST',
155170
'header' => "Content-Type: application/json\r\nAccept: application/json\r\n",
156171
'content' => $payload,
157172
'ignore_errors' => true,
@@ -162,6 +177,34 @@ private function delete(string $path, string $motivo): array
162177
return $this->fetchAndDecode($path, $context);
163178
}
164179

180+
private function buildCancelEventXml(string $chaveAcesso, string $motivo): string
181+
{
182+
$doc = new \DOMDocument('1.0', 'UTF-8');
183+
$doc->formatOutput = false;
184+
185+
$root = $doc->createElementNS('http://www.sped.fazenda.gov.br/nfse', 'pedRegEvento');
186+
$root->setAttribute('versao', '1.01');
187+
$doc->appendChild($root);
188+
189+
$infPedReg = $doc->createElement('infPedReg');
190+
$infPedReg->setAttribute('Id', 'PRE' . $chaveAcesso . '101101');
191+
$root->appendChild($infPedReg);
192+
193+
$infPedReg->appendChild($doc->createElement('tpAmb', $this->environment->sandboxMode ? '2' : '1'));
194+
$infPedReg->appendChild($doc->createElement('verAplic', 'akaunting-nfse'));
195+
$infPedReg->appendChild($doc->createElement('dhEvento', (new \DateTimeImmutable())->format('Y-m-d\\TH:i:sP')));
196+
$infPedReg->appendChild($doc->createElement('CNPJAutor', $this->cert->cnpj));
197+
$infPedReg->appendChild($doc->createElement('chNFSe', $chaveAcesso));
198+
199+
$e101101 = $doc->createElement('e101101');
200+
$e101101->appendChild($doc->createElement('xDesc', 'Cancelamento de NFS-e'));
201+
$e101101->appendChild($doc->createElement('cMotivo', '1'));
202+
$e101101->appendChild($doc->createElement('xMotivo', $motivo));
203+
$infPedReg->appendChild($e101101);
204+
205+
return $doc->saveXML($doc->documentElement) ?: '';
206+
}
207+
165208
/**
166209
* @return array<string, bool|string>
167210
*/

0 commit comments

Comments
 (0)