-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternal.py
87 lines (68 loc) · 3 KB
/
external.py
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
# -*- coding: utf-8 -*-
""" VAT validator 9000
EXTERNAL
Handles SOAP-request to external validation service and formatting data into simple format.
Marko Loponen
marko.juhani.loponen@gmail.com
"""
import xml.etree.ElementTree as ET
import requests
import server
import db
data_nodes = ["countryCode", "vatNumber", "requestDate", "name", "address"]
""" Data field names of the external service.
"""
namespace = "urn:ec.europa.eu:taxud:vies:services:checkVat:types"
def external_check_VAT(vat):
""" Check VAT from ec.europa.eu's Vat service.
Parameters:
vat (str) : Company VAT number
Return:
SUCCESS:
(dict) : Company data in format:
"city": str,
"name": str,
"address": str,
"postcode": str,
"updatedOn": datetime in str when added to DB,
"vatNumber": str,
"countryCode": str,
"requestDate": date in str when requested from external service
FAILURE:
(dict) :
"""
url='http://ec.europa.eu/taxation_customs/vies/services/checkVatService'
headers = {'content-type': 'application/soap+xml'}
body = """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:ec.europa.eu:taxud:vies:services:checkVat:types">
<soapenv:Header/>
<soapenv:Body>
<urn:checkVat>
<urn:countryCode>"""+vat[0:2]+"""</urn:countryCode>
<urn:vatNumber>"""+vat[2:]+"""</urn:vatNumber>
</urn:checkVat>
</soapenv:Body>
</soapenv:Envelope>"""
response = (requests.post(url,data=body,headers=headers),)[0]
if response.status_code == 200:
root = ET.fromstring(response.text)
if root.find(".//{" + namespace + "}valid").text == "false":
return {"city":"","name":"","address":"","postcode":"","updatedOn":"","vatNumber":"","countryCode":"","requestDate":"","error": {"is_error": True, "reason": "No VAT", "info": "No company with this VAT exist"}}
company = {"error": {"is_error": False, "reason": "", "info": ""}}
for node in data_nodes:
if node == "address":
""" Splitting the address to postcode and city.
"""
temp = root.find(".//{" + namespace + "}"+node).text.split("\n")
if len(temp) == 2:
company[node] = temp[0]
temp2 = temp[1].split(" ")
if len(temp2) >= 2:
company["postcode"] = temp2[0]
company["city"] = " ".join(temp2[1:])
else:
company[node] = root.find(".//{" + namespace + "}" + node).text
if not server.isValidFormat(company['vatNumber']):
company['vatNumber'] = vat
db.insertNewCompany(company)
return company
return False