-
Notifications
You must be signed in to change notification settings - Fork 59
/
Vies.php
317 lines (283 loc) · 8.29 KB
/
Vies.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
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
<?php
/**
* Vies
*
* Component using the European Commission (EC) VAT Information Exchange System (VIES) to verify and validate VAT
* registration numbers in the EU, using PHP and Composer.
*
* @author Michelangelo van Dam <dragonbe+github@gmail.com>
* @license MIT
*
*/
namespace DragonBe\Vies;
/**
* Class Vies
*
* This class provides a soap client for usage of the VIES web service
* provided by the European Commission to validate VAT numbers of companies
* registered within the European Union
*
* @category DragonBe
* @package \DragonBe\Vies
* @link http://ec.europa.eu/taxation_customs/vies/faqvies.do#item16
*/
class Vies
{
const VIES_PROTO = 'http';
const VIES_DOMAIN = 'ec.europa.eu';
const VIES_WSDL = '/taxation_customs/vies/checkVatService.wsdl';
const VIES_EU_COUNTRY_TOTAL = 28;
/**
* @var \SoapClient
*/
protected $soapClient;
/**
* @var string The WSDL for VIES service
*/
protected $wsdl;
/**
* @var array Options for the SOAP client
*/
protected $options;
/**
* @var HeartBeat A heartbeat checker to verify if the VIES service is available
*/
protected $heartBeat;
/**
* @var Validator a class to check Vat number control sum
*/
protected $validator;
/**
* Retrieves the SOAP client that will be used to communicate with the VIES
* SOAP service.
*
* @return \SoapClient
*/
public function getSoapClient()
{
if (null === $this->soapClient) {
$this->soapClient = new \SoapClient(
$this->getWsdl(),
$this->getOptions()
);
}
return $this->soapClient;
}
/**
* Sets the PHP SOAP Client and allows you to override the use of the native
* PHP SoapClient for testing purposes or for better integration in your own
* application.
*
* @param \SoapClient $soapClient
* @return Vies
*/
public function setSoapClient($soapClient)
{
$this->soapClient = $soapClient;
return $this;
}
/**
* Retrieves the location of the WSDL for the VIES SOAP service
*
* @return string
*/
public function getWsdl()
{
if (null === $this->wsdl) {
$this->wsdl = sprintf(
'%s://%s%s',
self::VIES_PROTO,
self::VIES_DOMAIN,
self::VIES_WSDL
);
}
return $this->wsdl;
}
/**
* Sets the location of the WSDL for the VIES SOAP Service
*
* @param string $wsdl
* @return Vies
* @example http://ec.europa.eu//taxation_customs/vies/checkVatService.wsdl
*/
public function setWsdl($wsdl)
{
$this->wsdl = $wsdl;
return $this;
}
/**
* Retrieves the options for the PHP SOAP service
*
* @return array
*/
public function getOptions()
{
if (null === $this->options) {
$this->options = [];
}
return $this->options;
}
/**
* Set options for the native PHP Soap Client
*
* @param array $options
* @return Vies
* @link http://php.net/manual/en/soapclient.soapclient.php
*/
public function setOptions($options)
{
$this->options = $options;
return $this;
}
/**
* Retrieves the heartbeat class that offers the option to check if the VIES
* service is up-and-running.
*
* @return HeartBeat
*/
public function getHeartBeat()
{
if (null === $this->heartBeat) {
$this->setHeartBeat(
new HeartBeat(
'tcp://' . self::VIES_DOMAIN,
80
)
);
}
return $this->heartBeat;
}
/**
* Sets the heartbeat functionality to verify if the VIES service is alive or not,
* especially since this service tends to have a bad reputation of its availability.
*
* @param HeartBeat $heartBeat
*/
public function setHeartBeat(HeartBeat $heartBeat)
{
$this->heartBeat = $heartBeat;
}
/**
* Sets the validator
*
* @return Validator
*/
public function getValidator()
{
if (null == $this->validator) {
$this->validator = new Validator();
}
return $this->validator;
}
/**
* Validates a given country code and VAT number and returns a
* \DragonBe\Vies\CheckVatResponse object
*
* @param string $countryCode The two-character country code of a European
* member country
* @param string $vatNumber The VAT number (without the country
* identification) of a registered company
* @param string $requesterCountryCode The two-character country code of a European
* member country
* @param string $requesterVatNumber The VAT number (without the country
* identification) of a registered company
* @return CheckVatResponse
* @throws ViesException
*/
public function validateVat($countryCode, $vatNumber, $requesterCountryCode = null, $requesterVatNumber = null)
{
if (!array_key_exists($countryCode, self::listEuropeanCountries())) {
throw new ViesException(sprintf('Invalid country code "%s" provided', $countryCode));
}
$vatNumber = self::filterVat($vatNumber);
if (!$this->validateVatSum($countryCode, $vatNumber)) {
$params = new \StdClass();
$params->countryCode = $countryCode;
$params->vatNumber = $vatNumber;
$params->requestDate = new \DateTime();
$params->valid = false;
return new CheckVatResponse($params);
}
$requestParams = array(
'countryCode' => $countryCode,
'vatNumber' => $vatNumber
);
if ($requesterCountryCode && $requesterVatNumber) {
if (!array_key_exists($requesterCountryCode, self::listEuropeanCountries())) {
throw new ViesException(sprintf('Invalid requestor country code "%s" provided', $requesterCountryCode));
}
$requesterVatNumber = self::filterVat($requesterVatNumber);
$requestParams['requesterCountryCode'] = $requesterCountryCode;
$requestParams['requesterVatNumber'] = $requesterVatNumber;
}
$response = $this->getSoapClient()->__soapCall(
'checkVatApprox',
array(
$requestParams
)
);
return new CheckVatResponse($response);
}
/**
* Validate a VAT number control sum
*
* @param string $countryCode The two-character country code of a European
* member country
* @param string $vatNumber The VAT number (without the country
* identification) of a registered company
* @return bool
*/
public function validateVatSum($countryCode, $vatNumber)
{
return $this->getValidator()->validate($countryCode, $vatNumber);
}
/**
* Filters a VAT number and normalizes it to an alfanumeric string
*
* @param string $vatNumber
* @return string
* @static
*/
public static function filterVat($vatNumber)
{
return str_replace(array(' ', '.', '-'), '', $vatNumber);
}
/**
* A list of European Union countries as of January 2015
*
* @return array
*/
public static function listEuropeanCountries()
{
return [
'AT' => 'Austria',
'BE' => 'Belgium',
'BG' => 'Bulgaria',
'CY' => 'Cyprus',
'CZ' => 'Czech Republic',
'DE' => 'Germany',
'DK' => 'Danmark',
'EE' => 'Estonia',
'EL' => 'Greece',
'ES' => 'Spain',
'FI' => 'Finland',
'FR' => 'France',
'HR' => 'Croatia',
'HU' => 'Hungary',
'IE' => 'Ireland',
'IT' => 'Italy',
'LU' => 'Luxembourg',
'LV' => 'Latvia',
'LT' => 'Lithuania',
'MT' => 'Malta',
'NL' => 'Netherlands',
'PL' => 'Poland',
'PT' => 'Portugal',
'RO' => 'Romania',
'SE' => 'Sweden',
'SI' => 'Slovenia',
'SK' => 'Slovakia',
'GB' => 'United Kingdom',
];
}
}