dmt-software/eu-vat-validation

VIES VAT Validation client

v3.0.3 2023-10-24 14:02 UTC

README

Build Status Scrutinizer Code Quality Code Coverage

This package contains a client to consume the EU VIES Vat soap-service.

Please keep in mind that there is a disclaimer for using the VAT service. This also applies to using this package.

Install

composer require dmt-software/eu-vat-validation

Usage

<?php

use DMT\CommandBus\Validator\ValidationException;
use DMT\Soap\Serializer\SoapFaultException;
use DMT\VatServiceEu\ClientBuilder;
use DMT\VatServiceEu\Request\CheckVat;
use DMT\VatServiceEu\Response\CheckVatResponse;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;

try {
    $request = new CheckVat();
    $request->setCountryCode('NL');
    $request->setVatNumber('804888644B01');

    /** @var ClientInterface $client */
    /** @var RequestFactoryInterface $requestFactory */
    $client = ClientBuilder::create($client, $requestFactory)->build();
    
    /** @var CheckVatResponse $response */
    $response = $client->execute($request);
    
    if ($response->isValid()) {
        // some business logic ...
    }
} catch (ValidationException $exception) {
    // input was incorrect
    foreach ($exception->getViolations() as $violation) {
        print $violation->getMessage();
    }
} catch (SoapFaultException $exception) {
    // service returned an error
    print $exception->getMessage();
}

Further reading