Class: Eurovat

Inherits:
Object
  • Object
show all
Defined in:
lib/eurovat.rb,
lib/eurovat/version.rb

Defined Under Namespace

Classes: InvalidFormatError

Constant Summary collapse

SERVICE_URL =
'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl'.freeze
VAT_FORMAT =
/\A([A-Z]{2})([0-9A-Za-z\+\*\.]{2,12})\Z/.freeze
EU_MEMBER_STATES =

Names must be consistent with the country_select plugin. If you know alternative country spellings please add them here.

[
  'Austria',
  'Belgium',
  'Bulgaria',
  'Cyprus',
  'Czech Republic',
  'Denmark',
  'Estonia',
  'Finland',
  'France',
  'Germany',
  'Greece',
  'Hungary',
  'Ireland',
  'Italy',
  'Latvia',
  'Lithuania',
  'Luxembourg',
  'Malta',
  'Netherlands',
  'Poland',
  'Portugal',
  'Romania',
  'Slovakia',
  'Slovenia',
  'Spain',
  'Sweden',
  'United Kingdom'
].freeze
VERSION_STRING =
'1.0.3'
@@country =
'Netherlands'
@@instance =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEurovat

Returns a new instance of Eurovat.



80
81
82
83
84
85
86
87
88
# File 'lib/eurovat.rb', line 80

def initialize
  @country = @@country
  @mutex   = Mutex.new
  if defined?(SOAP)
    @driver = SOAP::WSDLDriverFactory.new(SERVICE_URL).create_rpc_driver
  else
    @driver = Savon::Client.new(SERVICE_URL)
  end
end

Class Method Details

.check_vat_number(vat_number) ⇒ Object



75
76
77
78
# File 'lib/eurovat.rb', line 75

def self.check_vat_number(vat_number)
  @@instance ||= new
  @@instance.check_vat_number(vat_number)
end

.countryObject



50
51
52
# File 'lib/eurovat.rb', line 50

def self.country
  @@country
end

.country=(val) ⇒ Object



54
55
56
# File 'lib/eurovat.rb', line 54

def self.country=(val)
  @@country = country
end

.must_charge_vat?(customer_country, vat_number) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/eurovat.rb', line 58

def self.must_charge_vat?(customer_country, vat_number)
  # http://www.belastingdienst.nl/reken/diensten_in_en_uit_het_buitenland/
  if customer_country == @@country
    true
  else
    if present?(vat_number)
      false
    else
      EU_MEMBER_STATES.include?(customer_country)
    end
  end
end

.sanitize_vat_number(vat_number) ⇒ Object



71
72
73
# File 'lib/eurovat.rb', line 71

def self.sanitize_vat_number(vat_number)
  vat_number.gsub(/[\s\t\.]/, '').upcase
end

Instance Method Details

#check_vat_number(vat_number) ⇒ Object

Any exception other than InvalidFormatError indicates that the service is down.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/eurovat.rb', line 91

def check_vat_number(vat_number)
  vat_number = Eurovat.sanitize_vat_number(vat_number)
  if vat_number =~ VAT_FORMAT
    country_code = $1
    number = $2
    if @driver.respond_to?(:checkVat)
      @mutex.synchronize do
        begin
          result = @driver.checkVat(
            :countryCode => country_code,
            :vatNumber => number)
          result.valid == "true"
        rescue SOAP::FaultError => e
          if e.message == "INVALID_INPUT"
            raise InvalidFormatError, "#{vat_number.inspect} is not formatted like a valid VAT number"
          else
            raise e
          end
        end
      end
    else
      @mutex.synchronize do
        begin
          result = @driver.request(:checkVat) do
            soap.body = {
              :countryCode => country_code,
              :vatNumber => number
            }
          end
          result[:check_vat_response][:valid]
        rescue Savon::SOAP::Fault => e
          if e.message =~ /INVALID_INPUT/
            raise InvalidFormatError, "#{vat_number.inspect} is not formatted like a valid VAT number"
          else
            raise e
          end
        end
      end
    end
  else
    raise InvalidFormatError, "#{vat_number.inspect} is not formatted like a valid VAT number"
  end
end