PHP code example of iberfacil / es-aeat-vnif

1. Go to this page and download the library: Download iberfacil/es-aeat-vnif 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/ */

    

iberfacil / es-aeat-vnif example snippets


use Iberfacil\AeatVnif\VnifClient;
use Iberfacil\AeatVnif\Data\Taxpayer;
use Iberfacil\AeatVnif\Data\TaxpayerName;

$client = VnifClient::create('/etc/aeat/certificado.p12', getenv('AEAT_VNIF_CERT_PASSWORD'));

// Una persona física: nombre y apellidos por separado
$result = $client->check('00000000T', TaxpayerName::naturalPerson('Zefira', 'Lumina', 'Peralvillo'));

$result->result;              // IdentificationResult::Identified
$result->aeatResult;          // 'IDENTIFICADO' (literal de la AEAT)
$result->isIdentified();      // true
$result->sentName;            // 'LUMINA PERALVILLO ZEFIRA'
$result->censusName;          // 'LUMINÁ PERALVILLO ZÉFIRA' (como figura en el censo)
$result->nameWasCorrected();  // false: las letras coinciden, solo cambian las tildes

// Una entidad
$result = $client->check('B00000000', TaxpayerName::entity('Brumalia Ficticia, S.L.'));

// Un lote: los resultados llegan en el mismo orden que la entrada
$results = $client->checkBatch([
    Taxpayer::naturalPerson('00000000T', 'Zefira', 'Lumina', 'Peralvillo'),
    Taxpayer::entity('B00000000', 'Brumalia Ficticia SL'),
]);

foreach ($results as $r) {
    if ($r->failure !== null) {
        // NO PROCESADO: reintentar este contribuyente más tarde
    }
}

use Iberfacil\AeatVnif\VnifOptions;

$options = new VnifOptions(
    endpoint: VnifOptions::DEFAULT_ENDPOINT,
    batchSize: 500,       // contribuyentes por petición (1..10000)
    timeoutSeconds: 30,
    retries: 2,           // reintentos ante fallo transitorio de red
    retryDelayMs: 500,    // espera inicial, se duplica en cada intento
);

$client = VnifClient::create($ruta, $password, $options);

use Iberfacil\AeatVnif\Laravel\Facades\AeatVnif;
use Iberfacil\AeatVnif\Data\TaxpayerName;

$result = AeatVnif::check('00000000T', TaxpayerName::naturalPerson('Zefira', 'Lumina', 'Peralvillo'));

use Iberfacil\AeatVnif\Observers\CallbackObserver;
use Iberfacil\AeatVnif\Data\VnifRequest;
use Iberfacil\AeatVnif\Data\VnifResponse;

$client = $client->withObserver(
    CallbackObserver::make()
        ->before(fn (VnifRequest $r) => $log->info('AEAT VNIF', ['hash' => $r->xmlSha256, 'n' => $r->size(), 'intento' => $r->attempt]))
        ->after(fn (VnifRequest $r, VnifResponse $s) => $log->info('AEAT VNIF ok', $s->countsByResult()))
        ->failure(fn (VnifRequest $r, \Throwable $e) => $log->warning('AEAT VNIF fallo', ['tipo' => $e::class]))
);

$client = new VnifClient($certificate, $options, cache: new Psr16ResultCache($miCache, ttlSeconds: 86400));
bash
   export AEAT_VNIF_CERT=/etc/aeat/certificado.p12
   export AEAT_VNIF_CERT_PASSWORD='la-contraseña'
   
bash
php artisan vendor:publish --tag=aeat-vnif-config
bash
php artisan aeat-vnif:doctor
php artisan aeat-vnif:check 00000000T --nombre=Zefira --apellido1=Lumina --apellido2=Peralvillo
php artisan aeat-vnif:check B00000000 --razon-social="Brumalia Ficticia SL" --json