How to check EU VAT using VIES SOAP service in C# How to check EU VAT using VIES SOAP service in C# asp.net asp.net

How to check EU VAT using VIES SOAP service in C#


The simplest way I found is just to send an XML and parse it when it comes back:

var wc = new WebClient();var request = @"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:urn=""urn:ec.europa.eu:taxud:vies:services:checkVat:types"">    <soapenv:Header/>    <soapenv:Body>      <urn:checkVat>         <urn:countryCode>COUNTRY</urn:countryCode>         <urn:vatNumber>VATNUMBER</urn:vatNumber>      </urn:checkVat>    </soapenv:Body>    </soapenv:Envelope>";request = request.Replace("COUNTRY", countryCode);request = request.Replace("VATNUMBER", theRest);String response;try{    response = wc.UploadString("http://ec.europa.eu/taxation_customs/vies/services/checkVatService", request);}catch{    // service throws WebException e.g. when non-EU VAT is supplied}var isValid = response.Contains("<valid>true</valid>");


Here is a self-sufficient (no WCF, no WSDL, ...) utility class that will check the VAT number and get information on the company (name and address). It will return null if the VAT number is invalid or if any error occurred.

// sample calling codeConsole.WriteLine(EuropeanVatInformation.Get("FR89831948815"));...public class EuropeanVatInformation{    private EuropeanVatInformation() { }    public string CountryCode { get; private set; }    public string VatNumber { get; private set; }    public string Address { get; private set; }    public string Name { get; private set; }    public override string ToString() => CountryCode + " " + VatNumber + ": " + Name + ", " + Address.Replace("\n", ", ");    public static EuropeanVatInformation Get(string countryCodeAndVatNumber)    {        if (countryCodeAndVatNumber == null)            throw new ArgumentNullException(nameof(countryCodeAndVatNumber));        if (countryCodeAndVatNumber.Length < 3)            return null;        return Get(countryCodeAndVatNumber.Substring(0, 2), countryCodeAndVatNumber.Substring(2));    }    public static EuropeanVatInformation Get(string countryCode, string vatNumber)    {        if (countryCode == null)            throw new ArgumentNullException(nameof(countryCode));        if (vatNumber == null)            throw new ArgumentNullException(nameof(vatNumber));        countryCode = countryCode.Trim();        vatNumber = vatNumber.Trim().Replace(" ", string.Empty);        const string url = "http://ec.europa.eu/taxation_customs/vies/services/checkVatService";        const string xml = @"<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'><s:Body><checkVat xmlns='urn:ec.europa.eu:taxud:vies:services:checkVat:types'><countryCode>{0}</countryCode><vatNumber>{1}</vatNumber></checkVat></s:Body></s:Envelope>";        try        {            using (var client = new WebClient())            {                var doc = new XmlDocument();                doc.LoadXml(client.UploadString(url, string.Format(xml, countryCode, vatNumber)));                var response = doc.SelectSingleNode("//*[local-name()='checkVatResponse']") as XmlElement;                if (response == null || response["valid"]?.InnerText != "true")                    return null;                var info = new EuropeanVatInformation();                info.CountryCode = response["countryCode"].InnerText;                info.VatNumber = response["vatNumber"].InnerText;                info.Name = response["name"]?.InnerText;                info.Address = response["address"]?.InnerText;                return info;            }        }        catch        {            return null;        }    }}


On .NET platform it is common to consume a Web service so that we generate a proxy class. This can usually be done using Visual Studio "Add Web Reference" where you just fill in a path to the WSDL. An alternative is to generate source classes using wsdl.exe or svcutil.exe.

Then just consume this class and verifying VAT becomes a one-liner:

DateTime date = new checkVatPortTypeClient().checkVat(ref countryCode, ref vatNumber, out isValid, out name, out address);

Generating proxy provides strongly typed API to consume whole service and we don't need to manually create soap envelope and parse output text. It is much more easier, safer and generic solution than yours.