Need to verify VAT identification numbers in Ruby? VatCheck makes it easy and simple:
vat=VatCheck.new('GB333289454')puts'Legit'ifvat.valid?That's it. VatCheck first performs a regex validation. If it passes, it attempts to verify on VIES. Sometimes it's up, sometimes it's down. When it's down valid? gracefully falls back to regex.
Install it via RubyGems in your terminal:
gem install vat_check
Or in your Gemfile:
gem 'vat_check'
Note: VatCheck requires Ruby 2.0 or greater.
require'vat_check'vat=VatCheck.new('VATIN')This returns some nifty helper methods and attributes for all your VAT needs:
vat.regex# True if regex validatedvat.vies# True if VIES response is validvat.vies_available# True if VIES availablevat.response# Hash if VIES availablevat.valid?# True if regex and VIES validated (if available)vat.exists?# True if VIES available and VATIN existsvalid? will be your goto method for quick verification. If VIES is currently available, you'll get a response hash:
{:country_code=>"GB",:vat_number=>"333289454",:request_date=>Date.today,:valid=>true,:name=>"BRITISH BROADCASTING CORPORATION",:address=>"FAO ALEX FITZPATRICK\nBBC GROUP VAT MANAGER\nTHE LIGHT HOUSE (1ST FLOOR)\nMEDIA VILLAGE, 201 WOOD LANE\nLONDON\nW127TQ"}If you'd like to expose that info, go ahead and use exists?:
ifvat.exists?putsvat.response.nameputsvat.response.addressendVIES is available, the VAT is formatted correctly, and the VAT is registered to a business:
vat=VatCheck.new('GB333289454')vat.regex# truevat.vies_available# truevat.vies# truevat.response# {:country_code=>"GB", :vat_number=>"333289454", :request_date=>#<Date: 2016-01-13 ((2457401j,0s,0n),+0s,2299161j)>, :valid=>true, :name=>"BRITISH BROADCASTING CORPORATION", :address=>"FAO ALEX FITZPATRICK\nBBC GROUP VAT MANAGER\nTHE LIGHT HOUSE (1ST FLOOR)\nMEDIA VILLAGE, 201 WOOD LANE\nLONDON\nW12 7TQ"}vat.valid?# truevat.exists?# trueVIES is available, the VAT ID is formatted correctly, but the VAT ID is not registered to a business:
vat=VatCheck.new('GB999999999')vat.regex# truevat.vies_available# truevat.vies# falsevat.response# {:country_code=>"GB", :vat_number=>"999999999", :request_date=>#<Date: 2016-01-13 ((2457401j,0s,0n),+0s,2299161j)>, :valid=>false, :name=>"---", :address=>"---"}vat.valid?# falsevat.exists?# falseVAT is not formatted correctly:
vat=VatCheck.new('XX123456789')vat.regex# falsevat.vies# falsevat.vies_available# falsevat.response# {}vat.valid?# falsevat.exists?# falseVAT is formatted correctly but VIES is unavailable:
vat=VatCheck.new('GB333289454')vat.regex# truevat.vies_available# falsevat.vies# falsevat.response# {:error=>"Service unavailable"}vat.valid?# truevat.exists?# falseVAT is formatted correctly but VIES times out:
vat=VatCheck.new('GB333289454')vat.regex# truevat.vies_available# falsevat.vies# falsevat.response# {:error=>"Service timed out"}vat.valid?# truevat.exists?# false