Skip to content

Validate European VAT numbers with EU prefix #417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion stdnum/eu/vat.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"""

from stdnum.exceptions import *
from stdnum.util import clean, get_cc_module, get_soap_client
from stdnum.util import clean, get_cc_module, get_soap_client, isdigits


MEMBER_STATES = set([
Expand All @@ -58,10 +58,35 @@
"""The WSDL URL of the VAT Information Exchange System (VIES)."""


class EuropeanValidator(object):
"""
Foreign companies that trade with private individuals and non-business
organisations in the EU may have a VATIN starting with "EU" instead of a
country code.
"""

@classmethod
def compact(cls, number):
"""Compact European VAT Number"""
return clean(number, ' -').upper().strip()

@classmethod
def validate(cls, number):
"""Validate European VAT Number"""
number = cls.compact(number)
if not isdigits(number[2:]):
raise InvalidFormat()
if len(number) < 2 or len(number) > 13:
raise InvalidLength()
return number


def _get_cc_module(cc):
"""Get the VAT number module based on the country code."""
# Greece uses a "wrong" country code
cc = cc.lower()
if cc == 'eu':
return EuropeanValidator
if cc == 'el':
cc = 'gr'
if cc not in MEMBER_STATES:
Expand Down
7 changes: 7 additions & 0 deletions tests/test_eu_vat.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ These have been found online and should all be valid numbers.
... EL: 094279805
... El 800 179 925
...
... EU826010755
... EU8 26009064
...
... FI 02459042
... FI 0982651-1
... FI 10320534
Expand Down Expand Up @@ -812,6 +815,8 @@ flaws.
... DE 246X595 415
... DE 011125440
...
... EU 1234X1234
...
... IE 4550C59S
... IE 069385V8
...
Expand Down Expand Up @@ -864,6 +869,8 @@ These numbers should be mostly valid except that they have the wrong length.
...
... ES B-583784312
...
... EU 1234567890123
...
... FR000076090
...
... NL006866304B021
Expand Down