-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathvat.go
152 lines (132 loc) · 3.72 KB
/
vat.go
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
package vat
import (
"bytes"
"encoding/xml"
"errors"
"io/ioutil"
"net/http"
"regexp"
"strings"
"text/template"
"time"
)
type VATresponse struct {
CountryCode string
VATnumber string
RequestDate time.Time
Valid bool
Name string
Address string
}
const serviceUrl = "http://ec.europa.eu/taxation_customs/vies/services/checkVatService"
const envelope = `
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://schemas.conversesolutions.com/xsd/dmticta/v1">
<soapenv:Header/>
<soapenv:Body>
<checkVat xmlns="urn:ec.europa.eu:taxud:vies:services:checkVat:types">
<countryCode>{{.countryCode}}</countryCode>
<vatNumber>{{.vatNumber}}</vatNumber>
</checkVat>
</soapenv:Body>
</soapenv:Envelope>
`
var Timeout = 10 // seconds
var (
ErrVATnumberNotValid = errors.New("VAT number is not valid.")
ErrVATserviceUnreachable = errors.New("VAT number validation service is offline.")
ErrVATserviceError = "VAT number validation service returns an error : "
)
// Check returns *VATresponse for vat number
func CheckVAT(vatNumber string) (*VATresponse, error) {
vatNumber = sanitizeVatNumber(vatNumber)
e, err := getEnvelope(vatNumber)
if err != nil {
return nil, err
}
eb := bytes.NewBufferString(e)
client := http.Client{
Timeout: time.Duration(time.Duration(Timeout) * time.Second),
}
res, err := client.Post(serviceUrl, "text/xml;charset=UTF-8", eb)
if err != nil {
return nil, ErrVATserviceUnreachable
}
defer res.Body.Close()
xmlRes, err := ioutil.ReadAll(res.Body)
if bytes.Contains(xmlRes, []byte("INVALID_INPUT")) {
return nil, ErrVATnumberNotValid
}
var rd struct {
XMLName xml.Name `xml:"Envelope"`
Soap struct {
XMLName xml.Name `xml:"Body"`
Soap struct {
XMLName xml.Name `xml:"checkVatResponse"`
CountryCode string `xml:"countryCode"`
VATnumber string `xml:"vatNumber"`
RequestDate string `xml:"requestDate"` // 2015-03-06+01:00
Valid bool `xml:"valid"`
Name string `xml:"name"`
Address string `xml:"address"`
}
SoapFault struct {
XMLName string `xml:"Fault"`
Code string `xml:"faultcode"`
Message string `xml:"faultstring"`
}
}
}
if err := xml.Unmarshal(xmlRes, &rd); err != nil {
return nil, err
}
if rd.Soap.SoapFault.Message != "" {
return nil, errors.New(ErrVATserviceError + rd.Soap.SoapFault.Message)
}
if rd.Soap.Soap.RequestDate == "" {
return nil, errors.New("service returned invalid request date")
}
pDate, err := time.Parse("2006-01-02-07:00", rd.Soap.Soap.RequestDate)
if err != nil {
return nil, err
}
r := &VATresponse{
CountryCode: rd.Soap.Soap.CountryCode,
VATnumber: rd.Soap.Soap.VATnumber,
RequestDate: pDate,
Valid: rd.Soap.Soap.Valid,
Name: rd.Soap.Soap.Name,
Address: rd.Soap.Soap.Address,
}
return r, nil
}
// IsValid returns true if vat number is correct
func IsValidVAT(vatNumber string) (bool, error) {
r, err := CheckVAT(vatNumber)
if err != nil {
return false, err
}
return r.Valid, nil
}
// sanitizeVatNumber removes white space
func sanitizeVatNumber(vatNumber string) string {
vatNumber = strings.TrimSpace(vatNumber)
return regexp.MustCompile(" ").ReplaceAllString(vatNumber, "")
}
// getEnvelope parses envelope template
func getEnvelope(vatNumber string) (string, error) {
if len(vatNumber) < 3 {
return "", errors.New("VAT number is too short.")
}
t, err := template.New("envelope").Parse(envelope)
if err != nil {
return "", err
}
var result bytes.Buffer
if err := t.Execute(&result, map[string]string{
"countryCode": strings.ToUpper(vatNumber[0:2]),
"vatNumber": vatNumber[2:],
}); err != nil {
return "", err
}
return result.String(), nil
}