Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Contracts/NfseClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace LibreCodeCoop\NfsePHP\Contracts;

use LibreCodeCoop\NfsePHP\Dto\DecisionNfseData;
use LibreCodeCoop\NfsePHP\Dto\DpsData;
use LibreCodeCoop\NfsePHP\Dto\ReceiptData;

Expand All @@ -17,6 +18,11 @@ interface NfseClientInterface
*/
public function emit(DpsData $dps): ReceiptData;

/**
* Emit a complete NFS-e through the administrative/judicial decision bypass.
*/
public function emitDecision(DecisionNfseData $nfse): ReceiptData;

/**
* Query an existing NFS-e by its access key.
*/
Expand Down
38 changes: 36 additions & 2 deletions src/Http/NfseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use LibreCodeCoop\NfsePHP\Contracts\SecretStoreInterface;
use LibreCodeCoop\NfsePHP\Contracts\XmlSignerInterface;
use LibreCodeCoop\NfsePHP\Danfse\DanfseGenerator;
use LibreCodeCoop\NfsePHP\Dto\DecisionNfseData;
use LibreCodeCoop\NfsePHP\Dto\DpsData;
use LibreCodeCoop\NfsePHP\Dto\EventReceiptData;
use LibreCodeCoop\NfsePHP\Dto\EventRegistrationData;
Expand All @@ -30,6 +31,7 @@
use LibreCodeCoop\NfsePHP\Exception\QueryException;
use LibreCodeCoop\NfsePHP\Support\DpsIdentifier;
use LibreCodeCoop\NfsePHP\Support\GzipBase64;
use LibreCodeCoop\NfsePHP\Xml\DecisionNfseBuilder;
use LibreCodeCoop\NfsePHP\Xml\DpsSigner;
use LibreCodeCoop\NfsePHP\Xml\XmlBuilder;

Expand Down Expand Up @@ -79,6 +81,30 @@ public function emit(DpsData $dps): ReceiptData
return $this->parseReceiptResponse($body);
}

#[\Override]
public function emitDecision(DecisionNfseData $nfse): ReceiptData
{
$xml = (new DecisionNfseBuilder())->build($nfse);
$signed = $this->signer->sign($xml, $nfse->dps->cnpjPrestador);

[$httpStatus, $body] = $this->postCompressedXml(
'/decisao-judicial/nfse',
'xmlGZipB64',
$signed,
);

if ($httpStatus >= 400) {
throw new IssuanceException(
'SEFIN gateway rejected decision-flow issuance (HTTP ' . $httpStatus . ')',
NfseErrorCode::IssuanceRejected,
$httpStatus,
$body,
);
}

return $this->parseReceiptResponse($body);
}

public function query(string $chaveAcesso): ReceiptData
{
[$httpStatus, $body] = $this->get('/nfse/' . $chaveAcesso);
Expand Down Expand Up @@ -244,15 +270,23 @@ public function getDanfse(string $nfseXml): string
* @return array{int, array<string, mixed>}
*/
private function post(string $path, string $xmlPayload): array
{
return $this->postCompressedXml($path, 'dpsXmlGZipB64', $xmlPayload);
}

/**
* @return array{int, array<string, mixed>}
*/
private function postCompressedXml(string $path, string $field, string $xmlPayload): array
{
$compressedPayload = gzencode($xmlPayload);

if ($compressedPayload === false) {
throw new NetworkException('Failed to compress DPS XML payload before transmission.');
throw new NetworkException('Failed to compress fiscal XML payload before transmission.');
}

$payload = json_encode([
'dpsXmlGZipB64' => base64_encode($compressedPayload),
$field => base64_encode($compressedPayload),
], JSON_THROW_ON_ERROR);

return $this->fetchAndDecode($path, 'POST', $payload);
Expand Down
92 changes: 92 additions & 0 deletions tests/Unit/Http/InjectableTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use LibreCodeCoop\NfsePHP\Config\EnvironmentConfig;
use LibreCodeCoop\NfsePHP\Config\MunicipalParametersConfig;
use LibreCodeCoop\NfsePHP\Contracts\XmlSignerInterface;
use LibreCodeCoop\NfsePHP\Dto\DecisionIssuerAddressData;
use LibreCodeCoop\NfsePHP\Dto\DecisionNfseData;
use LibreCodeCoop\NfsePHP\Dto\DpsData;
use LibreCodeCoop\NfsePHP\Dto\HttpResponseData;
use LibreCodeCoop\NfsePHP\Dto\SubstitutionData;
Expand Down Expand Up @@ -81,6 +83,96 @@ public function sign(string $xml, string $cnpj): string
self::assertArrayHasKey('dpsXmlGZipB64', $payload);
}

public function testDecisionIssuanceUsesDedicatedBypassEndpointAndPayloadContract(): void
{
$transport = new FakeHttpTransport(new HttpResponseData(
201,
'{"nNFSe":"240","chaveAcesso":"DECISION-240","dataHoraProcessamento":"2026-10-04T12:00:00-03:00"}',
));

$client = new NfseClient(
environment: new EnvironmentConfig(
sandboxMode: true,
baseUrl: 'https://sefin.invalid.test/SefinNacional',
),
cert: new CertConfig(
cnpj: '11222333000181',
pfxPath: '/does/not/exist.p12',
vaultPath: 'test/no-secret-required',
),
secretStore: new NoOpSecretStore(),
signer: new class () implements XmlSignerInterface {
public function sign(string $xml, string $cnpj): string
{
return $xml;
}
},
transport: $transport,
);

$receipt = $client->emitDecision(new DecisionNfseData(
dps: new DpsData(
cnpjPrestador: '11222333000181',
municipioIbge: '3303302',
itemListaServico: '001',
valorServico: '1000.00',
aliquota: '5.00',
discriminacao: 'Consultoria em tecnologia da informacao',
tipoAmbiente: 2,
serie: '1',
numeroDps: '42',
dataCompetencia: '2026-10-04',
codigoTributacaoNacional: '010701',
opcaoSimplesNacional: 1,
regimeEspecialTributacao: 0,
tipoRetencaoIss: 1,
indicadorTributacao: 0,
),
numeroNfse: '240',
codigoNumerico: '123456789',
localEmissao: 'Niteroi',
localPrestacao: 'Niteroi',
descricaoTributacaoNacional: 'Consultoria em tecnologia da informacao',
emitenteNome: 'Prestador de Teste Ltda',
emitenteEndereco: new DecisionIssuerAddressData(
'Rua de Teste',
'100',
'Centro',
'3303302',
'RJ',
'24000000',
),
valorLiquido: '1000.00',
numeroDfse: '1',
codigoLocalIncidencia: '3303302',
localIncidencia: 'Niteroi',
baseCalculo: '1000.00',
aliquotaAplicada: '5.00',
valorIssqn: '50.00',
));

self::assertSame('240', $receipt->nfseNumber);
self::assertCount(1, $transport->requests);

$request = $transport->requests[0];
self::assertSame('POST', $request->method);
self::assertSame(
'https://sefin.invalid.test/SefinNacional/decisao-judicial/nfse',
$request->url,
);
self::assertNotNull($request->body);

$payload = json_decode($request->body, true, 512, JSON_THROW_ON_ERROR);
self::assertSame(['xmlGZipB64'], array_keys($payload));

$compressed = base64_decode((string) $payload['xmlGZipB64'], true);
self::assertNotFalse($compressed);
$xml = gzdecode($compressed);
self::assertNotFalse($xml);
self::assertStringContainsString('<NFSe', $xml);
self::assertStringContainsString('<cStat>102</cStat>', $xml);
}

public function testSubstitutionUsesNormalIssuanceEndpointWithoutExtraMutationRequest(): void
{
$transport = new FakeHttpTransport(new HttpResponseData(
Expand Down
Loading