Skip to content

Commit 3963391

Browse files
Rework to add UK VAT validation and improve error reporting (#6)
* Rework to add UK VAT validation and improve error reporting * Few little tweaks * Appease the linter gods * I guess remove this blank import as it seems to be unnecessary * Few changes based on PR review * Remove ErrUnexpected since any error we don't handle is unexpected...
1 parent cfdeb7b commit 3963391

17 files changed

+652
-566
lines changed

README.md

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ Package vat
22
===
33

44
![Build](https://github.com/Teamwork/vat/actions/workflows/build.yml/badge.svg)
5-
[![Go Report Card](https://goreportcard.com/badge/github.com/teamwork/vat/v2)](https://goreportcard.com/report/github.com/teamwork/vat/v2)
6-
[![GoDoc](https://godoc.org/github.com/teamwork/vat/v2?status.svg)](https://godoc.org/github.com/teamwork/vat/v2)
5+
[![Go Report Card](https://goreportcard.com/badge/github.com/teamwork/vat/v3)](https://goreportcard.com/report/github.com/teamwork/vat/v3)
6+
[![GoDoc](https://godoc.org/github.com/teamwork/vat/v3?status.svg)](https://godoc.org/github.com/teamwork/vat/v3)
77
[![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/teamwork/vat/master/LICENSE)
88

9-
Package for validating VAT numbers & retrieving VAT rates (from [ibericode/vat-rates](https://github.com/ibericode/vat-rates)) in Go.
9+
Package for validating VAT numbers & retrieving VAT rates (
10+
from [ibericode/vat-rates](https://github.com/ibericode/vat-rates)) in Go.
1011

1112
Based on https://github.com/dannyvankooten/vat
1213

@@ -28,28 +29,38 @@ import "github.com/teamwork/vat"
2829

2930
### Validating VAT numbers
3031

31-
VAT numbers can be validated by format, existence or both. VAT numbers are looked up using the [VIES VAT validation API](http://ec.europa.eu/taxation_customs/vies/).
32+
VAT numbers can be validated by format, existence or both.
33+
34+
EU VAT numbers are looked up using the [VIES VAT validation API](http://ec.europa.eu/taxation_customs/vies/).
35+
36+
UK VAT numbers are looked up
37+
using [UK GOV VAT validation API](https://developer.service.hmrc.gov.uk/api-documentation/docs/api/service/vat-registered-companies-api/1.0)
38+
.
3239

3340
```go
3441
package main
3542

3643
import "github.com/teamwork/vat"
3744

3845
func main() {
39-
// Validate number by format + existence
40-
validity, err := vat.ValidateNumber("NL123456789B01")
46+
// These validation functions return an error if the VAT number is invalid. If no error, then it is valid.
47+
48+
// Validate number by format + existence
49+
err := vat.Validate("NL123456789B01")
4150

42-
// Validate number format
43-
validity, err := vat.ValidateNumberFormat("NL123456789B01")
51+
// Validate number format
52+
err := vat.ValidateFormat("NL123456789B01")
4453

45-
// Validate number existence
46-
validity, err := vat.ValidateNumberExistence("NL123456789B01")
54+
// Validate number existence
55+
err := vat.ValidateExists("NL123456789B01")
4756
}
4857
```
4958

5059
### Retrieving VAT rates
5160

52-
> This package relies on a [community maintained repository of vat rates](https://github.com/ibericode/vat-rates). We invite you to toggle notifications for that repository and contribute changes to VAT rates in your country once they are announced.
61+
> This package relies on a [community maintained repository of vat rates](https://github.com/ibericode/vat-rates). We
62+
> invite you to toggle notifications for that repository and contribute changes to VAT rates in your country once they
63+
> are announced.
5364
5465
To get VAT rate periods for a country, first get a CountryRates struct using the country's ISO-3166-1-alpha2 code.
5566

@@ -59,16 +70,16 @@ You can get the rate that is currently in effect using the `GetRate` function.
5970
package main
6071

6172
import (
62-
"fmt"
63-
"github.com/teamwork/vat"
73+
"fmt"
74+
"github.com/teamwork/vat"
6475
)
6576

6677
func main() {
67-
c, err := vat.GetCountryRates("IE")
68-
r, err := c.GetRate("standard")
78+
c, err := vat.GetCountryRates("IE")
79+
r, err := c.GetRate("standard")
6980

70-
fmt.Printf("Standard VAT rate for IE is %.2f", r)
71-
// Output: Standard VAT rate for IE is 23.00
81+
fmt.Printf("Standard VAT rate for IE is %.2f", r)
82+
// Output: Standard VAT rate for IE is 23.00
7283
}
7384
```
7485

errors.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package vat
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
// ErrInvalidVATNumberFormat will be returned if a VAT with an invalid format is given
9+
var ErrInvalidVATNumberFormat = errors.New("vat: VAT number format is invalid")
10+
11+
// ErrVATNumberNotFound will be returned if the given VAT number is not found in the external lookup service
12+
var ErrVATNumberNotFound = errors.New("vat: number not found as an existing active VAT number")
13+
14+
// ErrInvalidCountryCode indicates that this package could not find a country matching the VAT number prefix
15+
var ErrInvalidCountryCode = errors.New("vat: unknown country code")
16+
17+
// ErrInvalidRateLevel will be returned when getting wrong rate level
18+
var ErrInvalidRateLevel = errors.New("vat: unknown rate level")
19+
20+
// ErrServiceUnavailable will be returned when the service is unreachable
21+
type ErrServiceUnavailable struct {
22+
Err error
23+
}
24+
25+
// Error returns the error message
26+
func (e ErrServiceUnavailable) Error() string {
27+
return fmt.Sprintf("vat: service is unreachable: %v", e.Err)
28+
}

gen.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//go:generate mockgen -destination=mocks/mock_lookup_service.go -package=mocks github.com/teamwork/vat/v3 LookupServiceInterface
2+
3+
package vat

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/teamwork/vat/v2
1+
module github.com/teamwork/vat/v3
22

33
go 1.21
44

lookup_service.go

Lines changed: 0 additions & 29 deletions
This file was deleted.

mocks/mock_lookup_service.go

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mocks/mock_vies_service.go

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)