-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhmrc_funcs.py
66 lines (50 loc) · 2.34 KB
/
hmrc_funcs.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
import requests
from requests.auth import HTTPBasicAuth
import re
def verify_company_details(company_number, provided_name, provided_address, provided_postcode):
# API endpoint
url = f"https://api.company-information.service.gov.uk/company/{company_number}"
# Authentication details
api_key = "17e97701-02d0-4d75-ba41-18e83a9f6dbe"
auth_details = HTTPBasicAuth(api_key, "") # Password is blank
# Fetch company details from the API
response = requests.get(url, auth=auth_details)
# Check for 404 error
if response.status_code == 404:
return {"error": "Company not found"}
data = response.json()
# Extract company name and registered office address from the API response
official_name = data["company_name"]
official_address = data["registered_office_address"]["address_line_1"]
official_postcode = data["registered_office_address"]["postal_code"]
# Verify provided name, address, and postcode
name_verified = official_name == provided_name
address_verified = official_address == provided_address
postcode_verified = official_postcode == provided_postcode
return {
"name_verified": name_verified,
"address_verified": address_verified,
"postcode_verified": postcode_verified,
"official_name": official_name,
"official_address": official_address,
"official_postcode": official_postcode
}
def verify_vat_number(vat_number):
# Extract only the numeric part of the VAT number
vat_numeric = ''.join(re.findall(r'\d+', vat_number))
# Use the HMRC API to verify the VAT number
url = f"https://api.service.hmrc.gov.uk/organisations/vat/check-vat-number/lookup/{vat_numeric}"
response = requests.get(url)
verification_result = {
"vat_verified": False,
"official_vat": vat_number,
"official_name": None
}
if response.status_code == 200:
data = response.json()
# Check if the response contains the target's vatNumber
if "target" in data and "vatNumber" in data["target"] and data["target"]["vatNumber"] == vat_numeric:
verification_result["vat_verified"] = True
verification_result["official_vat"] = data["target"]["vatNumber"]
verification_result["official_name"] = data["target"].get("name", None)
return verification_result