# 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:
- Set up Chargebee.js in your application.
- Initialized Chargebee.js with a publishable API key (opens new window). Functions call Chargebee through the JavaScript API, which authenticates with the publishable key.
# load('functions')
Loads and initializes the functions module, and attaches the function namespaces to the chargebee object.
# Syntax
chargebee.load('functions')
# Example
const chargebee = Chargebee.getInstance();
chargebee.load('functions').then(() => {
// The functions module is ready. Call chargebee.vat.validateVat() here.
});
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)
# 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.
});
});
2
3
4
5
6
7
8
9
10
11
12
# Parameters
# 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"
}
2
3
4
# Response properties
status: The validation result.Status Description VALIDVAT number validation was successful and the number is valid. INVALIDVAT number validation failed. UNDETERMINEDNo response from VIES, or another error occurred. message: Describes the response received from VIES. This property is present only when VIES returns comments.