brazilianfriendsofsymfony/nota-fiscal-bundle
Installation
composer require brazilianfriends/symfony-nota-fiscal-bundle
Add to config/bundles.php:
return [
// ...
BrazilianFriendsOfSymfony\NotaFiscalBundle\BFOSNotaFiscalBundle::class => ['all' => true],
];
First Use Case: Generate a NF-e (Nota Fiscal Eletrônica)
Configure the bundle in config/packages/bfos_nota_fiscal.yaml:
bfos_nota_fiscal:
environment: 'homologacao' # or 'producao'
cnpj: '12345678901234'
certificate_path: '%kernel.project_dir%/path/to/certificate.pfx'
certificate_password: 'your_password'
Use in a controller:
use BrazilianFriendsOfSymfony\NotaFiscalBundle\Service\NotaFiscalService;
public function generateNFe(NotaFiscalService $notaFiscalService)
{
$nfe = $notaFiscalService->createNFe([
'cnpj' => '98765432109876',
'serie' => '1',
'numero' => '123',
'valor' => 100.00,
'destinatario' => [
'cnpj' => '11122233344455',
'nome' => 'Destinatário Teste',
],
]);
return $nfe->getXml();
}
Key Files to Review
config/packages/bfos_nota_fiscal.yaml (Configuration)src/Service/NotaFiscalService.php (Core service)src/Entity/NotaFiscal.php (Data structure)Generating NF-e
$notaFiscalService->createNFe([
'cnpj' => '98765432109876',
'serie' => '1',
'numero' => '123',
'valor' => 100.00,
'destinatario' => [...],
'produtos' => [ // Optional
['descricao' => 'Produto 1', 'quantidade' => 2, 'valor' => 50.00],
],
]);
Consulting NF-e Status
$status = $notaFiscalService->consultarNFeStatus($nfe->getProtocolo());
Canceling NF-e
$notaFiscalService->cancelarNFe($nfe->getProtocolo(), 'Motivo de cancelamento');
Event Listeners
Subscribe to events like nfe.created or nfe.cancelled:
# config/services.yaml
services:
App\EventListener\NotaFiscalListener:
tags:
- { name: 'kernel.event_listener', event: 'nfe.created', method: 'onNFeCreated' }
HttpClient via GuzzleHttp for API calls if needed.class NotaFiscal extends Model
{
protected $casts = [
'protocolo' => 'string',
'xml' => 'text',
];
}
dispatch(new GenerateNFeJob($data));
Certificate Handling
.pfx certificate is in the correct path and has the right permissions.homologacao environment before switching to producao.Timeouts
try {
$response = $notaFiscalService->createNFe($data);
} catch (TimeoutException $e) {
retry()->times(3)->try(fn() => $notaFiscalService->createNFe($data));
}
Validation Errors
$errors = $notaFiscalService->parseSefasError($response);
Environment Switching
homologacao first. The producao environment has real financial implications.bfos_nota_fiscal.yaml:
debug: true
Monolog\Logger::addDebugChannel('sefas');
Custom Services
Extend NotaFiscalService to add business logic:
class CustomNotaFiscalService extends NotaFiscalService
{
public function createNFeWithCustomRules(array $data)
{
// Add validation or transformations
return parent::createNFe($data);
}
}
Event Customization
Override default events in config/packages/bfos_nota_fiscal.yaml:
events:
nfe.created: ['App\EventListener\CustomNFeListener::onNFeCreated']
SEFAZ Endpoint Overrides For testing, override the SEFAZ URL:
endpoints:
nfe: 'https://homologacao.sefas.test/nfe'
How can I help you explore Laravel packages today?