-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvies_service.go
More file actions
137 lines (118 loc) · 4.21 KB
/
vies_service.go
File metadata and controls
137 lines (118 loc) · 4.21 KB
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
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, opts ValidatorOpts) error
}
// lookupServiceWithResponse is implemented by services that can return the full response.
type lookupServiceWithResponse interface {
validateWithResponse(vatNumber string, opts ValidatorOpts) (*LookupResponse, 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, opts ValidatorOpts) error {
_, err := s.validateWithResponse(vatNumber, opts)
return err
}
// validateWithResponse performs validation and returns the full VIES response.
func (s *viesService) validateWithResponse(vatNumber string, _ ValidatorOpts) (*LookupResponse, error) {
if len(vatNumber) < 3 {
return nil, ErrInvalidVATNumberFormat
}
res, err := s.lookup(s.getEnvelope(vatNumber))
if err != nil {
return nil, ErrServiceUnavailable{Err: err}
}
defer func() {
_ = res.Body.Close()
}()
xmlRes, err := io.ReadAll(res.Body)
if err != nil {
return nil, 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 nil, ErrInvalidVATNumberFormat
}
// check if response contains "MS_UNAVAILABLE" string
if bytes.Contains(xmlRes, []byte("MS_UNAVAILABLE")) {
return nil, ErrServiceUnavailable{Err: errors.New("vies reports service is unavailable")}
} else if bytes.Contains(xmlRes, []byte("MS_MAX_CONCURRENT_REQ")) {
return nil, 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 nil, 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 nil, ErrVATNumberNotFound
}
return &LookupResponse{VIESResponse: r}, 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 service.
type VIESResponse struct {
CountryCode string `json:"countryCode"`
VATNumber string `json:"vatNumber"`
RequestDate string `json:"requestDate"`
Valid bool `json:"valid"`
Name string `json:"name"`
Address string `json:"address"`
}