2017-08-18 Check VAT number on VIES

2 minute read

Edit: 2018.08.19 Added SOAP check as a more reliable way to get status. NoPrint and CheckOnly switches
Edit: 2022.08.19 The SOAP services accepts now only text/xml as Content-Type
Changed also the switches - NoPrint and CheckOnly are not used now. By default only a check is done using the SOAP request. If you want to view the results in browser or auto print it use -ShowInBrowser and -Print switches.
The ShowInBrowser part isn’t working as they changed the webpage - I don’t need it anymore and won’t update the script in this part.

VIES

VIES provides an SOAP API to automate the VAT number check. I use it also to get the response.

In my case however it was needed to automate the check, open the web site and print it.

What I did was:

  • query results using the Invoke-WebRequest
  • store the results in a temp file
  • replace the href and src references to http://ec.europa.eu to show the web page later on
  • perform another request using SOAP service to get response status
  • open page in browser
  • create an object with results
  • print on default printer

The Invoke-WebRequest uses a POST method to http://ec.europa.eu/taxation_customs/vies/vatResponse.html

$POST = "memberStateCode=$country&number=$vatnumber&action=check"

Results are stored in a temporary file

Invoke-WebRequest -Method Post -Body $POST -Uri 'http://ec.europa.eu/taxation_customs/vies/vatResponse.html' -OutFile $tempFile

Replace src and href code to display page correctly with pictures and links

$file = $file.replace('href="','href="http://ec.europa.eu') 
$file = $file.replace('src="','src="http://ec.europa.eu')

Perform a SOAP service check using Invoke-WebRequest

$xmlSoap = '<?xml version="1.0" encoding="UTF-8"?>
  <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>{0}</urn:countryCode>
          <urn:vatNumber>{1}</urn:vatNumber>
      </urn:checkVat>
  </soapenv:Body>' -f $country, $vatnumber
#$SoapResults = Invoke-WebRequest -Method Post -Uri $uriSoap -Body $xmlSoap
$SoapResults = Invoke-WebRequest -Method Post -Uri $uriSoap -Body $xmlSoap -ContentType text/xml  #FIX Content-Type 19.08.2022

Create new IE com object and navigate to page

$ie = New-Object -com InternetExplorer.Application 
$ie.AddressBar = $false
$ie.MenuBar = $false
$ie.ToolBar = $false
$ie.visible=$true
$ie.navigate("$env:temp\vat.html")

Create output object with results

$obj = New-Object Pscustomobject -Property @{
			Date = Get-Date
			TIN = $TIN
			Result = $null
			User = $env:Username
}

Check if VAT number was valid or not

#check if $SoapResults 
if (($SoapResults -as [XML]).envelope.body.checkvatresponse.valid){
  if ($file -match ("Yes, valid VAT number")) { 
    $obj.Result = $true			
  }
  elseif ($file -match ("No, invalid VAT number")) { 
    $obj.Result = $false			
  }
  else{
    throw "Not expected results." 
  }
}
else{
  $obj.Result = ($SoapResults -as [XML]).envelope.body.checkvatresponse.valid
}

Finally automatic print

if (-not $NoPrint -and -not $CheckOnly){
  try {
    $ie.execWB(6,2)
  }
  catch{
    $Error[0]
  }
}

Source code

Source code on GitHub

Example usage

Check TIN, show web page and print results.

PS >  Check-VAT_VIES -TIN DE99999999999 -CheckersTIN DE99999999999 -ShowInBrowser -Print

Date                TIN           User     Result
----                ---           ----     ------
2017-08-18 16:47:04 DE99999999999 user1    True

Check TIN, show web page but don’t print.

PS >  Check-VAT_VIES -TIN DE99999999999 -ShowInBrowser

Date                TIN           User     Result
----                ---           ----     ------
2017-08-18 16:47:04 DE99999999999 user1    True

Check TIN only.

PS >  Check-VAT_VIES -TIN DE99999999999 

Date                TIN           User     Result
----                ---           ----     ------
2017-08-18 16:47:04 DE99999999999 user1    True

Leave a Comment