-
Notifications
You must be signed in to change notification settings - Fork 1
/
vies_service.go
125 lines (109 loc) · 3.52 KB
/
vies_service.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
package vat
import (
"bytes"
"encoding/xml"
"errors"
"io"
"net/http"
"strings"
)
// LookupServiceInterface is an interface for the service that calls external services to validate VATs.
type LookupServiceInterface interface {
Validate(vatNumber string) error
}
// viesService validates EU VAT numbers with the VIES service
type viesService struct{}
// Validate returns whether the given VAT number is valid or not
func (s *viesService) Validate(vatNumber string) error {
if len(vatNumber) < 3 {
return ErrInvalidVATNumberFormat
}
res, err := s.lookup(s.getEnvelope(vatNumber))
if err != nil {
return ErrServiceUnavailable{Err: err}
}
defer func() {
_ = res.Body.Close()
}()
xmlRes, err := io.ReadAll(res.Body)
if err != nil {
return ErrServiceUnavailable{Err: err} // assume if we can't read the body then VIES gave us a bad response
}
// check if response contains "INVALID_INPUT" string
if bytes.Contains(xmlRes, []byte("INVALID_INPUT")) {
return ErrInvalidVATNumberFormat
}
// check if response contains "MS_UNAVAILABLE" string
if bytes.Contains(xmlRes, []byte("MS_UNAVAILABLE")) {
return ErrServiceUnavailable{Err: errors.New("vies reports service is unavailable")}
} else if bytes.Contains(xmlRes, []byte("MS_MAX_CONCURRENT_REQ")) {
return ErrServiceUnavailable{Err: errors.New("max concurrent requests limit hit")}
}
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"`
}
}
}
if err = xml.Unmarshal(xmlRes, &rd); err != nil {
return ErrServiceUnavailable{Err: err} // assume if response data doesn't match the struct, the service is down
}
r := &viesResponse{
CountryCode: rd.Soap.Soap.CountryCode,
VATNumber: rd.Soap.Soap.VATNumber,
RequestDate: rd.Soap.Soap.RequestDate,
Valid: rd.Soap.Soap.Valid,
Name: rd.Soap.Soap.Name,
Address: rd.Soap.Soap.Address,
}
if !r.Valid {
return ErrVATNumberNotFound
}
return nil
}
// getEnvelope parses VIES lookup envelope template
func (s *viesService) getEnvelope(n string) string {
n = strings.ToUpper(n)
countryCode := n[0:2]
vatNumber := n[2:]
const envelopeTemplate = `<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<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>`
e := envelopeTemplate
e = strings.Replace(e, "{{.countryCode}}", countryCode, 1)
e = strings.Replace(e, "{{.vatNumber}}", vatNumber, 1)
return e
}
// lookup calls the VIES service to get info about the VAT number
func (s *viesService) lookup(envelope string) (*http.Response, error) {
envelopeBuffer := bytes.NewBufferString(envelope)
client := http.Client{
Timeout: serviceTimeout,
}
return client.Post(viesServiceURL, "text/xml;charset=UTF-8", envelopeBuffer)
}
const viesServiceURL = "https://ec.europa.eu/taxation_customs/vies/services/checkVatService"
// viesResponse holds the response data from the Vies call
type viesResponse struct {
CountryCode string
VATNumber string
RequestDate string
Valid bool
Name string
Address string
}