|
1
|
|
|
<?php |
|
2
|
|
|
App::uses('VatNumberCheckAppModel', 'VatNumberCheck.Model'); |
|
3
|
|
|
App::uses('HttpSocket', 'Network/Http'); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* VatNumberCheck Model |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
class VatNumberCheck extends VatNumberCheckAppModel { |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Use table. |
|
13
|
|
|
* |
|
14
|
|
|
* @var mixed False or table name |
|
15
|
|
|
*/ |
|
16
|
|
|
public $useTable = false; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The (translation) domain to be used for extracted validation messages in models. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
public $validationDomain = 'vat_number_check'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Url to check vat numbers. |
|
27
|
|
|
* |
|
28
|
|
|
*/ |
|
29
|
|
|
const CHECK_URL = 'http://ec.europa.eu/taxation_customs/vies/viesquer.do'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Normalizes a VAT number. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $vatNumber A VAT number |
|
35
|
|
|
* @return string A (normalized) VAT number |
|
36
|
|
|
*/ |
|
37
|
|
|
public function normalize($vatNumber) { |
|
38
|
|
|
$vatNumber = strtoupper($vatNumber); |
|
39
|
|
|
$vatNumber = preg_replace('/[^A-Z0-9]/', '', $vatNumber); |
|
40
|
|
|
|
|
41
|
|
|
return $vatNumber; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Splits a VAT number into querystring parameters. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $vatNumber A VAT number |
|
48
|
|
|
* @return array Querystring parameters |
|
49
|
|
|
*/ |
|
50
|
|
|
public function toQueryString($vatNumber) { |
|
51
|
|
|
$memberStateCode = substr($vatNumber, 0, 2); |
|
52
|
|
|
$number = substr($vatNumber, 2); |
|
53
|
|
|
$action = 'check'; |
|
54
|
|
|
$check = 'Verify'; |
|
55
|
|
|
|
|
56
|
|
|
return compact('memberStateCode', 'number', 'action', 'check'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Constructs an url for a given vat number. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $vatNumber A VAT number |
|
63
|
|
|
* @return string An url |
|
64
|
|
|
*/ |
|
65
|
|
|
public function constructUrl($vatNumber) { |
|
66
|
|
|
$queryString = $this->toQueryString($vatNumber); |
|
67
|
|
|
$queryString = http_build_query($queryString); |
|
68
|
|
|
|
|
69
|
|
|
return self::CHECK_URL . '?' . $queryString; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Downloads a given url. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $url An url |
|
76
|
|
|
* @return mixed Request body on success (string) otherwise false (boolean) |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getUrlContent($url) { |
|
79
|
|
|
$config = (array)Configure::read('VatNumberCheck.socketConfig'); |
|
|
|
|
|
|
80
|
|
|
$HttpSocket = new HttpSocket($config); |
|
81
|
|
|
|
|
82
|
|
|
try { |
|
83
|
|
|
$response = $HttpSocket->get($url); |
|
84
|
|
|
|
|
85
|
|
|
if ($response->isOk()) { |
|
86
|
|
|
return $response->body(); |
|
87
|
|
|
} |
|
88
|
|
|
} catch (Exception $e) { |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Checks a given VAT number. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $vatNumber A VAT number |
|
98
|
|
|
* @return bool Valid or not |
|
99
|
|
|
* @throws InternalErrorException |
|
100
|
|
|
*/ |
|
101
|
|
|
public function check($vatNumber) { |
|
102
|
|
|
$url = $this->constructUrl($vatNumber); |
|
103
|
|
|
$urlContent = $this->getUrlContent($url); |
|
104
|
|
|
|
|
105
|
|
|
if ($urlContent) { |
|
106
|
|
|
return (strpos($urlContent, 'Yes, valid VAT number') !== false); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
throw new InternalErrorException(__d('vat_number_check', 'Service unavailable')); |
|
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths