Package for validating VAT numbers & retrieving VAT rates ( from ibericode/vat-rates) in Go.
Based on https://github.com/dannyvankooten/vat
Use go get.
go get github.com/teamwork/vat
Then import the package into your own code.
import "github.com/teamwork/vat"
VAT numbers can be validated by format, existence or both.
EU VAT numbers are looked up using the VIES VAT validation API.
UK VAT numbers are looked up using UK GOV VAT validation API (requires signing up for the UK API).
package main
import "github.com/teamwork/vat"
func main() {
// These validation functions return an error if the VAT number is invalid. If no error, then it is valid.
// Validate number by format + existence
err := vat.Validate("NL123456789B01")
// Validate number format
err := vat.ValidateFormat("NL123456789B01")
// Validate number existence
err := vat.ValidateExists("NL123456789B01")
}
This package relies on a community maintained repository of vat rates. We invite you to toggle notifications for that repository and contribute changes to VAT rates in your country once they are announced.
To get VAT rate periods for a country, first get a CountryRates struct using the country's ISO-3166-1-alpha2 code.
You can get the rate that is currently in effect using the GetRate
function.
package main
import (
"fmt"
"github.com/teamwork/vat"
)
func main() {
c, err := vat.GetCountryRates("IE")
r, err := c.GetRate("standard")
fmt.Printf("Standard VAT rate for IE is %.2f", r)
// Output: Standard VAT rate for IE is 23.00
}
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 acquire a client ID and client secret, here's how to generate and use an access token:
package main
import "github.com/teamwork/vat"
func main() {
var ukAccessToken *vat.UKAccessToken
ukAccessToken, err := vat.GenerateUKAccessToken(vat.ValidatorOpts{
UKClientID: "yourClientID",
UKClientSecret: "yourClientSecret",
IsUKTest: true, // set this if you are testing this in their sandbox test API
})
if err != nil {
panic(err)
}
// Recommended to cache the access token until it expires
err := vat.Validate("GB123456789", vat.ValidatorOpts{
UKAccessToken: ukAccessToken.Token,
IsUKTest: true, // if token created in test mode, run validation in test mode
})
}
MIT licensed. See the LICENSE file for details.