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.
The content provided by Bilbabit SL in any of its sections (Blog, guides, landing pages, FAQ section, commercial or customer support emails) is for informational purposes only and has no legal validity. BILBABIT is not a tax consulting firm, but a fiscal software developer, so the information it provides is of an indicative nature. In any case, any activity related to invoicing and taxation should always and without exception be consulted with a tax advisor who understands the specific implications of each regulation for each company or professional in particular.