Help for developers to integrate Verifactu with PHP: learn to make REST requests, generate invoices with QR, sign with digital certificate and create XML efficiently.

To comply with the Verifactu requirements, there are two main options:
Integrate directly with the tax authority: This approach presents several technical challenges, including:
Use a REST API like Verifacti: A simple solution that allows you to make requests in JSON without the need for a digital certificate and without the technical challenges of direct integration. See Verifactu API documentation.
When using a REST API like Verifactu, you need to be able to make calls to this type of API. To make an HTTP request to a REST API from PHP, you can use different methods. The most common libraries are:
Below is an example of how to make the same request using cURL in PHP:
<?php
$url = "https://api.verifacti.com/verifactu/create";
$apiKey = "<API_KEY>";
$data = '{
"serie": "A",
"numero": "234634",
"fecha_expedicion": "02-12-2024",
"tipo_factura": "F1",
"descripcion": "Descripcion de la operacion",
"nif": "A15022510",
"nombre": "Nombre cliente",
"lineas": [{
"base_imponible": "200",
"tipo_impositivo": "21",
"cuota_repercutida": "42"
}],
"importe_total": "242"
}';
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer " . $apiKey,
"Content-Type: application/json"
),
));
$response = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error) {
echo "cURL Error: " . $error;
} else {
echo $response;
}
?>Part of the integration will require attaching a QR code to a PDF document from PHP. For this you can use:
Example with FPDF:
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->Image('qr_code.png', 10, 10, 50, 50);
$pdf->Output('factura.pdf', 'F');If you choose to generate the QR yourself, to generate a QR code in PHP, you can use libraries such as:
Example using QRcode:
include('phpqrcode/qrlib.php');
QRcode::png('Texto de ejemplo', 'qr_code.png');Note: This step would be unnecessary if using Verifacti, since the API provides the QR code.
Help for developers to integrate Verifactu with Python: learn to make REST requests, generate invoices with QR, sign with digital certificate and create XML efficiently.
Complete collection of Verifactu invoice examples: simplified, standard, corrective, intra-Community, export, IGIC, IPSI, cancellation, and more. Ready-to-copy JSON.
Verifactu mandatory compliance. Who is required to comply with VeriFactu. Mandatory compliance for computerized invoicing systems, legal entities and self-employed workers.