|
| 1 | +<?php |
| 2 | + |
| 3 | +use Nfse\Dto\Nfse\DpsData; |
| 4 | +use Nfse\Enums\TipoAmbiente; |
| 5 | +use Nfse\Http\NfseContext; |
| 6 | +use Nfse\Nfse; |
| 7 | +use Nfse\Support\IdGenerator; |
| 8 | + |
| 9 | +require_once __DIR__.'/../../vendor/autoload.php'; |
| 10 | + |
| 11 | +// 1. Lendo de banco de dados |
| 12 | +// $pfxContent = $pdo->query("SELECT conteudo FROM certificados WHERE id = 1")->fetchColumn(); |
| 13 | + |
| 14 | +// 2. Lendo de variável de ambiente em base64 |
| 15 | +// $pfxContent = base64_decode(getenv('CERTIFICADO_PFX_BASE64')); |
| 16 | + |
| 17 | +// 3. Lendo de um arquivo (equivalente ao comportamento anterior) |
| 18 | +$pfxContent = file_get_contents(__DIR__.'/../certs/contribuinte.pfx'); |
| 19 | + |
| 20 | +$certificatePassword = '[PASSWORD]'; |
| 21 | + |
| 22 | +// Passa certificateContent em vez de certificatePath |
| 23 | +$context = new NfseContext( |
| 24 | + ambiente: TipoAmbiente::Homologacao, |
| 25 | + certificatePath: null, |
| 26 | + certificatePassword: $certificatePassword, |
| 27 | + certificateContent: $pfxContent, |
| 28 | +); |
| 29 | + |
| 30 | +$nfse = new Nfse($context); |
| 31 | + |
| 32 | +try { |
| 33 | + $cnpjPrestador = '03279735000194'; |
| 34 | + $codigoMunicipio = '2304400'; |
| 35 | + $serie = '1'; |
| 36 | + $numero = '100'; |
| 37 | + |
| 38 | + $idDps = IdGenerator::generateDpsId( |
| 39 | + cpfCnpj: $cnpjPrestador, |
| 40 | + codIbge: $codigoMunicipio, |
| 41 | + serieDps: $serie, |
| 42 | + numDps: $numero |
| 43 | + ); |
| 44 | + |
| 45 | + $dps = new DpsData([ |
| 46 | + '@attributes' => ['versao' => '1.00'], |
| 47 | + 'infDPS' => [ |
| 48 | + '@attributes' => ['Id' => $idDps], |
| 49 | + 'tpAmb' => 2, |
| 50 | + 'dhEmi' => date('c'), |
| 51 | + 'verAplic' => 'SDK-PHP-1.0', |
| 52 | + 'serie' => $serie, |
| 53 | + 'nDPS' => $numero, |
| 54 | + 'dCompet' => date('Y-m-d'), |
| 55 | + 'tpEmit' => 1, |
| 56 | + 'cLocEmi' => $codigoMunicipio, |
| 57 | + 'prest' => [ |
| 58 | + 'CNPJ' => $cnpjPrestador, |
| 59 | + 'xNome' => 'Empresa de Teste', |
| 60 | + 'end' => [ |
| 61 | + 'endNac' => ['cMun' => $codigoMunicipio, 'CEP' => '60000000'], |
| 62 | + 'xLgr' => 'Rua Teste', |
| 63 | + 'nro' => '123', |
| 64 | + 'xBairro' => 'Centro', |
| 65 | + ], |
| 66 | + 'regTrib' => ['opSimpNac' => 1, 'regEspTrib' => 0], |
| 67 | + ], |
| 68 | + 'toma' => [ |
| 69 | + 'CNPJ' => '44827692000111', |
| 70 | + 'xNome' => 'Cliente de Teste', |
| 71 | + ], |
| 72 | + 'serv' => [ |
| 73 | + 'locPrest' => ['cLocPrestacao' => $codigoMunicipio], |
| 74 | + 'cServ' => ['cTribNac' => '010101', 'xDescServ' => 'Desenvolvimento de Software'], |
| 75 | + ], |
| 76 | + 'valores' => [ |
| 77 | + 'vServPrest' => ['vServ' => 100.00], |
| 78 | + 'trib' => [ |
| 79 | + 'tribMun' => ['tribISSQN' => 1, 'tpRetISSQN' => 1], |
| 80 | + 'tribFed' => ['piscofins' => ['CST' => '08']], |
| 81 | + 'totTrib' => ['indTotTrib' => 0], |
| 82 | + ], |
| 83 | + ], |
| 84 | + ], |
| 85 | + ]); |
| 86 | + |
| 87 | + $nfseData = $nfse->contribuinte()->emitir($dps); |
| 88 | + |
| 89 | + echo "NFS-e emitida com sucesso!\n"; |
| 90 | + echo 'Chave de Acesso: '.$nfseData->infNfse->id."\n"; |
| 91 | + |
| 92 | +} catch (\Exception $e) { |
| 93 | + echo 'Erro: '.$e->getMessage()."\n"; |
| 94 | +} |
0 commit comments