greenter/ws
Cliente PHP para conectar con los Web Services de SUNAT y enviar comprobantes electrónicos. Parte del ecosistema Greenter, con documentación oficial y soporte vía issues/pull requests en el repositorio principal.
Installation
composer require greenter/ws
Ensure your Laravel project meets the package’s PHP version requirements (tested on PHP 7.2+).
Configuration Publish the config file:
php artisan vendor:publish --provider="Greenter\Ws\WsServiceProvider"
Update .env with your SUNAT credentials (e.g., SUNAT_USERNAME, SUNAT_PASSWORD, SUNAT_RUC).
First Use Case: Sending a Test Invoice
Use the WsClient facade to send a test invoice via SUNAT’s sandbox:
use Greenter\Ws\Facades\WsClient;
$response = WsClient::sendTestInvoice([
'ruc' => '12345678901',
'serie' => 'F001',
'numero' => '001-2023',
'fecha' => now()->format('Y-m-d'),
'monto' => 100.00,
'tipo' => '01', // Factura Electrónica
]);
dd($response->success(), $response->message());
config/ws.php for default settings (e.g., sandbox/production endpoints, timeout values).Greenter\Ws\Contracts\WsResponse for response handling patterns.Sending Comprobantes (Invoices, Credit Notes, etc.)
Use the WsClient facade to dispatch requests to SUNAT’s API:
$response = WsClient::sendInvoice($comprobanteData, [
'test' => env('SUNAT_SANDBOX', false), // Use sandbox
'timeout' => 30,
]);
ruc, serie, numero), but ensure your data matches SUNAT’s requirements.Consulting Status Check the status of a sent comprobante:
$status = WsClient::consultStatus('12345678901', 'F001', '001-2023');
Handling Responses Always check the response object:
if ($response->success()) {
// Process successful response (e.g., save to DB)
$this->saveComprobante($response->data());
} else {
$this->handleError($response->message(), $response->code());
}
SendComprobanteJob) to avoid timeouts:
SendComprobanteJob::dispatch($comprobanteData)->onQueue('sunat');
\Log::info('SUNAT Response', ['data' => $response->toArray()]);
ComprobanteSent) after successful API calls:
event(new ComprobanteSent($response->data()));
Greenter\Ws\XmlBuilder to customize XML payloads:
class CustomXmlBuilder extends \Greenter\Ws\XmlBuilder {
public function buildCustomTag() { ... }
}
Retryable interface.Deprecated Package
lempa/sunat.Sandbox vs. Production
test: true) before going live.XML Schema Validation
debugbar to inspect XML structure:
\Debugbar::info('XML Payload', $this->xmlBuilder->render());
Rate Limiting
sleep(2); // Add delay between API calls
Certificate Issues
// Temporarily disable SSL verification (not recommended for production)
$client = new \Greenter\Ws\WsClient(['verify' => false]);
config(['ws.log.enabled' => true]);
\Log::debug('Raw Response', $response->rawContent());
Custom Response Handlers
Override Greenter\Ws\Handlers\ResponseHandler to add business logic:
class CustomResponseHandler extends \Greenter\Ws\Handlers\ResponseHandler {
public function handle($response) {
if ($response->code() === '01') {
// Custom logic for SUNAT code 01
}
}
}
Add New Comprobante Types
Extend the WsClient to support additional SUNAT comprobantes (e.g., Guías de Remisión):
public function sendGuiaRemision(array $data) {
return $this->send('guia_remision', $data);
}
Mock SUNAT for Testing
Use Laravel’s Mockery to simulate SUNAT responses in unit tests:
$mock = Mockery::mock('Greenter\Ws\Contracts\WsClient');
$mock->shouldReceive('sendInvoice')->andReturn(new WsResponse(true, 'OK'));
How can I help you explore Laravel packages today?