PHP
Cliente HTTP basado en cURL puro, sin dependencias externas. Funciona en cualquier PHP 7.4+ o 8.x, ideal para integraciones rápidas o ambientes shared hosting donde Composer no es opción.
<?php
// LxsClient.php
class LxsClient
{
private string $baseUrl;
private string $apiKey;
public function __construct(string $apiKey, string $baseUrl = 'https://api.lxsyscr.com')
{
$this->apiKey = $apiKey;
$this->baseUrl = $baseUrl;
}
public function emitir(array $data, ?string $idempotencyKey = null): array
{
$headers = [
'X-Api-Key: ' . $this->apiKey,
'Content-Type: application/json',
'Idempotency-Key: ' . ($idempotencyKey ?? $this->uuid4())
];
return $this->request('POST', '/api/v1/facturas', $headers, $data);
}
public function consultar(string $clave): array
{
return $this->request('GET', "/api/v1/facturas/$clave", ['X-Api-Key: ' . $this->apiKey]);
}
public function descargarPdf(string $clave): string
{
$ch = curl_init("{$this->baseUrl}/api/v1/facturas/$clave/pdf");
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['X-Api-Key: ' . $this->apiKey],
CURLOPT_TIMEOUT => 30
]);
return curl_exec($ch);
}
private function request(string $method, string $path, array $headers, ?array $body = null): array
{
$ch = curl_init($this->baseUrl . $path);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_TIMEOUT => 30
]);
if ($body) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($status >= 400) throw new Exception("HTTP $status: $response");
return json_decode($response, true);
}
private function uuid4(): string
{
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
}
}
// Uso:
$lxs = new LxsClient(getenv('LXS_API_KEY'));
$factura = $lxs->emitir([
'tipo' => 'FacturaElectronica',
'cliente' => ['idCliente' => 42],
'condicionVenta' => '01',
'medioPago' => '01',
'moneda' => 'CRC',
'lineas' => [['idProducto' => 100, 'cantidad' => 2]]
]);
echo "Clave: " . $factura['clave'];