Chargebee Typescript Client Library - API V2

This is the typescript Library for integrating with Chargebee. Sign up for a Chargebee account here.
Installation
Install the latest version 2.x.x of the library with the following commands:
$ npm install chargebee-typescript@">=2"
Then import the library as:
import {ChargeBee} from 'chargebee-typescript';
var chargebee = new ChargeBee();
Usage
To create a customer & subscription
import {
ChargeBee,
_subscription
} from 'chargebee-typescript';
var chargebee = new ChargeBee();
chargebee.configure({site : "mannar-test",
api_key : "test___dev__2d2aopx6Dh6tzq5qI3FuV7TuWMbxaudy"});
chargebee.subscription.create({
plan_id : "basic",
auto_collection : "off",
billing_address : {
first_name : "John",
last_name : "Doe",
line1 : "PO Box 9999",
city : "Walnut",
state : "California",
zip : "91789",
country : "US"
},
customer : {
first_name : "John",
last_name : "Doe",
email : "john@user.com"
}
}).request(function(error,result) {
if(error){
console.log(error);
}else{
console.log(`${result}`);
var subscription: typeof chargebee.subscription = result.subscription;
var customer: typeof chargebee.customer = result.customer;
var card: typeof chargebee.card = result.card;
var invoice: typeof chargebee.invoice = result.invoice;
var unbilled_charges: Array<typeof chargebee.unbilled_charge> = result.unbilled_charges;
}
});
Use of Filters In List API
import {
ChargeBee,
_subscription
} from 'chargebee-typescript';
var chargebee = new ChargeBee();
chargebee.configure({site : "mannar-test",
api_key : "test___dev__2d2aopx6Dh6tzq5qI3FuV7TuWMbxaudy"});
chargebee.subscription.list({
limit : 2,
plan_id : { in : ["basic","no-trial"] }
}).request(function(error,result) {
if(error){
console.log(error);
}else{
for(var i = 0; i < result.list.length;i++){
var entry=result.list[i]
console.log(`${entry}`);
var subscription: typeof chargebee.subscription = entry.subscription;
var customer: typeof chargebee.customer = entry.customer;
var card: typeof chargebee.card = entry.card;
}
}
});
To create subscription with custom headers and custom fields:
import {
ChargeBee,
_subscription
} from 'chargebee-typescript';
var chargebee = new ChargeBee();
chargebee.subscription.create({
plan_id : "basic"
}).param({
"cf_host_url" : "http://xyz.com",
"customer[cf_host_url]" : "http://xyz.com"
}).headers({
"chargebee-event-email" : "all-disabled",
"chargebee-request-origin-ip": "192.168.1.2"
}).request(function(error,result) {
if(error){
console.log(error);
}else{
var subscription = result.subscription
var customer = result.customer
console.log(subscription.cf_host_url);
console.log(customer.cf_host_url);
}
});
Error handling:
import {
ChargeBee,
_subscription
} from 'chargebee-typescript';
var chargebee = new ChargeBee();
chargebee.subscription.create({
}).request(function(error, result) {
if (error) {
handleCreateSubscriptionError(error);
} else {
console.log(result);
}
});
function handleCreateSubscriptionError(ex) {
if (ex.type == "payment") {
if ("card[number]" == ex.param) {
} else {
}
} else if (ex.type == "invalid_request") {
if ("coupon" == ex.param) {
if ("resource_not_found" == ex.api_error_code) {
} else if ("resource_limit_exhausted" == ex.api_error_code) {
} else if ("invalid_request" == ex.api_error_code) {
} else {
}
} else {
}
} else if (ex.type == "operation_failed") {
} else if (ex.type == "io_error") {
} else {
}
}
Documentation
The full documentation can be found on the chargebee site here:
https://apidocs.chargebee.com/docs/api?lang=typescript
License
See the LICENSE file.