codersfree/laravel-greenter
Paquete para emitir comprobantes electrónicos en Laravel con Greenter: firma digital, envío a SUNAT (SEE o REST), generación de XML firmados, manejo de CDR y representación impresa en HTML/PDF. Soporta emisión para múltiples empresas y envío de XML existente.
Installation
composer require codersfree/laravel-greenter
Publish the config file:
php artisan vendor:publish --provider="CodersFree\Greenter\GreenterServiceProvider" --tag="greenter-config"
Configuration
Update .env with your SUNAT credentials:
GREENTER_CLIENT_ID=your_client_id
GREENTER_CLIENT_SECRET=your_client_secret
GREENTER_ENVIRONMENT=sandbox|production
First Use Case: Generate a CFDI
use CodersFree\Greenter\Facades\Greenter;
$invoice = Greenter::invoice()
->setType('01') // Factura Electrónica
->setSeries('F001')
->setNumber('001-2024')
->setDate(new \DateTime())
->setSeller([
'ruc' => '12345678901',
'name' => 'EMPRESA S.A.',
'address' => 'Av. Ejemplo 123',
])
->setBuyer([
'ruc' => '98765432109',
'name' => 'CLIENTE S.A.',
])
->addItem([
'description' => 'Servicio de desarrollo',
'quantity' => 1,
'unitPrice' => 100.00,
'total' => 100.00,
])
->generate();
Invoice Generation
generate() to send to SUNAT and return the XML response.$invoice = $invoice->generate();
Invoice::create([
'xml' => $invoice->getXml(),
'sunat_response' => $invoice->getSunatResponse(),
]);
Credit Notes & Debit Notes
$creditNote = Greenter::creditNote()
->setRelatedInvoice('001-2024')
->setReason('Devolución parcial')
->addItem([...])
->generate();
Batch Processing
foreach ($orders as $order) {
$invoice = Greenter::invoice()
->setSeries($order->series)
->setNumber($order->number)
->setBuyer($order->client)
->addItems($order->items)
->generate();
// Store or queue for later
}
Webhook Handling
Route::post('/greenter/webhook', [GreenterWebhookController::class, 'handle']);
Queue Jobs for Async Processing Use Laravel Queues to avoid timeouts during SUNAT API calls:
InvoiceGenerateJob::dispatch($order)->onQueue('greenter');
Local Testing with Sandbox
Always test in sandbox mode before switching to production.
Error Handling Wrap API calls in try-catch blocks to handle SUNAT rejections gracefully:
try {
$invoice = Greenter::invoice()->generate();
} catch (\CodersFree\Greenter\Exceptions\SunatException $e) {
\Log::error("SUNAT Error: " . $e->getMessage());
// Retry or notify admin
}
RUC Validation
if (!preg_match('/^\d{11}$/', $ruc)) {
throw new \InvalidArgumentException("RUC inválido");
}
Timeouts
XML Schema Compliance
emissionDate, totalAmount). Use the package’s validation:
$invoice->validate(); // Throws if invalid
Environment Mismatches
GREENTER_ENVIRONMENT in .env to avoid production errors.Enable Debug Mode
Set GREENTER_DEBUG=true in .env to log raw API responses.
Inspect XML Output
Use dd($invoice->getXml()) to verify structure before sending.
SUNAT Test Credentials Use the SUNAT sandbox to test RUCs and scenarios.
Custom Templates Override the XML template by publishing and modifying:
php artisan vendor:publish --tag="greenter-views"
Additional Metadata Extend the builder with custom fields:
$invoice->setCustomField('custom_field', 'value');
Webhook Extensions
Subclass GreenterWebhookHandler to add custom logic for SUNAT callbacks.
Localization Translate error messages by extending the package’s language files.
How can I help you explore Laravel packages today?