yolk / valvat

Validates european vat numbers. Standalone or as a ActiveModel validator.
MIT License
318 stars 80 forks source link

Handeling different response data #133

Open whysthatso opened 5 months ago

whysthatso commented 5 months ago

First of, thank you for the work put into this.

I usually verify only German and Estonian VAT-IDs, but now i came across a Spanish one and my integration blew up.

Turns out the "Spanish response" includes a bunch of abstract test result fields:

(VAT ID anonymized)

pry> Valvat.new("ESZ1234567R").exists?(requester:  "IE6388047V")  
=> {
:country_code=>"ES", 
:vat_number=>"Z1234567R", 
:request_date=>Tue, 09 Apr 2024, 
:valid=>true, 
:name=>nil, 
:company_type=>nil, 
:name_match=>"3", 
:company_type_match=>"3", 
:street_match=>"3",
:postcode_match=>"3",
:city_match=>"3", 
:request_identifier=>"3bf3997c-9113-45ca-a0d8-f46211377caa"
}

pry> Valvat.new("EE123456789").exists?(requester:  "IE6388047V")
=> {:country_code=>"EE", 
:vat_number=>"123456789", 
:request_date=>Tue, 09 Apr 2024, 
:valid=>true, 
:name=>"Whyservices OÜ", 
:company_type=>nil, 
:address=>"Tänav tn 1 4 10142 Kesklinna linnaosa Tallinn", 
:request_identifier=>"WAPIAAAAY7BwlWoW"}

I'm using this in rails and what i currently do is

  1. take the hash and mutate some keys into different ones that go along my Vat Model's attribute names
  2. assign_attributes that hash to a Vat Model

This worked until it suddenly had these unexpected keys in the "Spanish response".

I understand that the Gem just pushes all the response field pairs into a hash and that's it. Also, there is no way to add country specific validation data to the request (i.e. company name, address, etc), right?

So my questions are:

  1. Is there a set of response key/value pairs that are reliable for every country?
  2. What does one usually do with the rest of the hash?

I could imagine i strip out the expectable key/values and then shove the rest into a jsonb column, never to be seen again? Maybe do something later with it or show it in a view for manual verification, etc. The Estonians send down everything, Germans nothing, and Spaniards comparison values. Not sure what other countries do.

whysthatso commented 5 months ago

and additional a kind of legal question: if one verifies the VAT ID and has the option to verify the address/name/etc, is one obliged to do that?