itaibo / vies-checker

European VIES VAT number validator
https://npmjs.com/vies-checker
MIT License
4 stars 1 forks source link

userError in response should be handled #1

Closed pano9000 closed 1 year ago

pano9000 commented 1 year ago

Hi,

I have ran into some issue today, where I would get inconsistent results back from the isValid function in vies-checker: I was testing it with a German VAT No, that I know is definitely valid but the tests flipped between showing "true" and "false" sometimes, which was a bit odd.

I then output the "internal" only response to the console, and by doing that I can see that the response actually contains an userError

const response = yield (0, request_js_1.default)(country, number);
console.log("vies response", response)
{
  isValid: false,
  requestDate: '2023-02-06T10:38:46.773Z',
  userError: 'MS_MAX_CONCURRENT_REQ',
  name: '---',
  address: '---',
  requestIdentifier: '',
  vatNumber: '123456789', //changed
  viesApproximate: {
    name: '---',
    street: '---',
    postalCode: '---',
    city: '---',
    companyType: '---',
    matchName: 3,
    matchStreet: 3,
    matchPostalCode: 3,
    matchCity: 3,
    matchCompanyType: 3
  }
}

However that userError is not taken into account currently, as far as I can tell, vies-checker only checks the isValid property and returns its value. This can lead to some confusion (as it did in my case), as it states that the VAT is invalid, even if it did not actually do a check.

If I check the file here, I can see there are a few possible error messages mentioned: https://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl

In case of problems, the returned FaultString can take the following specific values: 
    - INVALID_INPUT: The provided CountryCode is invalid or the VAT number is empty; 
    - GLOBAL_MAX_CONCURRENT_REQ: Your Request for VAT validation has not been processed; the maximum number of concurrent requests has been reached. Please re-submit your request later or contact TAXUD-VIESWEB@ec.europa.eu for further information": Your request cannot be processed due to high traffic on the web application. Please try again later; 
    - MS_MAX_CONCURRENT_REQ: Your Request for VAT validation has not been processed; the maximum number of concurrent requests for this Member State has been reached. Please re-submit your request later or contact TAXUD-VIESWEB@ec.europa.eu for further information": Your request cannot be processed due to high traffic towards the Member State you are trying to reach. Please try again later. 
    - SERVICE_UNAVAILABLE: an error was encountered either at the network level or the Web application level, try again later; 
    - MS_UNAVAILABLE: The application at the Member State is not replying or not available. Please refer to the Technical Information page to check the status of the requested Member State, try again later; 
    - TIMEOUT: The application did not receive a reply within the allocated time period, try again later. 

My feeling would be to only have "INVALID_INPUT" return false, while all other exceptions should potentially be handled differently.

What do you think?

itaibo commented 1 year ago

Hi @pano9000! Thank you for opening this issue. It is a really good idea

Maybe we can throw an error for everything when there is a userError. And for preventing INVALID_INPUT I can also add country types

How does it sound?

pano9000 commented 1 year ago

sounds reasonable to me, thanks for the quick response! one heads up: the userError property is filled, even if there was no real error - in those cases, it will say "VALID"

itaibo commented 1 year ago

Let me know what you think: #3

pano9000 commented 1 year ago

thank you for the quick response, I've left a comment there, but other than that, I think it looks good - I did not test it locally yet though, as I don't have any TypeScript environment set up currently.

itaibo commented 1 year ago

Great! It think that even without TypeScript you can benefit from it in your IDE

itaibo commented 1 year ago

Changes merged. Since now it throws errors, I have published a major version, just in case. https://www.npmjs.com/package/vies-checker/v/3.0.1