-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuk_vat_service.go
51 lines (43 loc) · 1.48 KB
/
uk_vat_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
package vat
import (
"fmt"
"io"
"net/http"
"strings"
)
// ukVATService is service that calls a UK VAT API to validate UK VAT numbers.
type ukVATService struct{}
// Validate checks if the given VAT number exists and is active. If no error is returned, then it is.
func (s *ukVATService) Validate(vatNumber string) error {
vatNumber = strings.ToUpper(vatNumber)
// Only VAT numbers starting with "GB" are supported by this service. All others should go through the VIES service.
if !strings.HasPrefix(vatNumber, "GB") {
return ErrInvalidCountryCode
}
client := http.Client{
Timeout: serviceTimeout,
}
response, err := client.Get(fmt.Sprintf(ukVATServiceURL, vatNumber[2:]))
if err != nil {
return ErrServiceUnavailable{Err: err}
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(response.Body)
if response.StatusCode == http.StatusBadRequest {
return ErrInvalidVATNumberFormat
}
if response.StatusCode == http.StatusNotFound {
return ErrVATNumberNotFound
}
if response.StatusCode != http.StatusOK {
return ErrServiceUnavailable{
Err: fmt.Errorf("unexpected status code from UK VAT API: %d", response.StatusCode),
}
}
// If we receive a valid 200 response from this API, it means the VAT number exists and is valid
return nil
}
// API Documentation:
// https://developer.service.hmrc.gov.uk/api-documentation/docs/api/service/vat-registered-companies-api/1.0
const ukVATServiceURL = "https://api.service.hmrc.gov.uk/organisations/vat/check-vat-number/lookup/%s"