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. 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.
});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), a search service owned by the European Commission.
Prerequisite
To carry out EU VAT validation, you must set your organization address 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.
});
});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"
}Response properties
status: The validation result.
Status | Description |
|---|---|
| VAT number validation was successful and the number is valid. |
| VAT number validation failed. |
| No response from VIES, or another error occurred. |
message: Describes the response received from VIES. This property is present only when VIES returns comments.