vat

package module
v0.0.0-...-2a35151 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2023 License: MIT Imports: 10 Imported by: 3

README

Package vat

Go Report Card GoDoc MIT licensed

Package for validating VAT numbers & retrieving VAT rates (from ibericode/vat-rates) in Go.

Installation

Use go get.

go get github.com/dannyvankooten/vat

Then import the package into your own code.

import "github.com/dannyvankooten/vat"

Usage

Validating VAT numbers

VAT numbers can be validated by format, existence or both. VAT numbers are looked up using the VIES VAT validation API.

package main

import "github.com/dannyvankooten/vat"

func main() {
  // Validate number by format + existence
  validity, err := vat.ValidateNumber("NL123456789B01")

  // Validate number format
  validity, err := vat.ValidateNumberFormat("NL123456789B01")

  // Validate number existence
  validity, err := vat.ValidateNumberExistence("NL123456789B01")
}
Retrieving VAT rates

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/dannyvankooten/vat"
)

func main() {
  c, err := vat.GetCountryRates("NL")
  r, err := c.GetRate("standard")

  fmt.Printf("Standard VAT rate for NL is %.2f", r)
  // Output: Standard VAT rate for NL is 21.00
}

License

MIT licensed. See the LICENSE file for details.

Documentation

Overview

Package vat helps you deal with European VAT in Go.

It offers VAT number validation using the VIES VAT validation API & VAT rates retrieval using jsonvat.com

Validate a VAT number

validity := vat.ValidateNumber("NL123456789B01")

Get VAT rate that is currently in effect for a given country

c, _ := vat.GetCountryRates("NL")
r, _ := c.GetRate("standard")

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrCountryNotFound = errors.New("vat: country not found")

ErrCountryNotFound indicates that this package could not find a country matching the VAT number prefix.

View Source
var ErrInvalidCountryCode = errors.New("vat: unknown country code")

ErrInvalidCountryCode will be returned when calling GetCountryRates with an invalid country code

View Source
var ErrInvalidRateLevel = errors.New("vat: unknown rate level")

ErrInvalidRateLevel will be returned when getting wrong rate level

View Source
var ErrInvalidVATNumber = errors.New("vat: vat number is invalid")

ErrInvalidVATNumber will be returned when an invalid VAT number is passed to a function that validates existence.

View Source
var ErrServiceUnavailable = errors.New("vat: service is unreachable")

ErrServiceUnavailable will be returned when VIES VAT validation API or jsonvat.com is unreachable.

View Source
var ServiceTimeout = 10

ServiceTimeout indicates the number of seconds before a service request times out.

Functions

func ValidateNumber

func ValidateNumber(n string) (bool, error)

ValidateNumber validates a VAT number by both format and existence. The existence check uses the VIES VAT validation SOAP API and will only run when format validation passes.

Example
vatNumber := "IE6388047V"
valid, _ := ValidateNumber(vatNumber)
fmt.Printf("Is %s valid: %t", vatNumber, valid)
Output:

Is IE6388047V valid: true

func ValidateNumberExistence

func ValidateNumberExistence(n string) (bool, error)

ValidateNumberExistence validates a VAT number by its existence using the VIES VAT API (using SOAP)

func ValidateNumberFormat

func ValidateNumberFormat(n string) (bool, error)

ValidateNumberFormat validates a VAT number by its format.

Types

type CountryRates

type CountryRates struct {
	CountryCode string `json:"country_code"`
	Periods     []RatePeriod
}

CountryRates holds the various differing VAT rate periods for a given country

func FetchRates

func FetchRates() ([]CountryRates, error)

FetchRates fetches the latest VAT rates from ibericode/vat-rates and updates the in-memory rates

func GetCountryRates

func GetCountryRates(countryCode string) (CountryRates, error)

GetCountryRates gets the CountryRates struct for a country by its ISO-3166-1-alpha2 country code.

func GetRates

func GetRates() ([]CountryRates, error)

GetRates returns the in-memory VAT rates

func (*CountryRates) GetRate

func (cr *CountryRates) GetRate(level string) (float32, error)

GetRate returns the currently active rate

func (*CountryRates) GetRateOn

func (cr *CountryRates) GetRateOn(t time.Time, level string) (float32, error)

GetRateOn returns the effective VAT rate on a given date

type RatePeriod

type RatePeriod struct {
	EffectiveFrom time.Time
	Rates         map[string]float32
}

RatePeriod represents a time and the various activate rates at that time.

type ViesResponse

type ViesResponse struct {
	CountryCode string
	VATNumber   string
	RequestDate string
	Valid       bool
	Name        string
	Address     string
}

func Lookup

func Lookup(vatNumber string) (*ViesResponse, error)

Lookup returns *ViesResponse for a VAT number

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL