Code Monkey home page Code Monkey logo

vat_check's Introduction

VatCheck RubyGems Build Status

Need to verify VAT identification numbers in Ruby? VatCheck makes it easy and simple:

vat = VatCheck.new('GB333289454')
puts 'Legit' if vat.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.

Getting Started

Install it via RubyGems in your terminal:

gem install vat_check

Or in your Gemfile:

gem 'vat_check', '~> 1.0'

Note: VatCheck requires Ruby 2.3 or greater.

Basic Usage

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 validated
vat.vies # True if VIES response is valid
vat.vies_available # True if VIES available
vat.response # Hash if VIES available

vat.valid? # True if regex and VIES validated (if available)
vat.exists? # True if VIES available and VATIN exists

valid? 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?:

if vat.exists?
  puts vat.response.name
  puts vat.response.address
end

Examples

VIES is available, the VAT is formatted correctly, and the VAT is registered to a business:

vat = VatCheck.new('GB333289454')
vat.regex # true
vat.vies_available # true
vat.vies # true
vat.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? # true
vat.exists? # true

VIES is available, the VAT ID is formatted correctly, but the VAT ID is not registered to a business:

vat = VatCheck.new('GB999999999')
vat.regex # true
vat.vies_available # true
vat.vies # false
vat.response # {:country_code=>"GB", :vat_number=>"999999999", :request_date=>#<Date: 2016-01-13 ((2457401j,0s,0n),+0s,2299161j)>, :valid=>false, :name=>"---", :address=>"---"}
vat.valid? # false
vat.exists? # false

VAT is not formatted correctly:

vat = VatCheck.new('XX123456789')
vat.regex # false
vat.vies # false
vat.vies_available # false
vat.response # {}
vat.valid? # false
vat.exists? # false

VAT is formatted correctly but VIES is unavailable:

vat = VatCheck.new('GB333289454')
vat.regex # true
vat.vies_available # false
vat.vies # false
vat.response # {:error=>"Service unavailable"}
vat.valid? # true
vat.exists? # false

VAT is formatted correctly but VIES times out:

vat = VatCheck.new('GB333289454')
vat.regex # true
vat.vies_available # false
vat.vies # false
vat.response # {:error=>"Service timed out"}
vat.valid? # true
vat.exists? # false

vat_check's People

Contributors

aleccarper avatar bittersweet avatar fastdivision avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vat_check's Issues

Account for VIES URL and subsequent call changing to https

Due to VIES updating to https, current calls to establish the client respond with:

<HTML><HEAD>
<TITLE>Redirect</TITLE>
</HEAD>
<BODY>
<FONT face="Helvetica">
<big><strong></strong></big><BR>
</FONT>
<blockquote>
<TABLE border=0 cellPadding=1 width="80%">
<TR><TD>
<FONT face="Helvetica">
<big>Redirect (policy_request_redirect)</big>
<BR>
<BR>
</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica">
Click <a href="https:&#x2F;&#x2F;ec.europa.eu&#x2F;taxation_customs&#x2F;vies&#x2F;checkVatService.wsdl">here</a> if you are not automatically redirected.
</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica">

</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica" SIZE=2>
<BR>
For assistance, contact your network support team.
</FONT>
</TD></TR>
</TABLE>
</blockquote>
</FONT>
</BODY></HTML>

I was able to spend a small amount of time looking at this, and it seemed like adding follow_redirects: true to

client = Savon.client(wsdl: 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl', log: false, log_level: :debug, pretty_print_xml: false)
got things working as expected through vat_check.rb, but I was unable to get the specs back to a passing state with this change.

rescue Savon::HTTPError also

Hi,

we use this gem in production and sometimes it failes, cause the VIES service answers with some garbage leading to raise a Savon::HTTPError which is not handled by this gem.

I would suggest to add this error next to this line:

rescue Savon::SOAPFault => e

What do you think?

Here is an example failure response of such a request:

HTTP error (307): <HTML><HEAD>
<TITLE>Network Error</TITLE>
</HEAD>
<BODY>
<FONT face="Helvetica">
<big><strong></strong></big><BR>
</FONT>
<blockquote>
<TABLE border=0 cellPadding=1 width="80%">
<TR><TD>
<FONT face="Helvetica">
<big>Network Error (tcp_error)</big>
<BR>
<BR>
</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica">
A communication error occurred: ""
</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica">
The Web Server may be down, too busy, or experiencing other problems preventing it from responding to requests. You may wish to try again at a later time.
</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica" SIZE=2>
<BR>
For assistance, contact your network support team.
</FONT>
</TD></TR>
</TABLE>
</blockquote>
</FONT>
</BODY></HTML>

GB VAT numbers will no longer registered with VIES after Jan 1st 2021

As a result of Brexit, regrading VIES:

UK businesses will be able to continue to use the EU VAT number validation service to check the validity of EU businesses, but UK VAT registrations will cease to be included.

The UK Gov has an API in beta for checking VAT numbers:

https://developer.service.hmrc.gov.uk/api-documentation/docs/api/service/vat-registered-companies-api/1.0

(API access is controlled by credentials. Users must sign up for GOV UK Developer Hub)

If VIES service is unavailable, but the vat nr matches the regexp, it should not return "valid"

On line 14 of file https://github.com/taxjar/vat_check/blob/master/lib/vat_check.rb#L14, you return true if the VAT number passes the regexp check, but the VIES service is not working for some reason. I don't think this should be the default. At worst, it should be configurable: we need to be sure the VAT number is correct. The reliability of the VIES services is often quite bad, but we need to be sure that a VAT number is a valid VAT number for accounting/reporting reasons.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.