-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_vat.php
65 lines (52 loc) · 1.92 KB
/
check_vat.php
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
<?php
/*
Module developed for the Open Source Content Management System WebsiteBaker (http://websitebaker.org)
Copyright (C) 2007 - 2017, Christoph Marti
LICENCE TERMS:
This module is free software. You can redistribute it and/or modify it
under the terms of the GNU General Public License - version 2 or later,
as published by the Free Software Foundation: http://www.gnu.org/licenses/gpl.html.
DISCLAIMER:
This module 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. See the
GNU General Public License for more details.
*/
// Check EU vat numbers
function check_vat($vat, $tax_group) {
// No check if soap extension is not laoded
if (!extension_loaded('soap')) {
return true;
}
// No check if vat number string has been left empty
if (empty($vat)) {
return true;
}
// Clean vat number string
$invalid_chars = array(chr(0), chr(9), chr(10), chr(11), chr(13), chr(173));
$vat = str_replace($invalid_chars, '', $vat);
// Split country code and vat number
$country_code = strtoupper(substr($vat, 0, 2));
$vat_no = substr($vat, 2);
// Country code must make part of the EU tax zone
if (strpos($tax_group, $country_code) === false) {
return false;
}
// Number part can not be empty
if (empty($vat_no)) {
return false;
}
// Check vat using SOAP
$result = null;
$wsdl_url = 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl';
try {
$soap = @new SoapClient($wsdl_url, array('exceptions' => 1));
$result = $soap->checkVat(array('countryCode' => $country_code, 'vatNumber' => $vat_no));
} catch(SoapFault $E) {
echo '<div class="mod_bakery_error_f"><p>'.$E->faultstring.'</p></div>';
}
if (isset($result->valid) && !$result->valid) {
return false;
}
return true;
}