PHP code example of invocash / verifactu-php

1. Go to this page and download the library: Download invocash/verifactu-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

invocash / verifactu-php example snippets




use VerifactuPHP\VerifactuClient;

$verifactu = VerifactuClient::usuario($email, $password);



use VerifactuPHP\VerifactuClient;
use VerifactuPHP\TbaiClient;

// Autenticación como usuario (email + contraseña): acceso completo, incluida la gestión de emisores y webhooks.
$verifactu = VerifactuClient::usuario($email, $password);

// Autenticación como emisor (username + API key): limitado al NIF del emisor.
$verifactu = VerifactuClient::emisor($username, $apiKey);

// El cliente TBAI se inicializa igual.
$tbai = TbaiClient::usuario($email, $password);
$tbai = TbaiClient::emisor($username, $apiKey);

use VerifactuPHP\Exceptions\ApiRequestRefusedException;
use VerifactuPHP\Exceptions\ApiException;
use VerifactuPHP\Exceptions\TransportException;
use VerifactuPHP\Exceptions\ClientException;

try {
    $respuesta = $verifactu->altaRegistroFacturacion($factura);
} catch (ApiRequestRefusedException $e) {
    // Rechazo de negocio. Métodos: getData(), getStatusCode(), getReason(), getMessage()
    $detalle = $e->getData();       // body de error de la API
    $http    = $e->getStatusCode(); // código HTTP
    $codigo  = $e->getReason();     // A-4
} catch (ApiException $e) {
    // Error de servidor, auth o respuesta ilegible. Métodos: getStatusCode(), getReason(), getMessage()
    $http   = $e->getStatusCode(); // código HTTP
    $codigo = $e->getReason();     // A-1, A-2, A-3
} catch (TransportException $e) {
    // Fallo de red. Métodos: getReason(), getMessage()
    $codigo = $e->getReason();     // T-0, T-1, T-2, T-3
} catch (ClientException $e) {
    // Error de uso del cliente. Métodos: getReason(), getMessage()
    $codigo = $e->getReason();     // C-1, C-2
}

$respuesta = $cliente->metodo($args);

public function request(
    string $method,        // GET, POST, PUT, PATCH, DELETE
    string $path,          // ruta relativa a la base de la API, p.ej. '/mi-endpoint?x=1'
    ?array $body = null,   // body que se enviará como JSON
    bool $conAuth = true,  // si la petición requiere token (por defecto, sí)
): array

$verifactu->request('POST', '/endpoint-nuevo', ['campo' => 'valor']);
$verifactu->request('GET', '/algo?x=1');

composer