Skip to content

Package for parsing and validating VAT Identification numbers

License

Notifications You must be signed in to change notification settings

creativefabrica/vat

Repository files navigation

VAT

Build Go Report Card GoDoc MIT licensed

Package for parsing and validating VAT Identification numbers

based on https://github.com/Teamwork/vat with some different design choices

Installing

go get https://github.com/creativefabrica/vat
import "github.com/creativefabrica/vat"

Usage

Parsing a VAT Number:

vatIN, err := vat.Parse("NL822010690B01")
if err != nil {
    fmt.Printf("Invalid VAT number: %s\n", err)
    return
}
fmt.Printf("Country Code: %s Number: %s\n", vatIN.CountryCode, vatIN.Number)

You can also use the Must variant if you want to panic on error; this is useful on tests:

vatIN := vat.MustParse("INVALID")

For validating that a VAT Number actually exists, two different APIs are used:

You can pass the clients implemented on the vies and ukvat packages as functional options to the vat Validator initializer:

validator := vat.NewValidator(
    vat.WithViesService(vies.NewClient()),
    vat.WithUKVATService(ukvat.NewClient(
        ukvat.ClientCredentials{
            Secret: os.Getenv("UKVAT_API_CLIENT_SECRET"),
            ID:     os.Getenv("UKVAT_API_CLIENT_ID"),
        },
    )),
)

err := validator.Validate(context.Background(), "GB146295999727")
if err != nil {
    return err
}

If you only need EU validation and/or UK validation for some reason, you can skip passing the unneeded clients.
In this case the Validate function will only validate format using the Parse function.

Full example

Package usage: vies

httpClient := &http.Client{}
client := vies.NewClient(
    // Use this option to provide a custom http client
    vies.WithHTTPClient(httpClient),
)

Package usage: ukvat

Important

For validating VAT numbers that begin with GB you will need to sign up to gain access to the UK government's VAT API. Once you have signed up and acquired a client ID and client secret you can provide them on the intitalizer

httpClient := &http.Client{}
client := ukvat.NewClient(
    ukvat.ClientCredentials{
        Secret: os.Getenv("UKVAT_API_CLIENT_SECRET"),
        ID:     os.Getenv("UKVAT_API_CLIENT_ID"),
    },
    // Use this option to provide a custom http client
    ukvat.WithHTTPClient(httpClient),
)

Note

The ukvat.Client struct will cache the auth token needed for the validation requests. To avoid getting 403 responses when validating VAT numbers, the client will refresh the token 2 minutes before it expires

If you need to hit the sandbox version of the UK VAT API you can use the following option:

ukvat.WithBaseURL(ukvat.TestServiceBaseURL)

Package usage: vattest

You can use this package to provide a mock validation client to the vat.Validator. This is useful in tests:

validationClientMock := vattest.NewMockValidationClient(gomock.NewController(t))
validator := vat.NewValidator(
    vat.WithUKVATClient(validationClientMock),
    vat.WithViesClient(validationClientMock),
)

About

Package for parsing and validating VAT Identification numbers

Resources

License

Stars

Watchers

Forks

Packages

No packages published