-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (36 loc) · 1.16 KB
/
index.js
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
'use strict';
const soap = require('soap');
const endpoint = 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl';
module.exports = {
validate: Validate
};
function Validate(vat_number, country_code) {
const xmlRegex= /(<([^>]+)>)/ig;
const payload = {
vatNumber: vat_number,
countryCode: country_code.toUpperCase(),
};
return new Promise((resolve, reject) => {
soap.createClient(endpoint, (connectionError, soapClient) => {
if (connectionError) {
return reject({
error: connectionError,
});
}
soapClient.checkVat(payload, (viesError, viesResult) => {
if (viesError) {
return reject({
error: viesError.body.split('<faultstring>')[1].replace(xmlRegex, ''),
is_valid: false,
data: null,
});
}
resolve({
error: null,
is_valid: viesResult.valid,
data: viesResult,
});
});
});
});
}