Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / C#

VIES - VAT number checker

Rate me:
Please Sign up or sign in to vote.
4.82/5 (20 votes)
28 Jul 2010CPOL4 min read 138.7K   4K   26   18
Using the VIES VAT number validation service provided by the European Commission's Taxation and Customs Union Directorate-General.

Introduction

When developing applications in the fields of e-commerce and electronic payment which should be used by customers from inside the European Union, you will come across the problem of VAT number validation of commercial customers. This article describes how to check the VAT using the official Web Service.

Background

The validation of VAT numbers is necessary because the EU taxation law provides a regulation called Reverse Charge System where the VAT tax rate may be devolved to the customer if he is located within another EU country and can provide a valid VAT number meaning that he is really a commercial customer. Additional information can be found here.

The European Commission's Taxation and Customs Union Directorate-General provides a Web Service therefore, which is available at: http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl.

With Visual Studio and the .NET SDK, the usage of this Web Service is straightforward.

Create the Web Service proxy class

First, open up the Visual Studio command prompt. You can find it in your Start menu under the Microsoft Visual Studio 2010/Visual Studio Tools entry. Browse to the directory where you want to create the proxy class at, and type:

wsdl http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl

into the command prompt.

You should see the following screen now:

wsdlCreateShot.jpg

This will create a file called checkVatServices.cs implementing the Web Service proxy with the use of the .wsdl file the European Union has published. Now we are basically done with the creation of the Web Service proxy class. Rapid development, right?

To use this class, simply import it into your project and add a reference to the System.Web.Services.dll library which is part of the .NET SDK. Now you are able to create an instance of the checkVatService class and call its checkVat(...) method.

The created file implements a class object with the methods defined within the .wsdl XML definition file. Furthermore, you will find methods for implementing the Web Service asynchronously inside your application. For further information about asynchronous usage of Web Services, you can read this interesting CodeProject article.

When using the service, please also be aware of the fact that Austrian VAT numbers must start with a heading "U" character (Q19 of the VIES FAQ).

For easy usage of this Web Service, I implemented a wrapper class as well as a demo project to show the usage by example.

The wrapper class

Within the zip file attached to this article, you can find a small wrapper class called ViesVatCheck which adds a heading character for Austrian VATs when its CheckVat() method is called.

The ViesVatCheck class implements two input and four output attributes, which are shown in the table below:

AttributeDescriptionDirection
CountryCodeThe country code of the VAT numberInput
VATNumberThe VAT number to checkInput
NameThe name of the company the VAT number belongs toOutput
AddressThe address of the companyOutput
IsValidtrue if the VAT could be validated, otherwise falseOutput
RetDateThe request date the Web Service returnsOutput

The first input attribute is the country code of the VAT number to check. This attribute can be one of the country codes below:

CountryCountry code
AustriaAT
BelgiumBE
BulgariaBG
CyprusCY
Czech Republic CZ
GermanyDE
DenmarkDK
EstoniaEE
GreeceEL
SpainES
FinlandFI
FranceFR
United KingdomGB
HungaryHU
IrelandIE
ItalyIT
LithuaniaLT
Luxembourg LU
MaltaMT
The NetherlandsNL
PolandPL
PortugalPT
RomaniaRO
SwedenSE
SloveniaSI
SlovakiaSK

If you do take a closer look at the FAQ of the VAT service published by the European Union, you can see that some of these countries are not always available for checking VAT numbers like described in Q11 of the FAQ. So this service can be used to pre- check VAT numbers, but a manual checking may be still needed for invalid VAT numbers.

The second attribute of the wrapper class is the VAT number itself. You do not need to add a heading U character for Austrian VAT as this is done for you by the wrapper class.

The output attributes of the class are available after the CheckVat member method is called.

Using the wrapper class

To use the wrapper class, simply download the zip and build the VATChecker project. This will create an assembly you can use within your project. Furthermore, the demo project contains a small WPF project which uses the ViesVatChecker class.

You can see the screen of the demo application in the following figure:

VatCheckerShot.jpg

Once you have added the assembly file, the wrapper class may be used within the demo application where you can find this code behind the event handler for the Validate button:

C#
private void button1_Click(object sender, RoutedEventArgs e)
{
    if (comboBoxCountries.SelectedItem == null)
    {
        comboBoxCountries.Focus();
        return;
    }

    if (string.IsNullOrEmpty(textBoxVatNumber.Text))
    {
        textBoxVatNumber.Focus();
        return;
    }

    ViesVatCheck check = new ViesVatCheck();
    check.VATNumber = textBoxVatNumber.Text;
    check.CountryCode = ((ComboBoxItem)comboBoxCountries.SelectedItem).Tag.ToString();
            
    bool bValid = check.CheckVat();

    if (bValid)
    {
        imageValid.Visibility = System.Windows.Visibility.Visible;
        imageInvalid.Visibility = System.Windows.Visibility.Hidden;

        labelAddress.Visibility = System.Windows.Visibility.Visible;
        labelCompanyName.Visibility = System.Windows.Visibility.Visible;
        labelRequestDate.Visibility = System.Windows.Visibility.Visible;
                

        labelAddress.Content = check.Address;
        labelCompanyName.Content = check.Name;
        labelRequestDate.Content = check.RetDate.ToShortDateString();
        textBoxVatNumber.Text = check.VATNumber;
    }
    else
    {
        imageValid.Visibility = System.Windows.Visibility.Hidden;
        imageInvalid.Visibility = System.Windows.Visibility.Visible;

        labelAddress.Visibility = System.Windows.Visibility.Hidden;
        labelCompanyName.Visibility = System.Windows.Visibility.Hidden;
        labelRequestDate.Visibility = System.Windows.Visibility.Hidden;
    }
}

The project was created and built with Visual Studio 2010 and .NET 4. If you are using a previous version of the .NET Framework, follow the steps described above to create your own VIES Web Service proxy class.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO MasterSoft Software Solutions Ltd.
Austria Austria

Chris is one of the founders of MasterSoft Software Solutions Ltd., a software development company located in Austria.


He has worked as a C++ developer in the fields of 3D computer simulations and studied Business Informatics before the company opening.


Chris is using C++/C# intensely but also has experience in Java or PHP.


To get additional information about him or his company feel free to visit the webpage of his company or visit his profiles in the social networks like G+, Twitter or Xing for all german networkers.

You are also welcome to visit the webpage for all hosting services his company offers at MasterServer


Comments and Discussions

 
QuestionVIES - VAT number checker Pin
Member 1336363116-Aug-17 1:50
Member 1336363116-Aug-17 1:50 
AnswerRe: VIES - VAT number checker Pin
Christian Kleinheinz26-Aug-17 6:07
Christian Kleinheinz26-Aug-17 6:07 
GeneralMy vote of 1 Pin
Member 1028529316-Dec-14 11:51
professionalMember 1028529316-Dec-14 11:51 
GeneralRe: My vote of 1 Pin
Christian Kleinheinz16-Dec-14 23:32
Christian Kleinheinz16-Dec-14 23:32 
QuestionError 407 through TMG Forefront Pin
RoBoNeville16-Dec-14 5:52
professionalRoBoNeville16-Dec-14 5:52 
AnswerRe: Error 407 through TMG Forefront Pin
Christian Kleinheinz16-Dec-14 23:25
Christian Kleinheinz16-Dec-14 23:25 
QuestionThe request failed with HTTP status 403: AuthorizedOnly. Pin
mani1026m1-Oct-13 0:26
mani1026m1-Oct-13 0:26 
Hi,

When I try to consume this web service am getting "The request failed with HTTP status 403: AuthorizedOnly." error.

am passing parameter as "vat.checkVat(ref code, ref vet, out bValid, out strName, out strAddress)"

Please tell me how to resolve this error.

\Mani.
QuestionGot error when i am going to validate VAT Number Pin
Anand Bakhal8-Apr-13 2:55
professionalAnand Bakhal8-Apr-13 2:55 
AnswerRe: Got error when i am going to validate VAT Number Pin
Christian Kleinheinz10-Apr-13 5:43
Christian Kleinheinz10-Apr-13 5:43 
GeneralRe: Got error when i am going to validate VAT Number Pin
Anand Bakhal11-Apr-13 18:43
professionalAnand Bakhal11-Apr-13 18:43 
GeneralMy vote of 5 Pin
hoernchenmeister13-Jan-12 1:11
hoernchenmeister13-Jan-12 1:11 
GeneralNot working for me unless Expect100Continue is set false. Pin
sgmoore11-Apr-11 23:09
sgmoore11-Apr-11 23:09 
GeneralBusiness Application Pin
ebrunolopes19-Sep-10 21:39
ebrunolopes19-Sep-10 21:39 
GeneralRe: Business Application Pin
Christian Kleinheinz25-Sep-10 5:19
Christian Kleinheinz25-Sep-10 5:19 
GeneralSource code Pin
deBaires28-Jul-10 4:23
deBaires28-Jul-10 4:23 
GeneralRe: Source code Pin
Christian Kleinheinz28-Jul-10 8:24
Christian Kleinheinz28-Jul-10 8:24 
GeneralAdvice for better design Pin
Gil Fink27-Jul-10 19:41
Gil Fink27-Jul-10 19:41 
GeneralRe: Advice for better design Pin
Christian Kleinheinz28-Jul-10 8:18
Christian Kleinheinz28-Jul-10 8:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.