PHP code example of mendesalexandre / php-nfse-nacional
1. Go to this page and download the library: Download mendesalexandre/php-nfse-nacional library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mendesalexandre / php-nfse-nacional example snippets
use PhpNfseNacional\NFSe;
use PhpNfseNacional\Config;
use PhpNfseNacional\Certificate\Certificate;
use PhpNfseNacional\DTO\{Prestador, Tomador, Endereco, Servico, Valores, Identificacao};
use PhpNfseNacional\Enums\{Ambiente, RegimeEspecialTributacao};
// 1. Carregue o certificado A1
$cert = Certificate::fromPfxFile('/path/cert.pfx', 'senha-do-pfx');
// 2. Configure o prestador (singleton, uma vez na app)
$prestador = new Prestador(
cnpj: '12345678000195',
inscricaoMunicipal: '11408',
razaoSocial: 'EMPRESA XYZ',
endereco: new Endereco(
logradouro: 'R DAS NOGUEIRAS',
numero: '1108',
bairro: 'SETOR COMERCIAL',
cep: '01310100',
codigoMunicipioIbge: '3550308',
uf: 'MT',
),
regimeEspecial: RegimeEspecialTributacao::NotarioOuRegistrador,
);
$config = new Config(
prestador: $prestador,
ambiente: Ambiente::Homologacao,
);
// 3. Crie o SDK
$nfse = NFSe::create($config, $cert);
// 4. Emita uma NFS-e
$resposta = $nfse->emitir(
identificacao: new Identificacao(numeroDps: 1, serie: '1'),
tomador: new Tomador(
documento: '12345678901',
nome: 'Cliente Exemplo',
endereco: new Endereco(
logradouro: 'Rua A',
numero: '100',
bairro: 'Centro',
cep: '01310100',
codigoMunicipioIbge: '3550308',
uf: 'MT',
),
),
servico: new Servico(
discriminacao: 'Certidão de matrícula',
codigoMunicipioPrestacao: '3550308',
),
valores: new Valores(
valorServicos: 100.00,
deducoesReducoes: 20.00,
aliquotaIssqnPercentual: 4.00,
),
);
echo "Chave: " . $resposta->chaveAcesso;
echo "Número: " . $resposta->numeroNfse;
use PhpNfseNacional\DTO\MotivoCancelamento;
$resposta = $nfse->cancelar(
chaveAcesso: '35012345200001234567890123456789012345678123456789',
motivo: MotivoCancelamento::ErroEmissao,
justificativa: 'Valor da NFS-e divergente do recibo',
);
use PhpNfseNacional\DTO\MotivoCancelamento;
use PhpNfseNacional\DTO\MotivoSubstituicao;
$resposta = $nfse->substituir(
chaveOriginal: '51079092200179028000138000000000005726057774456203',
chaveSubstituta: '51079092200179028000138000000000005826057774456204',
motivo: MotivoSubstituicao::DesenquadramentoSimples,
);
use PhpNfseNacional\Dps\EventoNfse;
use PhpNfseNacional\Dps\EventoBuilder;
final class MeuEventoCustomizado implements EventoNfse
{
public function chaveAcesso(): string { return '...'; }
public function codigoTipoEvento(): string { return '101102'; } // ex: substituição
public function nSequencial(): int { return 1; }
public function descricao(): string { return 'Substituição de NFS-e'; }
public function camposGrupo(): array { return ['campoX' => 'valor']; }
}
$xml = (new EventoBuilder($config))->build(new MeuEventoCustomizado());
$xmlAssinado = $signer->sign($xml, 'infPedReg');
$resposta = $client->postXml($endpoints->cancelarNfse($chave), $xmlAssinado);
use PhpNfseNacional\Certificate\Signer;
Signer::habilitarLegacyProviderRuntime();
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.