DEV Community

W System
W System

Posted on Originally published at wsystem.dev

VIES says MS_UNAVAILABLE again: a retry + fallback pattern that keeps checkout from treating an outage as invalid VAT (PHP)

When VIES returns MS_UNAVAILABLE or MS_MAX_CONCURRENT_REQ, it is easy to treat that as “this VAT number is invalid” and block checkout. That is the wrong reading.

MS_UNAVAILABLE means that country's VAT register did not answer VIES. MS_MAX_CONCURRENT_REQ means that country is at its concurrent-request limit. Neither is a verdict on the VAT number — treat both as unknown and retry.

Do not reject the customer or treat the number as invalid. Mark VAT as unverified / pending, retry in the background with bounded backoff, and re-check in VIES before relying on the result (for example for zero-rating).

PHP sample: exponential backoff with jitter

The sample below is a SOAP helper of about 30 lines. It retries only transient faults (MS_UNAVAILABLE, MS_MAX_CONCURRENT_REQ, SERVICE_UNAVAILABLE, TIMEOUT, GLOBAL_MAX_CONCURRENT_REQ). A last-good-result cache is recommended below but is not part of the helper.

<?php
const VIES_WSDL     = 'https://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl';
const VIES_ENDPOINT = 'https://ec.europa.eu/taxation_customs/vies/services/checkVatService';

// Faults that mean "try again later", not "this VAT number is bad".
const VIES_RETRYABLE = [
    'MS_MAX_CONCURRENT_REQ', 'GLOBAL_MAX_CONCURRENT_REQ',
    'MS_UNAVAILABLE', 'SERVICE_UNAVAILABLE', 'TIMEOUT',
];

function viesClient(): SoapClient
{
    static $client = null;
    return $client ??= new SoapClient(VIES_WSDL, [
        'location'           => VIES_ENDPOINT,
        'exceptions'         => true,
        'connection_timeout' => 10,
        'cache_wsdl'         => WSDL_CACHE_BOTH,
    ]);
}

function checkVatApproxWithRetry(array $params, int $maxAttempts = 6): stdClass
{
    for ($attempt = 1; ; $attempt++) {
        try {
            return viesClient()->checkVatApprox($params);
        } catch (SoapFault $fault) {
            // VIES isn't consistent about case ("Invalid_input" vs "INVALID_INPUT")
            $code = strtoupper(trim($fault->getMessage()));
            if (!in_array($code, VIES_RETRYABLE, true) || $attempt >= $maxAttempts) {
                throw $fault;
            }
            // 2, 4, 8, 16, 32 s (max 60) plus up to 1 s of random jitter
            $delay = min(60, 2 ** $attempt) + random_int(0, 1000) / 1000;
            usleep((int) ($delay * 1_000_000));
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Do not retry INVALID_INPUT (the result will not change). Store a successful check together with its requestDate (and requestIdentifier if you sent requester details) in your own application if you want a last-good result — that storage is not part of the helper above.

Live VIES status

A live per-country board is at https://wsystem.dev/vies-status (27 EU member states + Northern Ireland / XI). Error codes, including MS_UNAVAILABLE, MS_MAX_CONCURRENT_REQ and TIMEOUT, are explained at https://wsystem.dev/vies-error-codes. The longer checkout decision table is the canonical article: https://wsystem.dev/vies-down-fallback.

Optional API (disclosure)

Optional, if you would rather not run the retries yourself: the W System VAT API (disclosure: I work on W System). No key: 20 free calls/day per IP, then pay-per-call. GET /vies/status is free and not counted. When a member state stays down, the API can answer from that country's official source for PL, CZ, FI and FR, or from a dated last VIES result when we have one; otherwise you get member_state_unavailable with valid: null — never valid: false because of an outage. A national-register fallback is not the same as a VIES consultation number.

Disclosure: I work on W System, which offers this VAT API. The retry code in the article works without it.

Contact: hello@wsystem.dev

Top comments (0)