API Reference
VatCalculator Class
The main class for handling VAT calculations and validations.
Constructor
constructor(config: Partial<VatCalculatorConfig> = {})Creates a new VAT calculator instance with optional configuration.
Configuration Options
interface VatCalculatorConfig {
businessCountryCode?: CountryCode
validateVatNumbers?: boolean
validatePostalCodes?: boolean
validateCountryCodes?: boolean
forwardSoapFaults?: boolean
soapTimeout?: number
rules?: VatRules
}Core Methods
calculate
calculate(
netPrice: number,
countryCode?: CountryCode,
postalCode?: string,
company: boolean = false,
type?: VatRateType
): VatCalculationResultCalculates VAT for a given price and location.
Returns:
interface VatCalculationResult {
netPrice: number
grossPrice: number
vatAmount: number
vatRate: number
countryCode: CountryCode
isCompany: boolean
details: {
ruleApplied: VatRateType
reverseCharge: boolean
vatNumberUsed?: string
postalCodeUsed?: string
}
}calculateNet
calculateNet(
grossPrice: number,
countryCode?: CountryCode,
postalCode?: string,
company: boolean = false,
type?: VatRateType
): VatCalculationResultCalculates the net price from a gross price.
VAT Number Methods
isValidVatNumber
async isValidVatNumber(vatNumber: string): Promise<boolean>Validates a VAT number against the VIES service.
getVatDetails
async getVatDetails(vatNumber: string): Promise<VatNumberValidationResult>Gets detailed information about a VAT number.
Returns:
interface VatNumberValidationResult {
isValid: boolean
name?: string
address?: string
countryCode?: CountryCode
vatNumber?: string
isCompany: boolean
}Setter Methods
setCountryCode
setCountryCode(countryCode: CountryCode): thisSets the country code for VAT calculations.
setPostalCode
setPostalCode(postalCode: string): thisSets the postal code for VAT calculations.
setCompany
setCompany(company: boolean): thisSets whether the customer is a business.
setVatNumber
setVatNumber(vatNumber: string): thisSets the VAT number for the calculation.
setBusinessCountryCode
setBusinessCountryCode(businessCountryCode: CountryCode): thisSets the business country code.
Getter Methods
getNetPrice
getNetPrice(): numberGets the current net price.
getCountryCode
getCountryCode(): CountryCode | undefinedGets the current country code.
getPostalCode
getPostalCode(): string | undefinedGets the current postal code.
isCompany
isCompany(): booleanGets whether the customer is a business.
getVatNumber
getVatNumber(): string | undefinedGets the current VAT number.
Utility Methods
shouldCollectVat
shouldCollectVat(countryCode: CountryCode): booleanDetermines whether VAT should be collected for a given country.
getTaxRateForCountry
getTaxRateForCountry(
countryCode: CountryCode,
company: boolean = false,
type?: VatRateType
): numberGets the VAT rate for a specific country.
getTaxRateForLocation
getTaxRateForLocation(
countryCode: CountryCode,
postalCode?: string,
company: boolean = false,
type?: VatRateType
): numberGets the VAT rate for a specific location, considering special territories.
Types
CountryCode
type CountryCode = 'AT' | 'BE' | 'BG' | 'CY' | 'CZ' | 'DE' | 'DK' | 'EE' | 'EL' |
'ES' | 'FI' | 'FR' | 'GB' | 'HR' | 'HU' | 'IE' | 'IT' | 'LT' | 'LU' | 'LV' |
'MT' | 'NL' | 'PL' | 'PT' | 'RO' | 'SE' | 'SI' | 'SK'VatRateType
type VatRateType = 'standard' | 'reduced' | 'super-reduced' | 'parking' | 'high' | 'low'VatRules
interface VatRules {
[key: string]: CountryVatRule
}
interface CountryVatRule {
rate: number
rates?: {
[key in VatRateType]?: number
}
specialRules?: {
vatNumberRequired?: boolean
reverseCharge?: boolean
}
}Exceptions
The library throws the following exceptions:
VatCalculatorException
Base exception class for all VAT calculator errors.
InvalidCountryCodeException
Thrown when an invalid country code is provided.
InvalidPostalCodeException
Thrown when an invalid postal code format is provided.
InvalidVatNumberException
Thrown when an invalid VAT number format is provided.
VatCheckUnavailableException
Thrown when the VIES VAT validation service is unavailable.
Usage Examples
See the Usage Guide for detailed examples of how to use these APIs.