Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Eseperio/verifactu-php/llms.txt
Use this file to discover all available pages before exploring further.
VerifactuService is the central static class that orchestrates all communication with the AEAT VERI*FACTU web service. It wires together certificate management, XML serialisation, hash generation, XML signing, SOAP transport, and response parsing into the four primary operations: register, cancel, query, and QR generation.
Namespace: eseperio\verifactu\services\VerifactuService
Configuration constants
These string constants are the accepted keys for theconfig() method and for getConfig().
| Constant | Value | Description |
|---|---|---|
WSDL_ENDPOINT | wsdl | Path or URL to the AEAT WSDL file. Defaults to the bundled SistemaFacturacion.wsdl when omitted. |
SOAP_ENDPOINT | soapEndpoint | Override the SOAP endpoint URL (e.g. to point at the AEAT test environment). When omitted the endpoint from the WSDL is used. |
CERT_PATH_KEY | certPath | Absolute filesystem path to the PFX/P12 or PEM certificate used for SOAP authentication and XML signing. |
CERT_PASSWORD_KEY | certPassword | Password for the certificate file. |
QR_VERIFICATION_URL | qrValidationUrl | Base URL for AEAT invoice verification, prepended to QR code payloads. |
Methods
config($data): void
Sets the global configuration for the service. Call this once during application bootstrap before performing any operations.
Absolute path to the digital certificate file (
.pfx, .p12, or .pem). Must be a certificate recognised by the AEAT — typically an electronic seal or qualified certificate for the issuing entity.Password for the certificate. Pass an empty string
'' for password-free PEM files.AEAT QR verification base URL. This URL is embedded in every QR code generated by
generateInvoiceQr().Path or URL to the WSDL service definition. Defaults to the bundled
SistemaFacturacion.wsdl included with the library. Override to use a different AEAT environment WSDL.Overrides the SOAP service location URL extracted from the WSDL. Useful for targeting the AEAT pre-production (PRE) environment without providing a different WSDL.
Calling
config() resets the internal SoapClient instance. Any subsequent operation will create a new client with the updated settings.getConfig($param): mixed
Retrieves a single configuration value by key. Throws \InvalidArgumentException if the key has not been set.
registerInvoice(InvoiceSubmission $invoice): InvoiceResponse
Registers a new invoice record with AEAT. This is the primary submission operation for VERI*FACTU.
Internal execution steps:
- Validate the
InvoiceSubmissionmodel (all required fields, format checks). Throws\InvalidArgumentExceptionon failure. - Generate hash — calls
HashGeneratorService::generate()to compute the SHA-256 huella and sets$invoice->hash. - Final validation — re-validates the complete model including the newly set hash.
- Serialise — calls
InvoiceSerializer::toInvoiceXml()to produce aRegistroAltaDOM document. - Sign — calls
XmlSignerService::signXml()to apply an XAdES Enveloped digital signature using the configured certificate. The signature is embedded inside theRegistroAltaelement. - Wrap — calls
InvoiceSerializer::wrapXmlWithRegFactuStructure()to embed the signedRegistroAltainside theRegFactuSistemaFacturacionSOAP envelope structure (Cabecera+RegistroFactura). - SOAP call — invokes the
RegFactuSistemaFacturacionSOAP operation via the configuredSoapClient. - Parse response — calls
ResponseParserService::parseInvoiceResponse()on the raw SOAP response XML and returns anInvoiceResponse.
\InvalidArgumentException— model validation failed.\RuntimeException— SOAP communication error or signing failure.\SoapFault— AEAT service returned a SOAP fault.
cancelInvoice(InvoiceCancellation $cancellation): InvoiceResponse
Cancels a previously registered invoice record with AEAT.
Internal execution steps:
- Validate the
InvoiceCancellationmodel. Throws\InvalidArgumentExceptionon failure. - Generate hash — calls
HashGeneratorService::generate()for the cancellation record. - Final validation including hash.
- Serialise — calls
InvoiceSerializer::toCancellationXml()to produce aRegistroAnulacionDOM document. - Wrap — calls
InvoiceSerializer::wrapXmlWithRegFactuStructure()to embed the record inside the SOAP structure. - Sign — calls
XmlSignerService::signXml()on the fully wrapped XML. The signature wraps the entireRegFactuSistemaFacturacionelement for cancellations. - SOAP call — invokes the
RegFactuSistemaFacturacionSOAP operation. - Parse response — returns an
InvoiceResponse.
queryInvoices(InvoiceQuery $query): QueryResponse
Queries invoices previously submitted to AEAT for a given tax period.
- Validate the
InvoiceQuerymodel. - Serialise to XML via
InvoiceSerializer::toQueryXml(). - SOAP call — invokes
ConsultaFactuSistemaFacturacion. - Parse via
ResponseParserService::parseQueryResponse()and return aQueryResponse.
generateInvoiceQr(InvoiceRecord $record, $destination = QrGeneratorService::DESTINATION_STRING, $size = 300, $engine = QrGeneratorService::RENDERER_GD): string
Generates an AEAT-compliant QR code for a registered invoice record. The QR encodes the verification URL with the invoice’s NIF, series number, issue date, and hash.
An
InvoiceSubmission or InvoiceCancellation whose hash field has already been set (either by registerInvoice() or manually via HashGeneratorService::generate()).Output destination. Use
QrGeneratorService::DESTINATION_STRING to return raw image data, or QrGeneratorService::DESTINATION_FILE to save to a temp file and return its path.Pixel resolution of the QR code image (width × height for PNG; viewBox size for SVG).
Renderer backend. One of
RENDERER_GD (PNG via GD), RENDERER_IMAGICK (PNG via ImageMagick), or RENDERER_SVG.qrValidationUrl configuration key.
Error handling
| Exception | Thrown by | Cause |
|---|---|---|
\InvalidArgumentException | config(), getConfig(), registerInvoice(), cancelInvoice(), queryInvoices() | Missing or invalid configuration key; model validation failure. |
\RuntimeException | registerInvoice(), cancelInvoice() | SOAP fault from AEAT; certificate loading failure; XML signing error. |
\SoapFault | registerInvoice(), queryInvoices() | Raw SOAP-level fault from the AEAT endpoint. registerInvoice() wraps this in a \RuntimeException; queryInvoices() rethrows it. |
When AEAT rejects individual invoice lines the response
submissionStatus will be "AceptadoConErrores" or "Incorrecto" and the details are in $response->lineResponses[n]['CodigoErrorRegistro'] / ['DescripcionErrorRegistro']. These are not PHP exceptions — check the InvoiceResponse object after every call.