/usr/share/perl5/Business/Tax/VAT/Validation.pm is in libbusiness-tax-vat-validation-perl 0.22-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 | package Business::Tax::VAT::Validation;
############################################################################
# IT Development software #
# European VAT number validator Version 0.22 #
# Copyright 2003 Nauwelaerts B bpgn@cpan.org #
# Created 06/08/2003 Last Modified 04/10/2011 #
############################################################################
# COPYRIGHT NOTICE #
# Copyright 2003 Bernard Nauwelaerts All Rights Reserved. #
# #
# THIS SOFTWARE IS RELEASED UNDER THE GNU Public Licence #
# Please see COPYING for details #
# #
# DISCLAIMER #
# As usual with GNU software, this one is provided as is, #
# WITHOUT ANY WARRANTY, without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. #
# #
############################################################################
# Revision history (dd/mm/yyyy) : #
# #
# 0.22 04/10/2011; Typo fix in POD error message #2 #
# (Thanks to Martin H. Sluka) #
# Minor POD fixes (BPGN) #
# Fix subs args bug (performance fix) (BPGN) #
# 0.21 04/08/2009; New error status 19: EU database too busy #
# (Martin H. Sluka) #
# 0.20 18/08/2008; VIES HTML changes: now allowing multiples spaces #
# (Thanks to Simon Williams, Benoît Galy & Raluca Boboia) #
# 0.19 29/04/2008; HU regexp: missing digit "9" added #
# (Thanks to Simon Williams) #
# 0.18 03/01/2008; BE regexp: from transitional 9-digit & 10-digit format #
# to 10-digit new format #
# 0.16 13/07/2007; Allowing spaces in regexps #
# 0.15 06/07/2007; Added missing "keys" during $self->{members} #
# constuction #
# (Thanks to Dave O.) #
# Corrected returned 0-1 error codes inversion #
# Some POD improvements #
# 0.14 05/07/2007; VIES interface changed $baseurl, and POST params were #
# changed to lowercase. Added support for Bulgaria and #
# Romania (thanks to Kaloyan Iliev) #
# New get_last_error_code method #
# Updated regexps according to the actual VIES FAQ #
# Some slight documentation improvements #
# Improved tests: each regexp is tested accordind to FAQ #
# 0.13 16/01/2007; VIES interface changed "not found" layout #
# (Thanks to Tom Kirkpatrick for this update) #
# 0.12 10/11/2006; YAML Compliance #
# 0.11 10/11/2006; Minor bug allowing one forbidden character #
# corrected in Belgian regexp #
# (Thanks to Andy Wardley for this report) #
# + added regular_expressions property #
# for external testing purposes #
# 0.10 20/07/2006; Adding Test::Pod to test suite #
# 0.09 20/06/2006; local_check method allows you to test VAT numbers #
# without asking the EU database. Based on regexps. #
# 0.08 20/06/2006; 9 and 10 digits transitional regexp for Belgium #
# From 31/12/2007, only 10 digits will be valid #
# 0.07 25/05/2006; Now we use "request" method not "simple request" #
# in order to follow potential redirects #
# 0.06 25/05/2006; Changed $baseurl #
# (Thanks to Torsten Mueller for this update) #
# 0.05 19/01/2006; Adding support for proxy settings #
# (Thanks to Tom Kirkpatrick for this update) #
# 0.04 01/11/2004; Adding support for error "Member Service Unavailable" #
# 0.03 01/11/2004; Adding 10 new members. #
# (Thanks to Robert Alloway for this update) #
# 0.02 29/09/2003; Fix alphanumeric VAT numbers rejection #
# (Thanks to Robert Alloway for the regexps) #
# 0.01 06/08/2003; Initial release #
# #
############################################################################
use strict;
BEGIN {
$Business::Tax::VAT::Validation::VERSION = '0.22';
use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
}
=head1 NAME
Business::Tax::VAT::Validation - A class for european VAT numbers validation.
=head1 SYNOPSIS
use Business::Tax::VAT::Validation;
my $hvatn=Business::Tax::VAT::Validation->new();
# Check number
if ($hvatn->check($VAT, [$member_state])){
print "OK\n";
} else {
print $hvatn->get_last_error;
}
=head1 DESCRIPTION
This class provides you a easy api to check validity of european VAT numbers (if the provided number exists).
It asks the EU database (VIES) for this, using its web interface methods.
=head1 CONSTRUCTOR
=over 4
=item B<new> Class constructor.
$hvatn=Business::Tax::VAT::Validation->new();
If your system is located behind a proxy :
$hvatn=Business::Tax::VAT::Validation->new(-proxy => ['http', 'http://example.com:8001/']);
Note : See LWP::UserAgent for proxy options.
=cut
sub new {
my($class, %arg) = @_;
my $self = {
#baseurl => 'http://europa.eu.int/comm/taxation_customs/vies/cgi-bin/viesquer', # Obsolete since v0.06
#baseurl => 'http://ec.europa.eu/taxation_customs/vies/cgi-bin/viesquer', # Obsolete since v0.14
baseurl => 'http://ec.europa.eu/taxation_customs/vies/viesquer.do',
error => '',
error_code => 0,
re => {
### t/01_localcheck.t tests if these regexps accepts all regular VAT numbers, according to VIES FAQ
AT => 'U[0-9]{8}',
BE => '0[0-9]{9}',
BG => '[0-9]{9,10}',
CY => '[0-9]{8}[A-Za-z]',
CZ => '[0-9]{8,10}',
DE => '[0-9]{9}',
DK => '[0-9]{2} ?[0-9]{2} ?[0-9]{2} ?[0-9]{2}',
EE => '[0-9]{9}',
EL => '[0-9]{9}',
ES => '([A-Za-z0-9][0-9]{7}[A-Za-z0-9])',
FI => '[0-9]{8}',
FR => '[A-Za-z0-9]{2} ?[0-9]{9}',
GB => '([0-9]{3} ?[0-9]{4} ?[0-9]{2}|[0-9]{3} ?[0-9]{4} ?[0-9]{2} ?[0-9]{3}|GD[0-9]{3}|HA[0-9]{3})',
HU => '[0-9]{8}',
IE => '[0-9][A-Za-z0-9\+\*][0-9]{5}[A-Za-z]',
IT => '[0-9]{11}',
LT => '([0-9]{9}|[0-9]{12})',
LU => '[0-9]{8}',
LV => '[0-9]{11}',
MT => '[0-9]{8}',
NL => '[0-9]{9}B[0-9]{2}',
PL => '[0-9]{10}',
PT => '[0-9]{9}',
RO => '[0-9]{2,10}',
SE => '[0-9]{12}',
SI => '[0-9]{8}',
SK => '[0-9]{10}',
},
proxy => $arg{-proxy}
};
$self = bless $self, $class;
$self->{members}=join('|', keys %{$self->{re}});
$self;
}
=back
=head1 PROPERTIES
=over 4
=item B<member_states> Returns all member states 2-digit codes as array
@ms=$hvatn->member_states;
=cut
sub member_states {
(keys %{$_[0]->{re}})
}
=item B<regular_expressions> - Returns a hash list containing one regular expression for each country
If you want to test a VAT number format ouside this module, e.g. embedded as javascript in a web form.
%re=$hvatn->regular_expressions;
returns
(
AT => 'U[0-9]{8}',
...
SK => '[0-9]{10}',
);
=cut
sub regular_expressions {
(%{$_[0]->{re}})
}
=back
=head1 METHODS
=cut
=over 4
=item B<check> - Checks if a VAT number exists into the VIES database
$ok=$hvatn->check($VAT, [$member_state]);
You may either provide the VAT number under its complete form (e.g. BE-123456789, BE123456789)
or either specify VAT and MS (member state) individually.
Valid MS values are :
AT, BE, BG, CY, CZ, DE, DK, EE, EL, ES,
FI, FR, GB, HU, IE, IT, LU, LT, LV, MT,
NL, PL, PT, RO, SE, SI, SK
=cut
sub check {
my($self,$vatn,$mscc,@other)=@_; # @other is here for backward compatibility purposes
return $self->_set_error('You must provide a VAT number') unless $vatn;
$mscc||='';
($vatn, $mscc)=$self->_format_vatn($vatn, $mscc);
if ($vatn) {
my $ua = LWP::UserAgent->new;
if (ref $self->{proxy} eq 'ARRAY') {
$ua->proxy(@{$self->{proxy}});
} else {
$ua->env_proxy;
}
$ua->agent('Business::Tax::VAT::Validation/'.$Business::Tax::VAT::Validation::VERSION);
my $req = POST $self->{baseurl},
[
'selectedLanguage' => 'EN',
'ms' => $mscc ,
'vat' => $vatn ,
'iso' => $mscc ,
];
return $mscc.'-'.$vatn if $self->_is_res_ok($ua->request($req)->as_string);
}
0;
}
=item B<local_check> - Checks if a VAT number format is valid
$ok=$hvatn->local_check($VAT, [$member_state]);
This method is based on regexps only and DOES NOT asks the VIES database
=cut
sub local_check {
my($self,$vatn,$mscc,@other)=@_; # @other is here for backward compatibility purposes
return $self->_set_error('You must provide a VAT number') unless $vatn;
$mscc||='';
($vatn, $mscc)=$self->_format_vatn($vatn, $mscc);
if ($vatn) {
return 1
} else {
return 0
}
}
=item B<get_last_error_code> - Returns the last recorded error code
=item B<get_last_error> - Returns the last recorded error
my $err = $hvatn->get_last_error_code();
my $txt = $hvatn->get_last_error();
Possible errors are :
=over 4
=item *
0 Unknown MS code : Internal checkup failed (Specified Member State does not exist)
=item *
1 Invalid VAT number format : Internal checkup failed (bad syntax)
=item *
2 This VAT number doesn't exist in EU database : distant checkup
=item *
3 This VAT number contains errors : distant checkup
=item *
17 Time out connecting to the database : Temporary error when the connection to the database times out
=item *
18 Member Sevice Unavailable: The EU database is unable to reach the requested member's database.
=item *
19 The EU database is too busy.
=item *
257 Invalid response, please contact the author of this module. : This normally only happens if this software doesn't recognize any valid pattern into the response document: this generally means that the database interface has been modified, and you'll make the author happy by submitting the returned response !!!
=back
If error_code > 16, you should temporarily accept the provided number, and periodically perform new checks until response is OK or error < 17
If error_code > 256, you should temporarily accept the provided number, contact the author, and perform a new check when the software is updated.
=cut
sub get_last_error {
$_[0]->{error};
}
sub get_last_error_code {
$_[0]->{error_code};
}
### PRIVATE FUNCTIONS ==========================================================
sub _format_vatn {
my($self,$vatn,$mscc)=@_;
my $null='';
$vatn=~s/\-/ /g; $vatn=~s/\./ /g; $vatn=~s/\s+/ /g;
if (!$mscc && $vatn=~s/^($self->{members}) ?/$null/e) {
$mscc=$1;
}
return $self->_set_error(0, "Unknown MS code") if $mscc!~m/^($self->{members})$/;
my $re=$self->{re}{$mscc};
return $self->_set_error(1, "Invalid VAT number format") if $vatn!~m/^$re$/;
($vatn, $mscc);
}
sub _is_res_ok {
my($self,$res)=@_;
if ($res=~/^(\d{3}) (.*)/) {
return $self->_set_error($1, $2)
}
$res=~s/[\r\n]//; $res=~s/>/>\n/;
foreach (split(/\n/, $res)) {
next unless $_;
if (/^\s*No\, invalid VAT number/) {
return $self->_set_error(2, "This VAT number doesn't exists in EU database.")
} elsif (/^\s*Error\: (.*)$/) {
return $self->_set_error(3, "This VAT number contains errors: ".$1)
} elsif (/Request time-out\. Please re-submit your request later/){
return $self->_set_error(17, "Time out connecting to the database")
} elsif (/^\s*Member State service unavailable/) {
return $self->_set_error(18, "Member State service unavailable: Please re-submit your request later.")
} elsif (/^\s*(System busy: Too many requests)\. (Please re-submit your request later\.)/) {
return $self->_set_error(19, "$1: $2")
}
return 1 if /^\s*Yes\, valid VAT number\s*$/;
}
$self->_set_error(257, "Invalid response, please contact the author of this module. ".$res)
}
sub _set_error {
my ($self, $code, $txt)=@_;
$self->{error_code}=$code;
$self->{error}=$txt;
undef
}
=back
=head1 WHY NOT SOAP ?
Just because this module's author wasn't given such time to do so. Furthermore, the SOAP module available at CPAN at time of writing is farly too complex to be used here, simple tasks having to be simply performed. However, if you already use SOAP in your application, it may be better to query the VIES database by this way. See the VIES documentation for further details on how to use it.
=head1 SEE ALSO
LWP::UserAgent
=head1 FEEDBACK
If you find this module useful, or have any comments, suggestions or improvements, please let me know.
=head1 AUTHOR
Bernard Nauwelaerts <bpgn@cpan.org>
=head1 CREDITS
Many thanks to the following people, actively involved in the development of this software by submitting patches, bug reports, new members regexps, VIES interface changes,... (sorted by last intervention) :
=over 4
=item *
Martin H. Sluka, noris network AG, Germany.
=item *
Simon Williams, UK2 Limited, United Kingdom & Benoît Galy, Greenacres, France & Raluca Boboia, Evozon, Romania
=item *
Dave O., POBox, U.S.A.
=item *
Kaloyan Iliev, Digital Systems, Bulgaria.
=item *
Tom Kirkpatrick, Virus Bulletin, United Kingdom.
=item *
Andy Wardley, individual, United Kingdom.
=item *
Robert Alloway, Service Centre, United Kingdom.
=item *
Torsten Mueller, Archesoft, Germany
=back
=head1 LICENSE
GPL. Enjoy! See COPYING for further information on the GPL.
=head1 DISCLAIMER
See I<http://ec.europa.eu/taxation_customs/vies/viesdisc.do> to known the limitations of the EU validation service.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
=cut
1;
|