Dynamics GP real time EU tax registration number validation using VIES

Member states of the European Union expose thier VAT (Tax Registration Number) validation to the masses via VIES. Below is an example using Amazon’s VAT registration details against the service on the EU website.
image

image

 

Here the member state to which the organisation belongs has been selected, then the VAT number has been entered allowing for validity check. In the next screen we see the response to clicking the verifty button.

image

– open data goodness! - now for the cool bit…

Webservice

If you hunt under the FAQ, buried in there, it turns out there is a SOAP webservice available that does the same thing!

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

This WSDL URL is our key to some great functionality for Dynamics GP.

In Visual Studio For Dynamics GP Add-in project create a WCF reference to the VIES SOAP webservice.
In your proxy class you will find checkVatPortTypeClient.CheckVat as a method. This is the one we are after, supply the parameters and by reference the results will be returned

. image

We then create a visual studio add in event handler to fire on the user leaving the Tax Registration field of relevant forms in GP. Hence we can validate the VAT number the user has entered! So in our Add in project in the event handler for the options window Tax Registration Field we handle the event.

Public Shared Sub TaxRegNumberValidateBeforeOriginal(Sender As Object, e As System.ComponentModel.CancelEventArgs)
Try
Dim CustomerMaintOptionsForm As MicrosoftDynamicsGpModifiedDictionary.RmCustomerMaintenanceForm.RmCustomerOptionsWindow = _
MicrosoftDynamicsGpModified.Forms.RmCustomerMaintenance.RmCustomerOptions
If Not CustomerMaintOptionsForm.TaxRegistrationNumber.IsEmpty Then
If Windows.Forms.MessageBox.Show("Remember to update address intrastats field too." & vbCrLf & "Do you want to validate this number?", "Validate?", _
Windows.Forms.MessageBoxButtons.YesNo, Windows.Forms.MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Using oVATNumberValidationForm As New VATNumberValidation
oVATNumberValidationForm.VatNumber = CustomerMaintOptionsForm.TaxRegistrationNumber.Value
If oVATNumberValidationForm.ShowDialog() = vbOK Then
e.Cancel = True
Else
e.Cancel = True
End If
CustomerMaintOptionsForm.TaxRegistrationNumber.Value = oVATNumberValidationForm.VatNumber
End Using
End If
End If
Catch ex As Exception
Windows.Forms.MessageBox.Show("TaxRegNumberValidateBeforeOriginal" & vbCrLf & ex.ToString)
End Try
End Sub

Using the address of the company being looked up, we select the state code from the combo box (could automate this), we then supply to the webservice on hitting the validate button. A windows form shows the results of the validation to the user. If the supplied details fail validation we show the windows form to the user so they can correct the issue and try again. On closing the form we update the Tax Registration Field.

image

Private Sub btnValidate_Click(sender As System.Object, e As System.EventArgs) Handles btnValidate.Click
If txtVATNumber.Text.Length > 0 And cbxMemberState.SelectedIndex <> -1 Then
Dim oStringBuilder As New Text.StringBuilder
Dim o As New VATChecker.checkVatPortTypeClient
Dim ovalid As Boolean
Dim oName As String = String.Empty
Dim oAddress As String = String.Empty
Dim oResult As String = String.Empty
Dim oSelectedCountryCode As String = CStr(cbxMemberState.SelectedItem).Split(CChar(("-")))(0)

oResult = o.checkVat(oSelectedCountryCode, txtVATNumber.Text, ovalid, oName, oAddress)
If ovalid Then
oStringBuilder.AppendLine(oName)
oStringBuilder.AppendLine(oAddress)
oStringBuilder.AppendLine(oResult)
txtResults.Text = oStringBuilder.ToString
Else
txtResults.Text = "INVALID"
End If

Else
Windows.Forms.MessageBox.Show("Enter VAT number and select member state")
End If


End Sub

Conclusion

This is a rough example, the webservice call can be asynchronous, the country code derived from addresses, more error checking and neater presentation of the results, window could be derived from Dex windows for familiar Dex styles, however this post is more about what is possible in twenty mins with Dynamics GP plugins and data opened to the world via webservices.

Optionally, if the details of the your company are passed to the service (state & VAT number), then a consultation number is returned which is proof that you have looked up and validated the VAT number should you be challenged on that fact, but this is optional.

Note:

Different countries have maintenance windows where this service will not work check the VIES website for details

Update 15th Oct 2015

Common Errors

Over the years this has been running, the service has some common occasional errors. To handle them, place the following exception handler around the call to the web service (checkvat).

This makes some human readable error messages and makes some sense and instructs the end user as to what to do, preventing  helpdesk issues from being logged against these transient errors.  This Github project DragonBe VIES has the common errors listed out, I’ve taken my own spin on them below.

Catch ex As System.ServiceModel.FaultException
If ex.Message = "GLOBAL_MAX_CONCURRENT_REQ" Then
Throw New Exception("The European Commission (EC) VAT Information Exchange System (VIES) has too many requests at the same time to cope with, try again later.", ex)
ElseIf ex.Message = "MS_MAX_CONCURRENT_REQ" Then
Throw New Exception("The European Commission (EC) VAT Information Exchange System (VIES) has too many requests at the same time to cope with, try again later.", ex)
ElseIf ex.Message = "TIMEOUT" Then
Throw New Exception("The European Commission (EC) VAT Information Exchange System (VIES) too long to contact member state database, try again later.", ex)
ElseIf ex.Message = "SERVICE_UNAVAILABLE" Then
Throw New Exception("The European Commission (EC) VAT Information Exchange System (VIES) is currently unavailable, may be undergoing maintenance, try again later.", ex)
ElseIf ex.Message = "SERVER_BUSY" Then
Throw New Exception("The European Commission (EC) VAT Information Exchange System (VIES) is too busy to service your request, try again later.", ex)
ElseIf ex.Message = "MS_UNAVAILABLE" Then
Throw New Exception("The European Commission (EC) VAT Information Exchange System (VIES) is currently unable to contact that member state database, try another member state or try this later.", ex)
Else
Throw
End If

End Try