PHP code example of paseto / nfse-betha
1. Go to this page and download the library: Download paseto/nfse-betha 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/ */
paseto / nfse-betha example snippets
use Paseto\NFSeBetha\NFSeBetha;
// Read certificate content
$certificateContent = file_get_contents('/path/to/your/certificate.pfx');
// Define prestador data once
$prestadorData = [
'cnpj' => '12345678000195',
'inscricao_municipal' => '12345'
];
// Initialize the API with certificate content and prestador data
$nfseAPI = new NFSeBetha($certificateContent, $certificatePassword, $prestadorData);
// Generate NFSe (no prestador needed - uses constructor data!)
$nfse = $nfseAPI->gerarNfse([
'rps' => [
'numero' => '123',
'serie' => '1',
'tipo' => '1',
'data_emissao' => date('Y-m-d'),
'status' => '1'
],
'competencia' => date('Y-m-d'),
'servico' => [
'valores' => [
'valor_servicos' => '100.00',
'valor_deducoes' => '0.00',
'valor_iss' => '3.75',
'aliquota' => '3.75',
'desconto_incondicionado' => '0.00',
'desconto_condicionado' => '0.00'
],
'iss_retido' => '2',
'item_lista_servico' => '0103',
'discriminacao' => 'Serviços de consultoria em tecnologia da informação',
'codigo_municipio' => '4204608',
'exigibilidade_iss' => '1'
],
'tomador' => [
'identificacao' => [
'cnpj' => '02509015000105',
'inscricao_municipal' => '22540'
],
'razao_social' => 'EMPRESA EXEMPLO LTDA',
'endereco' => [
'logradouro' => 'RUA EXEMPLO',
'numero' => '123',
'bairro' => 'CENTRO',
'codigo_municipio' => '4204608',
'uf' => 'SC',
'cep' => '88000000'
]
],
'optante_simples_nacional' => '1',
'incentivo_fiscal' => '2'
]);
// Check result
if ($nfse !== false) {
echo "NFSe generated successfully!";
echo "Number: " . $nfse['ListaNfse']['CompNfse']['Nfse']['InfNfse']['Numero'];
} else {
echo "Error: " . $nfseAPI->getLastError();
}
// Cancel NFSe
$cancellation = $nfseAPI->cancelarNfse([
'numero' => '1',
'codigo_municipio' => '4204608',
'codigo_cancelamento' => '1'
]);
if ($cancellation !== false) {
echo "NFSe cancelled successfully!";
} else {
echo "Error: " . $nfseAPI->getLastError();
}
// Consult NFSe by range
$consultation = $nfseAPI->consultarNfseFaixa([
'faixa' => [
'numero_inicial' => '1',
'numero_final' => '10'
],
'pagina' => '1'
]);
$certificatePath = '/secure/path/to/certificate.pfx';
$certificatePassword = 'your-certificate-password';
$certificatePath = $_ENV['NFSE_CERTIFICATE_PATH'];
$certificatePassword = $_ENV['NFSE_CERTIFICATE_PASSWORD'];
try {
$result = $nfseAPI->gerarNfse($rpsData);
if ($result === false) {
// Check specific error
$error = $nfseAPI->getLastError();
echo "Error: " . $error;
} else {
// Success - process result
echo "Success!";
}
} catch (Exception $e) {
// Handle exceptions
echo "Exception: " . $e->getMessage();
}