vat

package module
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: MIT Imports: 10 Imported by: 0

README

Package vat

Build Go Report Card GoDoc MIT licensed

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

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

Installation

Use go get.

go get github.com/teamwork/vat

Then import the package into your own code.

import "github.com/teamwork/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/teamwork/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/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
}

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

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.

func ValidateNumberExistence

func ValidateNumberExistence(vatNumber 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
}

ViesResponse holds the response data from the Vies call

func Lookup

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

Lookup returns *ViesResponse for a VAT number

type ViesService

type ViesService struct{}

ViesService implements ViesServiceInterface using the HTTP client.

func (*ViesService) Lookup

func (s *ViesService) Lookup(envelope string) (*http.Response, error)

Lookup calls the VIES service to get info about the VAT number

type ViesServiceInterface

type ViesServiceInterface interface {
	Lookup(envelope string) (*http.Response, error)
}

ViesServiceInterface is an interface for the service that calls VIES.

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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