# Functions

# Overview

Functions are utility operations that Chargebee.js runs on behalf of your site during a checkout or sign-up flow. They let you carry out an activity or fetch information about a customer before you submit anything to Chargebee. For example, you can check whether an EU VAT number is valid before you exempt a customer from taxes.

Functions ship as a separate module. Load the module once with load('functions'), then call the function you need on the chargebee object.

# Prerequisites

Before you use functions, ensure that you have:

# load('functions')

Loads and initializes the functions module, and attaches the function namespaces to the chargebee object.

# Syntax

chargebee.load('functions')
1

# Example

const chargebee = Chargebee.getInstance();

chargebee.load('functions').then(() => {
  // The functions module is ready. Call chargebee.vat.validateVat() here.
});
1
2
3
4
5

# Return value

Returns a promise that resolves when the module has loaded. Call functions only after the promise resolves.

# EU VAT validation

Chargebee validates a VAT number by sending a validation request to the VAT Information Exchange System (VIES) (opens new window), a search service owned by the European Commission.

Prerequisite

To carry out EU VAT validation, you must set your organization address (opens new window) in Chargebee Billing.

# vat.validateVat(options)

Validates a European Union VAT number against VIES.

# Syntax

chargebee.vat.validateVat(options)
1

# Example

const chargebee = Chargebee.getInstance();

chargebee.load('functions').then(() => {
  chargebee.vat.validateVat({
    country: "DE",
    vat_number: "DE123456789"
  }).then(result => {
    // Handle the validation result.
  }).catch(error => {
    // Handle the error.
  });
});
1
2
3
4
5
6
7
8
9
10
11
12

# Parameters

country
String Required
2-letter ISO 3166 alpha-2 country code
vat_number
String Required
EU VAT Number

# Return value

Returns a promise that resolves to an object holding the validation status and, when VIES returns one, a message. The promise rejects if either country or vat_number is missing.

# Sample response
{
  "status": "INVALID",
  "message": "Customer VAT number is invalid"
}
1
2
3
4
# Response properties
  • status: The validation result.

    Status Description
    VALID VAT number validation was successful and the number is valid.
    INVALID VAT number validation failed.
    UNDETERMINED No response from VIES, or another error occurred.
  • message: Describes the response received from VIES. This property is present only when VIES returns comments.