Module: VatCheck::Request

Defined in:
lib/vat_check/requests.rb

Class Method Summary collapse

Class Method Details

.lookup(vat) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/vat_check/requests.rb', line 5

def self.lookup(vat)
  client = Savon.client(wsdl: 'https://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl', follow_redirects: true, log: false, log_level: :debug, pretty_print_xml: false)
  country_code, vat_number = VatCheck::Utility.split(vat)
  begin
    response = client.call(:check_vat, message: {country_code: country_code, vat_number: vat_number}, message_tag: :checkVat)
    response.to_hash[:check_vat_response].reject { |key| key == :@xmlns }
  rescue Savon::SOAPFault => e
    if !!(e.message =~ /MS_UNAVAILABLE/)
      return {error:'Service unavailable'}
    else
      return {error:"Unknown error: #{e.message}"}
    end
  rescue Timeout::Error
    return {error:'Service timed out'}
  end
end