The guidelines provided on this page facilitate the initiation of the integration process with Chargebee APIs. Chargebee's server-side Software Development Kits (SDKs) diminish the effort required to utilize our REST APIs. This commences with minimizing the standardized code that you are required to author.
In this quickstart,
- You will install a Client library.
- Make calls to our Product Catalog APIs to define a plan-item price.
- Create a customer record in Chargebee, associating a test card to it.
Finally, you will make one API call which will create both a subscription and the associated invoice.
Select from one of the following SDK depending on your environment.
Step 1: Get your API key
Once you have signed up for a Chargebee account, a test site is created for you. Obtain a full-access API key from your test site for testing and development. You must be an admin or owner of the Chargebee site to see its API keys. Here’s an example of what the key can look like: test_YkiZnDgc1MWyjlWRNBJgHsKCRSSB8cuDlS
.
API keys must be managed securely. See this section to learn best practices.
Step 2: Download and set up the SDK
Install the latest version of the library with:
npm install chargebee
# or
yarn add chargebee
# or
pnpm install chargebee
Step 3: Create a customer with a payment method
Create a customer named Acme Eastern
and associate a test card payment method with them. Enable auto-collection
so that the card is charged automatically when you eventually create a subscription.
const chargebee = require('chargebee');
chargebee.configure({
site: '<YOUR_SITE_NAME>',
api_key: '<YOUR_API_KEY>',
});
try {
const { customer, card } = await chargebee.customer
.create({
id: 'acme-east',
company: 'Acme Eastern',
auto_collection: 'on',
card : {
number : "4111111111111111",
expiry_month : 12,
expiry_year : 2028,
cvv : "100"
}
})
.request();
} catch (err) {
// handle error
}
Step 4: Create an item family
Create an item family. This defines a group of products and services that you sell. In our example, it is Cloud Storage
.
const chargebee = require('chargebee');
chargebee.configure({
site: '<YOUR_SITE_NAME>',
api_key: '<YOUR_API_KEY>',
});
try {
const { item_family } = await chargebee.item_family
.create({
id: 'cloud-storage',
name: 'Cloud Storage'
})
.request();
} catch (err) {
// handle error
}
Step 5: Create an item
Now, create an item. An item defines one of the products within the Cloud Storage item family. Set its type
to plan
. An item of type
plan
is required to create a subscription. Let’s call it Silver Plan
.
const chargebee = require('chargebee');
chargebee.configure({
site: '<YOUR_SITE_NAME>',
api_key: '<YOUR_API_KEY>',
});
try {
const { item } = await chargebee.item
.create({
id : "silver-plan",
name : "Silver Plan",
type : "plan",
item_family_id: 'cloud-storage'
})
.request();
} catch (err) {
// handle error
}
Step 6: Create an item price
Item prices define the pricing for an item. They specify how often the customer is charged in a subscription, how much they’re charged, and what currency. Let’s create an item price for Silver Plan
, which charges $500 every month, and call it Silver USD monthly
.
const chargebee = require('chargebee');
chargebee.configure({
site: '<YOUR_SITE_NAME>',
api_key: '<YOUR_API_KEY>',
});
try {
const { item } = await chargebee.item_price
.create({
id : "silver-plan-USD-monthly",
item_id : "silver-plan",
name : "silver USD monthly",
pricing_model : "per_unit",
price : 50000,
external_name : "silver USD",
period_unit : "month",
period : 1
})
.request();
} catch (err) {
// handle error
}
Step 7: Create a subscription
We’re now ready to create a subscription for Acme Eastern
. Let’s subscribe the customer to 5
units of Silver USD monthly
.
const chargebee = require('chargebee');
chargebee.configure({
site: '<YOUR_SITE_NAME>',
api_key: '<YOUR_API_KEY>',
});
try {
const { item } = await chargebee.subscription
.create_with_items("acme-east", {
subscription_items: [
{
item_price_id: "silver-plan-USD-monthly",
quantity: 5
}
]
})
.request();
} catch (err) {
// handle error
}
The endpoint responds with a payload that carries the following objects:
- The subscription that was created.
- The customer for whom it was created.
- The details of the card that was charged.
- The invoice that was generated for the charge.
- A sample response is provided below:
{
"subscription": {
"id": "16Bjl4Sa1y72u3Ppw",
"billing_period": 1,
"billing_period_unit": "month",
"customer_id": "acme-east",
"status": "active",
"current_term_start": 1623416481,
"current_term_end": 1626008481,
"next_billing_at": 1626008481,
"created_at": 1623416481,
"started_at": 1623416481,
"activated_at": 1623416481,
"updated_at": 1623416481,
"has_scheduled_changes": false,
"resource_version": 1623416481565,
"deleted": false,
"object": "subscription",
"currency_code": "USD",
"subscription_items": [
{
"item_price_id": "silver-plan-USD-monthly",
"item_type": "plan",
"quantity": 5,
"unit_price": 50000,
"amount": 250000,
"free_quantity": 0,
"object": "subscription_item"
}
],
"due_invoices_count": 0,
"mrr": 0
},
"customer": {
"id": "acme-east",
"auto_collection": "on",
"net_term_days": 0,
"allow_direct_debit": false,
"created_at": 1623294986,
"taxability": "taxable",
"updated_at": 1623294986,
"pii_cleared": "active",
"resource_version": 1623294986772,
"deleted": false,
"object": "customer",
"card_status": "valid",
"promotional_credits": 0,
"refundable_credits": 0,
"excess_payments": 0,
"unbilled_charges": 0,
"preferred_currency_code": "USD",
"primary_payment_source_id": "pm_AzyuEASZtkKjx1wxQ",
"payment_method": {
"object": "payment_method",
"type": "card",
"reference_id": "tok_AzyuEASZtkKjl1wxP",
"gateway": "chargebee",
"gateway_account_id": "gw_AzZhVYSZlfWhVmD9",
"status": "valid"
}
},
"card": {
"status": "valid",
"gateway": "chargebee",
"gateway_account_id": "gw_AzZhVYSZlfWhVmD9",
"iin": "411111",
"last4": "1111",
"card_type": "visa",
"funding_type": "credit",
"expiry_month": 12,
"expiry_year": 2022,
"created_at": 1623294986,
"updated_at": 1623294986,
"resource_version": 1623294986774,
"object": "card",
"masked_number": "************1111",
"customer_id": "acme-east",
"payment_source_id": "pm_AzyuEASZtkKjx1wxQ"
},
"invoice": {
"id": "12",
"customer_id": "acme-east",
"subscription_id": "16Bjl4Sa1y72u3Ppw",
"recurring": true,
"status": "paid",
"price_type": "tax_exclusive",
"date": 1623416481,
"due_date": 1623416481,
"net_term_days": 0,
"exchange_rate": 1.0,
"total": 250000,
"amount_paid": 250000,
"amount_adjusted": 0,
"write_off_amount": 0,
"credits_applied": 0,
"amount_due": 0,
"paid_at": 1623416481,
"updated_at": 1623416481,
"resource_version": 1623416481559,
"deleted": false,
"object": "invoice",
"first_invoice": true,
"amount_to_collect": 0,
"round_off_amount": 0,
"new_sales_amount": 250000,
"has_advance_charges": false,
"currency_code": "USD",
"base_currency_code": "USD",
"is_gifted": false,
"term_finalized": true,
"tax": 0,
"line_items": [
{
"id": "li_16Bjl4Sa1y73T3Ppy",
"date_from": 1623416481,
"date_to": 1626008481,
"unit_amount": 50000,
"quantity": 5,
"amount": 250000,
"pricing_model": "per_unit",
"is_taxed": false,
"tax_amount": 0,
"object": "line_item",
"subscription_id": "16Bjl4Sa1y72u3Ppw",
"customer_id": "acme-east",
"description": "Silver USD",
"entity_type": "plan_item_price",
"entity_id": "silver-plan-USD-monthly",
"tax_exempt_reason": "tax_not_configured",
"discount_amount": 0,
"item_level_discount_amount": 0
}
],
"sub_total": 250000,
"linked_payments": [
{
"txn_id": "txn_16Bjl4Sa1y74A3Ppz",
"applied_amount": 250000,
"applied_at": 1623416481,
"txn_status": "success",
"txn_date": 1623416481,
"txn_amount": 250000
}
],
"dunning_attempts": [],
"applied_credits": [],
"adjustment_credit_notes": [],
"issued_credit_notes": [],
"linked_orders": []
}
}
Step 1: Get your API key
Once you have signed up for a Chargebee account, a test site is created for you. Obtain a full-access API key from your test site for testing and development. You must be an admin or owner of the Chargebee site to see its API keys. Here’s an example of what the key can look like: test_YkiZnDgc1MWyjlWRNBJgHsKCRSSB8cuDlS
.
API keys must be managed securely. Learn more about best practices.
Step 2: Download and set up the SDK
Install the latest version 2.x.x of the library with the following commands:
pip install 'chargebee>=2,<3'
or
easy_install --upgrade 'chargebee>=2,<3'
If you would prefer to install it from source, just checkout the latest version for 2.x.x by git checkout [latest 2.x.x release tag]
and install with the following command:
python setup.py install
Step 3: Create a Customer with a Payment Method
Create a customer named Acme Eastern
and associate a test card payment method with them. Enable auto-collection
so that the card is charged automatically when you eventually create a subscription.
import chargebee
import json
chargebee.configure("{site_api_key}","{site}")
result = chargebee.Customer.create({
"id": "acme-east",
"company": "Acme Eastern",
"card" : {
"number" : "4111111111111111",
"expiry_month" : 12,
"expiry_year" : 2028,
"cvv" : "100"
}
})
customer = result.customer
card = result.card
Step 4: Create an Item Family
Create an item family. This defines a group of products and services that you sell.
In our example, it is Cloud Storage
.
import chargebee
import json
chargebee.configure("{site_api_key}","{site}")
result = chargebee.ItemFamily.create({
"id" : "cloud-storage",
"name" : "Cloud Storage"
})
item_family = result.item_family
Step 5: Create an Item
Now, create an item. An item defines one of the products within the Cloud Storage
item family. Set its type
to plan
. An item of type
plan
is required to create a subscription.
Let’s call it Silver Plan
.
import chargebee
import json
chargebee.configure("{site_api_key}","{site}")
result = chargebee.Item.create({
"id" : "silver-plan",
"name" : "Silver Plan",
"type" : "plan",
"item_family_id": "cloud-storage"
})
item = result.item
Step 6: Create an Item Price
Item prices define the pricing for an item. T hey specify how often the customer is charged in a subscription, how much they’re charged, and what currency.
Let’s create an item price for Silver Plan
, which charges $500 every month, and call it Silver USD monthly
.
import chargebee
import json
chargebee.configure("{site_api_key}","{site}")
result = chargebee.ItemPrice.create({
"id" : "silver-plan-USD-monthly",
"item_id" : "silver-plan",
"name" : "silver USD monthly",
"pricing_model" : "per_unit",
"price" : 50000,
"external_name" : "silver USD",
"period_unit" : "month",
"period" : 1
})
item_price = result.item_price
Step 7: Create a Subscription
We’re now ready to create a subscription for Acme Eastern
. Let’s subscribe the customer to 5
units of Silver USD monthly
.
import chargebee
import json
chargebee.configure("{site_api_key}","{site}")
result = chargebee.Subscription.create_with_items("acme-east",{
"subscription_items" : [
{
"item_price_id" : "silver-plan-USD-monthly",
"quantity" : 5
}]
})
subscription = result.subscription
customer = result.customer
card = result.card
invoice = result.invoice
unbilled_charges = result.unbilled_charges
The endpoint responds with a payload that carries the following objects:
- The subscription that was created.
- The customer for whom it was created.
- The details of the card that was charged.
- The invoice that was generated for the charge.
- A sample response is provided below:
{
"subscription": {
"id": "16Bjl4Sa1y72u3Ppw",
"billing_period": 1,
"billing_period_unit": "month",
"customer_id": "acme-east",
"status": "active",
"current_term_start": 1623416481,
"current_term_end": 1626008481,
"next_billing_at": 1626008481,
"created_at": 1623416481,
"started_at": 1623416481,
"activated_at": 1623416481,
"updated_at": 1623416481,
"has_scheduled_changes": false,
"resource_version": 1623416481565,
"deleted": false,
"object": "subscription",
"currency_code": "USD",
"subscription_items": [
{
"item_price_id": "silver-plan-USD-monthly",
"item_type": "plan",
"quantity": 5,
"unit_price": 50000,
"amount": 250000,
"free_quantity": 0,
"object": "subscription_item"
}
],
"due_invoices_count": 0,
"mrr": 0
},
"customer": {
"id": "acme-east",
"auto_collection": "on",
"net_term_days": 0,
"allow_direct_debit": false,
"created_at": 1623294986,
"taxability": "taxable",
"updated_at": 1623294986,
"pii_cleared": "active",
"resource_version": 1623294986772,
"deleted": false,
"object": "customer",
"card_status": "valid",
"promotional_credits": 0,
"refundable_credits": 0,
"excess_payments": 0,
"unbilled_charges": 0,
"preferred_currency_code": "USD",
"primary_payment_source_id": "pm_AzyuEASZtkKjx1wxQ",
"payment_method": {
"object": "payment_method",
"type": "card",
"reference_id": "tok_AzyuEASZtkKjl1wxP",
"gateway": "chargebee",
"gateway_account_id": "gw_AzZhVYSZlfWhVmD9",
"status": "valid"
}
},
"card": {
"status": "valid",
"gateway": "chargebee",
"gateway_account_id": "gw_AzZhVYSZlfWhVmD9",
"iin": "411111",
"last4": "1111",
"card_type": "visa",
"funding_type": "credit",
"expiry_month": 12,
"expiry_year": 2022,
"created_at": 1623294986,
"updated_at": 1623294986,
"resource_version": 1623294986774,
"object": "card",
"masked_number": "************1111",
"customer_id": "acme-east",
"payment_source_id": "pm_AzyuEASZtkKjx1wxQ"
},
"invoice": {
"id": "12",
"customer_id": "acme-east",
"subscription_id": "16Bjl4Sa1y72u3Ppw",
"recurring": true,
"status": "paid",
"price_type": "tax_exclusive",
"date": 1623416481,
"due_date": 1623416481,
"net_term_days": 0,
"exchange_rate": 1.0,
"total": 250000,
"amount_paid": 250000,
"amount_adjusted": 0,
"write_off_amount": 0,
"credits_applied": 0,
"amount_due": 0,
"paid_at": 1623416481,
"updated_at": 1623416481,
"resource_version": 1623416481559,
"deleted": false,
"object": "invoice",
"first_invoice": true,
"amount_to_collect": 0,
"round_off_amount": 0,
"new_sales_amount": 250000,
"has_advance_charges": false,
"currency_code": "USD",
"base_currency_code": "USD",
"is_gifted": false,
"term_finalized": true,
"tax": 0,
"line_items": [
{
"id": "li_16Bjl4Sa1y73T3Ppy",
"date_from": 1623416481,
"date_to": 1626008481,
"unit_amount": 50000,
"quantity": 5,
"amount": 250000,
"pricing_model": "per_unit",
"is_taxed": false,
"tax_amount": 0,
"object": "line_item",
"subscription_id": "16Bjl4Sa1y72u3Ppw",
"customer_id": "acme-east",
"description": "Silver USD",
"entity_type": "plan_item_price",
"entity_id": "silver-plan-USD-monthly",
"tax_exempt_reason": "tax_not_configured",
"discount_amount": 0,
"item_level_discount_amount": 0
}
],
"sub_total": 250000,
"linked_payments": [
{
"txn_id": "txn_16Bjl4Sa1y74A3Ppz",
"applied_amount": 250000,
"applied_at": 1623416481,
"txn_status": "success",
"txn_date": 1623416481,
"txn_amount": 250000
}
],
"dunning_attempts": [],
"applied_credits": [],
"adjustment_credit_notes": [],
"issued_credit_notes": [],
"linked_orders": []
}
}
Step 1: Get your API key
Once you have signed up for a Chargebee account, a test site is created for you. Obtain a full-access API key from your test site for testing and development. You must be an admin or owner of the Chargebee site to see its API keys. Here’s an example of what the key can look like: test_YkiZnDgc1MWyjlWRNBJgHsKCRSSB8cuDlS
.
API keys must be managed securely. Learn more about best practices.
Step 2: Download and set up the SDK
Chargebee
is available on Packagist and can be installed using Composer
composer require chargebee/chargebee-php
Step 3: Create a customer with a payment method
Create a customer named Acme Eastern
and associate a test card payment method with them. Enable auto-collection
so that the card is charged automatically when you eventually create a subscription.
require 'ChargeBee.php';
ChargeBee_Environment::configure("my-domain-test","test_YkiZnDgc1MWyjlWRNBJgHsKCRSSB8cuDlS");
$result = ChargeBee_Customer::create(array(
"id" => "acme-east",
"company" => "Acme Eastern",
"autoCollection" => "on",
"card" => array(
"number" => "4111111111111111",
"cvv" => "100",
"expiryMonth" => 12,
"expiryYear" => 2022
)
));
$customer = $result->customer();
$card = $result->card();
Step 4: Create an Item Family
Create an item family. This defines a group of products and services that you sell.
In our example, it is Cloud Storage
.
$result = ChargeBee_ItemFamily::create(array(
"id" => "cloud-storage",
"name" => "Cloud Storage"
));
$itemFamily = $result->itemFamily();
Step 5: Create an Item
Now, create an item. An item defines one of the products within the Cloud Storage item family. Set its type
to plan
. An item of type
, plan
is required to create a subscription.
Let’s call it Silver Plan
.
$result = ChargeBee_Item::create(array(
"id" => "silver-plan",
"name" => "Silver Plan",
"type" => "plan",
"item_family_id" => "cloud-storage"
));
$item = $result->item();
Step 6: Create an item price
Item prices define the pricing for an item. They specify how often the customer is charged in a subscription, how much they’re charged, and what currency.
Let’s create an item price for Silver Plan
, which charges $500 every month, and call it Silver USD monthly
.
$result = ChargeBee_ItemPrice::create(array(
"id" => "silver-plan-USD-monthly",
"itemId" => "silver-plan",
"name" => "Silver USD monthly",
"pricingModel" => "per_unit",
"price" => 50000,
"externalName" => "Silver USD",
"periodUnit" => "month",
"period" => 1
));
$itemPrice = $result->itemPrice();
Step 7: Create a subscription
We’re now ready to create a subscription for Acme Eastern
. Let’s subscribe the customer to 5
units of Silver USD monthly
.
$result = ChargeBee_Subscription::createWithItems("acme-east",array(
"subscriptionItems" => array(array(
"itemPriceId" => "silver-plan-USD-monthly",
"quantity" => 5))
));
$subscription = $result->subscription();
$customer = $result->customer();
$card = $result->card();
$invoice = $result->invoice();
$unbilledCharges = $result->unbilledCharges();
The endpoint responds with a payload that carries the following objects:
- The subscription that was created.
- The customer for whom it was created.
- The details of the card that was charged.
- The invoice that was generated for the charge.
- A sample response is provided below:
{
"subscription": {
"id": "16Bjl4Sa1y72u3Ppw",
"billing_period": 1,
"billing_period_unit": "month",
"customer_id": "acme-east",
"status": "active",
"current_term_start": 1623416481,
"current_term_end": 1626008481,
"next_billing_at": 1626008481,
"created_at": 1623416481,
"started_at": 1623416481,
"activated_at": 1623416481,
"updated_at": 1623416481,
"has_scheduled_changes": false,
"resource_version": 1623416481565,
"deleted": false,
"object": "subscription",
"currency_code": "USD",
"subscription_items": [
{
"item_price_id": "silver-plan-USD-monthly",
"item_type": "plan",
"quantity": 5,
"unit_price": 50000,
"amount": 250000,
"free_quantity": 0,
"object": "subscription_item"
}
],
"due_invoices_count": 0,
"mrr": 0
},
"customer": {
"id": "acme-east",
"auto_collection": "on",
"net_term_days": 0,
"allow_direct_debit": false,
"created_at": 1623294986,
"taxability": "taxable",
"updated_at": 1623294986,
"pii_cleared": "active",
"resource_version": 1623294986772,
"deleted": false,
"object": "customer",
"card_status": "valid",
"promotional_credits": 0,
"refundable_credits": 0,
"excess_payments": 0,
"unbilled_charges": 0,
"preferred_currency_code": "USD",
"primary_payment_source_id": "pm_AzyuEASZtkKjx1wxQ",
"payment_method": {
"object": "payment_method",
"type": "card",
"reference_id": "tok_AzyuEASZtkKjl1wxP",
"gateway": "chargebee",
"gateway_account_id": "gw_AzZhVYSZlfWhVmD9",
"status": "valid"
}
},
"card": {
"status": "valid",
"gateway": "chargebee",
"gateway_account_id": "gw_AzZhVYSZlfWhVmD9",
"iin": "411111",
"last4": "1111",
"card_type": "visa",
"funding_type": "credit",
"expiry_month": 12,
"expiry_year": 2022,
"created_at": 1623294986,
"updated_at": 1623294986,
"resource_version": 1623294986774,
"object": "card",
"masked_number": "************1111",
"customer_id": "acme-east",
"payment_source_id": "pm_AzyuEASZtkKjx1wxQ"
},
"invoice": {
"id": "12",
"customer_id": "acme-east",
"subscription_id": "16Bjl4Sa1y72u3Ppw",
"recurring": true,
"status": "paid",
"price_type": "tax_exclusive",
"date": 1623416481,
"due_date": 1623416481,
"net_term_days": 0,
"exchange_rate": 1.0,
"total": 250000,
"amount_paid": 250000,
"amount_adjusted": 0,
"write_off_amount": 0,
"credits_applied": 0,
"amount_due": 0,
"paid_at": 1623416481,
"updated_at": 1623416481,
"resource_version": 1623416481559,
"deleted": false,
"object": "invoice",
"first_invoice": true,
"amount_to_collect": 0,
"round_off_amount": 0,
"new_sales_amount": 250000,
"has_advance_charges": false,
"currency_code": "USD",
"base_currency_code": "USD",
"is_gifted": false,
"term_finalized": true,
"tax": 0,
"line_items": [
{
"id": "li_16Bjl4Sa1y73T3Ppy",
"date_from": 1623416481,
"date_to": 1626008481,
"unit_amount": 50000,
"quantity": 5,
"amount": 250000,
"pricing_model": "per_unit",
"is_taxed": false,
"tax_amount": 0,
"object": "line_item",
"subscription_id": "16Bjl4Sa1y72u3Ppw",
"customer_id": "acme-east",
"description": "Silver USD",
"entity_type": "plan_item_price",
"entity_id": "silver-plan-USD-monthly",
"tax_exempt_reason": "tax_not_configured",
"discount_amount": 0,
"item_level_discount_amount": 0
}
],
"sub_total": 250000,
"linked_payments": [
{
"txn_id": "txn_16Bjl4Sa1y74A3Ppz",
"applied_amount": 250000,
"applied_at": 1623416481,
"txn_status": "success",
"txn_date": 1623416481,
"txn_amount": 250000
}
],
"dunning_attempts": [],
"applied_credits": [],
"adjustment_credit_notes": [],
"issued_credit_notes": [],
"linked_orders": []
}
}
Step 1: Get your API key
Once you have signed up for a Chargebee account, a test site is created for you. Obtain a full-access API key from your test site for testing and development. You must be an admin or owner of the Chargebee site to see its API keys. Here’s an example of what the key can look like: test_YkiZnDgc1MWyjlWRNBJgHsKCRSSB8cuDlS
.
API keys must be managed securely. See this section to learn best practices.
Step 2: Download and set up the SDK
Install the latest version with:
go get github.com/chargebee/chargebee-go/v3
Step 3: Create a customer with a payment method
Create a customer named Acme Eastern
and associate a test card payment method with them. Enable auto-collection
so that the card is charged automatically when you eventually create a subscription.
package main
import (
"fmt"
"github.com/chargebee/chargebee-go/v3"
customerAction "github.com/chargebee/chargebee-go/v3/actions/customer"
"github.com/chargebee/chargebee-go/v3/models/customer"
)
func main() {
chargebee.Configure("{site_api_key}","{site}");
res,err := customerAction.Create(&customer.CreateRequestParams {
id: "acme-east",
company: "Acme Eastern",
Card : &customer.CreateCardParams {
Number : "4111111111111111",
ExpiryMonth : chargebee.Int32(12),
ExpiryYear : chargebee.Int32(2028),
Cvv : "100",
},
}).Request()
if err != nil {
fmt.Println(err)
}else{
Customer := res.Customer
Card := res.Card
}
}
Step 4: Create an item family
Create an item family . This defines a group of products and services that you sell.
In our example, it is Cloud Storage
.
package main
import (
"fmt"
"github.com/chargebee/chargebee-go/v3"
itemFamilyAction "github.com/chargebee/chargebee-go/v3/actions/itemfamily"
"github.com/chargebee/chargebee-go/v3/models/itemfamily"
)
func main() {
chargebee.Configure("{site_api_key}","{site}");
res,err := itemFamilyAction.Create(&itemfamily.CreateRequestParams {
Id : "cloud-storage",
Name : "Cloud Storage",
}).Request()
if err != nil {
fmt.Println(err)
}else{
ItemFamily := res.ItemFamily
}
}
Step 5: Create an item
Now, create an item.
An item defines one of the products within the Cloud Storage item family. Set its type
to plan
. An item of type
plan
is required to create a subscription. Let’s call it Silver Plan
.
package main
import (
"fmt"
"github.com/chargebee/chargebee-go/v3"
itemAction "github.com/chargebee/chargebee-go/v3/actions/item"
"github.com/chargebee/chargebee-go/v3/models/item"
)
func main() {
chargebee.Configure("{site_api_key}","{site}");
res,err := itemAction.Create(&item.CreateRequestParams {
Id : "silver-plan",
Name : "Silver Plan",
Type : itemEnum.TypePlan,
ItemFamilyId : "cloud-storage"
}).Request()
if err != nil {
fmt.Println(err)
}else{
Item := res.Item
}
}
Step 6: Create an item price
Item prices define the pricing for an item. They specify how often the customer is charged in a subscription, how much they’re charged, and what currency. Let’s create an item price for Silver Plan
, which charges $500 every month, and call it Silver USD monthly
.
package main
import (
"fmt"
"github.com/chargebee/chargebee-go/v3"
itemPriceAction "github.com/chargebee/chargebee-go/v3/actions/itemprice"
"github.com/chargebee/chargebee-go/v3/models/itemprice"
)
func main() {
chargebee.Configure("{site_api_key}","{site}");
res,err := itemPriceAction.Create(&itemprice.CreateRequestParams {
Id : "silver-plan-USD-monthly",
ItemId : "silver-plan",
Name : "silver USD monthly",
PricingModel : enum.PricingModelPerUnit,
Price : chargebee.Int64(50000),
ExternalName : "silver USD",
PeriodUnit : itemPriceEnum.PeriodUnitMonth,
Period : chargebee.Int32(1),
}).Request()
if err != nil {
fmt.Println(err)
}else{
ItemPrice := res.ItemPrice
}
}
Step 7: Create a subscription
We’re now ready to create a subscription for Acme Eastern
. Let’s subscribe the customer to 5
units of Silver USD monthly
.
package main
import (
"fmt"
"github.com/chargebee/chargebee-go/v3"
subscriptionAction "github.com/chargebee/chargebee-go/v3/actions/subscription"
"github.com/chargebee/chargebee-go/v3/models/subscription"
)
func main() {
chargebee.Configure("{site_api_key}","{site}");
res,err := subscriptionAction.CreateWithItems("acme-east",&subscription.CreateWithItemsRequestParams {
SubscriptionItems : []*subscription.CreateWithItemsSubscriptionItemParams {
{
ItemPriceId : "silver-plan-USD-monthly",
Quantity : chargebee.Int32(5),
},
},
}).Request()
if err != nil {
fmt.Println(err)
}else{
Subscription := res.Subscription
Customer := res.Customer
Card := res.Card
Invoice := res.Invoice
UnbilledCharges := res.UnbilledCharges
}
}
The endpoint responds with a payload that carries the following objects:
- The subscription that was created.
- The customer for whom it was created.
- The details of the card that was charged.
- The invoice that was generated for the charge.
- A sample response is provided below:
{
"subscription": {
"id": "16Bjl4Sa1y72u3Ppw",
"billing_period": 1,
"billing_period_unit": "month",
"customer_id": "acme-east",
"status": "active",
"current_term_start": 1623416481,
"current_term_end": 1626008481,
"next_billing_at": 1626008481,
"created_at": 1623416481,
"started_at": 1623416481,
"activated_at": 1623416481,
"updated_at": 1623416481,
"has_scheduled_changes": false,
"resource_version": 1623416481565,
"deleted": false,
"object": "subscription",
"currency_code": "USD",
"subscription_items": [
{
"item_price_id": "silver-plan-USD-monthly",
"item_type": "plan",
"quantity": 5,
"unit_price": 50000,
"amount": 250000,
"free_quantity": 0,
"object": "subscription_item"
}
],
"due_invoices_count": 0,
"mrr": 0
},
"customer": {
"id": "acme-east",
"auto_collection": "on",
"net_term_days": 0,
"allow_direct_debit": false,
"created_at": 1623294986,
"taxability": "taxable",
"updated_at": 1623294986,
"pii_cleared": "active",
"resource_version": 1623294986772,
"deleted": false,
"object": "customer",
"card_status": "valid",
"promotional_credits": 0,
"refundable_credits": 0,
"excess_payments": 0,
"unbilled_charges": 0,
"preferred_currency_code": "USD",
"primary_payment_source_id": "pm_AzyuEASZtkKjx1wxQ",
"payment_method": {
"object": "payment_method",
"type": "card",
"reference_id": "tok_AzyuEASZtkKjl1wxP",
"gateway": "chargebee",
"gateway_account_id": "gw_AzZhVYSZlfWhVmD9",
"status": "valid"
}
},
"card": {
"status": "valid",
"gateway": "chargebee",
"gateway_account_id": "gw_AzZhVYSZlfWhVmD9",
"iin": "411111",
"last4": "1111",
"card_type": "visa",
"funding_type": "credit",
"expiry_month": 12,
"expiry_year": 2022,
"created_at": 1623294986,
"updated_at": 1623294986,
"resource_version": 1623294986774,
"object": "card",
"masked_number": "************1111",
"customer_id": "acme-east",
"payment_source_id": "pm_AzyuEASZtkKjx1wxQ"
},
"invoice": {
"id": "12",
"customer_id": "acme-east",
"subscription_id": "16Bjl4Sa1y72u3Ppw",
"recurring": true,
"status": "paid",
"price_type": "tax_exclusive",
"date": 1623416481,
"due_date": 1623416481,
"net_term_days": 0,
"exchange_rate": 1.0,
"total": 250000,
"amount_paid": 250000,
"amount_adjusted": 0,
"write_off_amount": 0,
"credits_applied": 0,
"amount_due": 0,
"paid_at": 1623416481,
"updated_at": 1623416481,
"resource_version": 1623416481559,
"deleted": false,
"object": "invoice",
"first_invoice": true,
"amount_to_collect": 0,
"round_off_amount": 0,
"new_sales_amount": 250000,
"has_advance_charges": false,
"currency_code": "USD",
"base_currency_code": "USD",
"is_gifted": false,
"term_finalized": true,
"tax": 0,
"line_items": [
{
"id": "li_16Bjl4Sa1y73T3Ppy",
"date_from": 1623416481,
"date_to": 1626008481,
"unit_amount": 50000,
"quantity": 5,
"amount": 250000,
"pricing_model": "per_unit",
"is_taxed": false,
"tax_amount": 0,
"object": "line_item",
"subscription_id": "16Bjl4Sa1y72u3Ppw",
"customer_id": "acme-east",
"description": "Silver USD",
"entity_type": "plan_item_price",
"entity_id": "silver-plan-USD-monthly",
"tax_exempt_reason": "tax_not_configured",
"discount_amount": 0,
"item_level_discount_amount": 0
}
],
"sub_total": 250000,
"linked_payments": [
{
"txn_id": "txn_16Bjl4Sa1y74A3Ppz",
"applied_amount": 250000,
"applied_at": 1623416481,
"txn_status": "success",
"txn_date": 1623416481,
"txn_amount": 250000
}
],
"dunning_attempts": [],
"applied_credits": [],
"adjustment_credit_notes": [],
"issued_credit_notes": [],
"linked_orders": []
}
}
Step 1: Get your API key
Once you have signed up for a Chargebee account, a test site is created for you. Obtain a full-access API key from your test site for testing and development. You must be an admin or owner of the Chargebee site to see its API keys. Here’s an example of what the key can look like: test_YkiZnDgc1MWyjlWRNBJgHsKCRSSB8cuDlS
.
API keys must be managed securely. See this section to learn best practices.
Step 2: Download and set up the SDK
Maven
For Maven, add the following dependency to your POM:
<dependency>
<groupId>com.chargebee</groupId>
<artifactId>chargebee-java</artifactId>
<version>LATEST</version>
</dependency>
The above code would fetch the latest version of the library. To download a specific version, replace LATEST
in the above code with the version number.
Gradle
For Gradle, add the following to build.gradle
:
implementation group: 'com.chargebee', name: 'chargebee-java', version: '{VERSION}'
To get the latest version, remove version: '{VERSION}'
in the above code.
Step 3: Create a customer with a payment method
Create a customer named Acme Eastern
and associate a test card payment method with them. Enable auto-collection
so that the card is charged automatically when you eventually create a subscription.
Environment.configure("my-domain-test","test_YkiZnDgc1MWyjlWRNBJgHsKCRSSB8cuDlS");
Result result = Customer.create()
.id("acme-east")
.company("Acme Eastern")
.autoCollection(on)
.cardNumber(4111111111111111)
.cardCvv(100)
.cardExpiryMonth(12)
.cardExpiryYear(2022)
.request();
Customer customer = result.customer();
Card card = result.card();
Step 4: Create an item family
Create an item family. This defines a group of products and services that you sell. In our example, it is Cloud Storage
.
Result result = ItemFamily.create()
.id("cloud-storage")
.name("Cloud Storage")
.request();
ItemFamily itemFamily = result.itemFamily();
Step 5: Create an item
Now, create an item. An item defines one of the products within the Cloud Storage item family. Set its type
to plan
. An item of type
plan
is required to create a subscription. Let’s call it Silver Plan
.
Result result = Item.create()
.id("silver-plan")
.name("Silver Plan")
.type(Type.PLAN)
.itemFamilyId("cloud-storage")
.request();
Item item = result.item();
Step 6: Create an item price
Item prices define the pricing for an item. They specify how often the customer is charged in a subscription, how much they’re charged, and what currency. Let’s create an item price for Silver Plan
, which charges $500 every month, and call it Silver USD monthly
.
Result result = ItemPrice.create()
.id("silver-plan-USD-monthly")
.itemId("silver-plan")
.name("Silver USD monthly")
.pricingModel(PricingModel.PER_UNIT)
.price(50000)
.externalName("Silver USD")
.periodUnit(PeriodUnit.MONTH)
.period(1)
.request();
ItemPrice itemPrice = result.itemPrice();
Step 7: Create a subscription
We’re now ready to create a subscription for Acme Eastern
. Let’s subscribe the customer to 5
units of Silver USD monthly
.
Result result = Subscription.createWithItems("acme-east")
.subscriptionItemItemPriceId(0,"silver-plan-USD-monthly")
.subscriptionItemQuantity(0,5)
.request();
Subscription subscription = result.subscription();
Customer customer = result.customer();
Card card = result.card();
Invoice invoice = result.invoice();
List<UnbilledCharge> unbilledCharges = result.unbilledCharges();
The endpoint responds with a payload that carries the following objects:
- The subscription that was created.
- The customer for whom it was created.
- The details of the card that was charged.
- The invoice that was generated for the charge.
- A sample response is provided below:
{
"subscription": {
"id": "16Bjl4Sa1y72u3Ppw",
"billing_period": 1,
"billing_period_unit": "month",
"customer_id": "acme-east",
"status": "active",
"current_term_start": 1623416481,
"current_term_end": 1626008481,
"next_billing_at": 1626008481,
"created_at": 1623416481,
"started_at": 1623416481,
"activated_at": 1623416481,
"updated_at": 1623416481,
"has_scheduled_changes": false,
"resource_version": 1623416481565,
"deleted": false,
"object": "subscription",
"currency_code": "USD",
"subscription_items": [
{
"item_price_id": "silver-plan-USD-monthly",
"item_type": "plan",
"quantity": 5,
"unit_price": 50000,
"amount": 250000,
"free_quantity": 0,
"object": "subscription_item"
}
],
"due_invoices_count": 0,
"mrr": 0
},
"customer": {
"id": "acme-east",
"auto_collection": "on",
"net_term_days": 0,
"allow_direct_debit": false,
"created_at": 1623294986,
"taxability": "taxable",
"updated_at": 1623294986,
"pii_cleared": "active",
"resource_version": 1623294986772,
"deleted": false,
"object": "customer",
"card_status": "valid",
"promotional_credits": 0,
"refundable_credits": 0,
"excess_payments": 0,
"unbilled_charges": 0,
"preferred_currency_code": "USD",
"primary_payment_source_id": "pm_AzyuEASZtkKjx1wxQ",
"payment_method": {
"object": "payment_method",
"type": "card",
"reference_id": "tok_AzyuEASZtkKjl1wxP",
"gateway": "chargebee",
"gateway_account_id": "gw_AzZhVYSZlfWhVmD9",
"status": "valid"
}
},
"card": {
"status": "valid",
"gateway": "chargebee",
"gateway_account_id": "gw_AzZhVYSZlfWhVmD9",
"iin": "411111",
"last4": "1111",
"card_type": "visa",
"funding_type": "credit",
"expiry_month": 12,
"expiry_year": 2022,
"created_at": 1623294986,
"updated_at": 1623294986,
"resource_version": 1623294986774,
"object": "card",
"masked_number": "************1111",
"customer_id": "acme-east",
"payment_source_id": "pm_AzyuEASZtkKjx1wxQ"
},
"invoice": {
"id": "12",
"customer_id": "acme-east",
"subscription_id": "16Bjl4Sa1y72u3Ppw",
"recurring": true,
"status": "paid",
"price_type": "tax_exclusive",
"date": 1623416481,
"due_date": 1623416481,
"net_term_days": 0,
"exchange_rate": 1.0,
"total": 250000,
"amount_paid": 250000,
"amount_adjusted": 0,
"write_off_amount": 0,
"credits_applied": 0,
"amount_due": 0,
"paid_at": 1623416481,
"updated_at": 1623416481,
"resource_version": 1623416481559,
"deleted": false,
"object": "invoice",
"first_invoice": true,
"amount_to_collect": 0,
"round_off_amount": 0,
"new_sales_amount": 250000,
"has_advance_charges": false,
"currency_code": "USD",
"base_currency_code": "USD",
"is_gifted": false,
"term_finalized": true,
"tax": 0,
"line_items": [
{
"id": "li_16Bjl4Sa1y73T3Ppy",
"date_from": 1623416481,
"date_to": 1626008481,
"unit_amount": 50000,
"quantity": 5,
"amount": 250000,
"pricing_model": "per_unit",
"is_taxed": false,
"tax_amount": 0,
"object": "line_item",
"subscription_id": "16Bjl4Sa1y72u3Ppw",
"customer_id": "acme-east",
"description": "Silver USD",
"entity_type": "plan_item_price",
"entity_id": "silver-plan-USD-monthly",
"tax_exempt_reason": "tax_not_configured",
"discount_amount": 0,
"item_level_discount_amount": 0
}
],
"sub_total": 250000,
"linked_payments": [
{
"txn_id": "txn_16Bjl4Sa1y74A3Ppz",
"applied_amount": 250000,
"applied_at": 1623416481,
"txn_status": "success",
"txn_date": 1623416481,
"txn_amount": 250000
}
],
"dunning_attempts": [],
"applied_credits": [],
"adjustment_credit_notes": [],
"issued_credit_notes": [],
"linked_orders": []
}
}
Step 1: Get your API key
Once you have signed up for a Chargebee account, a test site is created for you. Obtain a full-access API key from your test site for testing and development. You must be an admin or owner of the Chargebee site to see its API keys. Here’s an example of what the key can look like: test_YkiZnDgc1MWyjlWRNBJgHsKCRSSB8cuDlS
.
API keys must be managed securely. See this section to learn best practices.
Step 2: Download and set up the SDK
Install the latest version of the gem with the following command...
sudo gem install chargebee -v '~>2'
Step 3: Create a customer with a payment method
Create a customer named Acme Eastern
and associate a test card payment method with them. Enable auto-collection
so that the card is charged automatically when you eventually create a subscription.
require 'chargebee'
ChargeBee.configure(:site => "my-domain-test",
:api_key => "test_YkiZnDgc1MWyjlWRNBJgHsKCRSSB8cuDlS")
result = ChargeBee::Customer.create({
:id => "acme-east",
:company => "Acme Eastern",
:autoCollection => "on",
:card => {
:number => "4111111111111111",
:expiry_month => 12,
:expiry_year => 2022,
:cvv => "100"
}
})
customer = result.customer
card = result.card
Step 4: Create an item family
Create an item family. This defines a group of products and services that you sell. In our example, it is Cloud Storage
.
result = ChargeBee::ItemFamily.create({
:id => "cloud-storage",
:name => "Cloud Storage"
})
item_family = result.item_family
Step 5: Create an item
Now, create an item. An item defines one of the products within the Cloud Storage item family. Set its type
to plan
. An item of type
plan
is required to create a subscription. Let’s call it Silver Plan
.
result = ChargeBee::Item.create({
:id => "silver-plan",
:name => "Silver Plan",
:type => "plan",
:item_family_id => "cloud-storage"
})
item = result.item
Step 6: Create an item price
Item prices define the pricing for an item. They specify how often the customer is charged in a subscription, how much they’re charged, and what currency. Let’s create an item price for Silver Plan
, which charges $500 every month, and call it Silver USD monthly
.
result = ChargeBee::ItemPrice.create({
:id => "silver-plan-USD-monthly",
:item_id => "silver-plan",
:name => "Silver USD monthly",
:pricing_model => "per_unit",
:price => 50000,
:external_name => "Silver USD",
:period_unit => "month",
:period => 1
})
item_price = result.item_price
Step 7: Create a subscription
We’re now ready to create a subscription for Acme Eastern
. Let’s subscribe the customer to 5
units of Silver USD monthly
.
result = ChargeBee::Subscription.create_with_items("acme-east",{
:subscription_items => [{
:item_price_id => "silver-plan-USD-monthly",
:quantity => 5
}]
})
subscription = result.subscription
customer = result.customer
card = result.card
invoice = result.invoice
unbilled_charges = result.unbilled_charges
The endpoint responds with a payload that carries the following objects:
- The subscription that was created.
- The customer for whom it was created.
- The details of the card that was charged.
- The invoice that was generated for the charge.
- A sample response is provided below:
{
"subscription": {
"id": "16Bjl4Sa1y72u3Ppw",
"billing_period": 1,
"billing_period_unit": "month",
"customer_id": "acme-east",
"status": "active",
"current_term_start": 1623416481,
"current_term_end": 1626008481,
"next_billing_at": 1626008481,
"created_at": 1623416481,
"started_at": 1623416481,
"activated_at": 1623416481,
"updated_at": 1623416481,
"has_scheduled_changes": false,
"resource_version": 1623416481565,
"deleted": false,
"object": "subscription",
"currency_code": "USD",
"subscription_items": [
{
"item_price_id": "silver-plan-USD-monthly",
"item_type": "plan",
"quantity": 5,
"unit_price": 50000,
"amount": 250000,
"free_quantity": 0,
"object": "subscription_item"
}
],
"due_invoices_count": 0,
"mrr": 0
},
"customer": {
"id": "acme-east",
"auto_collection": "on",
"net_term_days": 0,
"allow_direct_debit": false,
"created_at": 1623294986,
"taxability": "taxable",
"updated_at": 1623294986,
"pii_cleared": "active",
"resource_version": 1623294986772,
"deleted": false,
"object": "customer",
"card_status": "valid",
"promotional_credits": 0,
"refundable_credits": 0,
"excess_payments": 0,
"unbilled_charges": 0,
"preferred_currency_code": "USD",
"primary_payment_source_id": "pm_AzyuEASZtkKjx1wxQ",
"payment_method": {
"object": "payment_method",
"type": "card",
"reference_id": "tok_AzyuEASZtkKjl1wxP",
"gateway": "chargebee",
"gateway_account_id": "gw_AzZhVYSZlfWhVmD9",
"status": "valid"
}
},
"card": {
"status": "valid",
"gateway": "chargebee",
"gateway_account_id": "gw_AzZhVYSZlfWhVmD9",
"iin": "411111",
"last4": "1111",
"card_type": "visa",
"funding_type": "credit",
"expiry_month": 12,
"expiry_year": 2022,
"created_at": 1623294986,
"updated_at": 1623294986,
"resource_version": 1623294986774,
"object": "card",
"masked_number": "************1111",
"customer_id": "acme-east",
"payment_source_id": "pm_AzyuEASZtkKjx1wxQ"
},
"invoice": {
"id": "12",
"customer_id": "acme-east",
"subscription_id": "16Bjl4Sa1y72u3Ppw",
"recurring": true,
"status": "paid",
"price_type": "tax_exclusive",
"date": 1623416481,
"due_date": 1623416481,
"net_term_days": 0,
"exchange_rate": 1.0,
"total": 250000,
"amount_paid": 250000,
"amount_adjusted": 0,
"write_off_amount": 0,
"credits_applied": 0,
"amount_due": 0,
"paid_at": 1623416481,
"updated_at": 1623416481,
"resource_version": 1623416481559,
"deleted": false,
"object": "invoice",
"first_invoice": true,
"amount_to_collect": 0,
"round_off_amount": 0,
"new_sales_amount": 250000,
"has_advance_charges": false,
"currency_code": "USD",
"base_currency_code": "USD",
"is_gifted": false,
"term_finalized": true,
"tax": 0,
"line_items": [
{
"id": "li_16Bjl4Sa1y73T3Ppy",
"date_from": 1623416481,
"date_to": 1626008481,
"unit_amount": 50000,
"quantity": 5,
"amount": 250000,
"pricing_model": "per_unit",
"is_taxed": false,
"tax_amount": 0,
"object": "line_item",
"subscription_id": "16Bjl4Sa1y72u3Ppw",
"customer_id": "acme-east",
"description": "Silver USD",
"entity_type": "plan_item_price",
"entity_id": "silver-plan-USD-monthly",
"tax_exempt_reason": "tax_not_configured",
"discount_amount": 0,
"item_level_discount_amount": 0
}
],
"sub_total": 250000,
"linked_payments": [
{
"txn_id": "txn_16Bjl4Sa1y74A3Ppz",
"applied_amount": 250000,
"applied_at": 1623416481,
"txn_status": "success",
"txn_date": 1623416481,
"txn_amount": 250000
}
],
"dunning_attempts": [],
"applied_credits": [],
"adjustment_credit_notes": [],
"issued_credit_notes": [],
"linked_orders": []
}
}
Step 1: Get your API key
Once you have signed up for a Chargebee account, a test site is created for you. Obtain a full-access API key from your test site for testing and development. You must be an admin or owner of the Chargebee site to see its API keys. Here’s an example of what the key can look like: test_YkiZnDgc1MWyjlWRNBJgHsKCRSSB8cuDlS
.
API keys must be managed securely. See this section to learn best practices.
Step 2: Download and set up the SDK
You can install any release of an active library version using NuGet—a package manager for Visual Studio—by running the following command in the NuGet Package Manager Console:
Install-Package ChargeBee -Version {MAJOR}.{MINOR}.{PATCH}
For example, the following commands are valid
- Install the latest version
Install-Package ChargeBee -Version 3.0.0
- Install an earlier version, say v2.5.1
Install-Package ChargeBee -Version 2.5.1
Step 3: Create a customer with a payment method
Create a customer named Acme Eastern
and associate a test card payment method with them. Enable auto-collection
so that the card is charged automatically when you eventually create a subscription.
using ChargeBee.Api;
using ChargeBee.Models;
ApiConfig.Configure("{site}","{site_api_key}");
EntityResult result = Customer.Create()
.Id("acme-east")
.Company("Acme Eastern")
.CardNumber("4111111111111111")
.CardExpiryMonth(12)
.CardExpiryYear(2028)
.CardCvv("100")
.Request();
Customer customer = result.Customer;
Card card = result.Card;
Step 4: Create an item family
Create an item family. This defines a group of products and services that you sell. In our example, it is Cloud Storage
.
using ChargeBee.Api;
using ChargeBee.Models;
ApiConfig.Configure("{site}","{site_api_key}");
EntityResult result = ItemFamily.Create()
.Id("cloud-storage")
.Name("Cloud Storage")
.Request();
ItemFamily itemFamily = result.ItemFamily;
Step 5: Create an item
Now, create an item. An item defines one of the products within the Cloud Storage item family. Set its type
to plan
. An item of type
plan
is required to create a subscription. Let’s call it Silver Plan
.
using ChargeBee.Api;
using ChargeBee.Models;
ApiConfig.Configure("{site}","{site_api_key}");
EntityResult result = Item.Create()
.Id("silver-plan")
.Name("Silver Plan")
.Type(TypeEnum.Plan)
.ItemFamilyId('cloud-storage')
.Request();
Item item = result.Item;
Step 6: Create an item price
Item prices define the pricing for an item. They specify how often the customer is charged in a subscription, how much they’re charged, and what currency. Let’s create an item price for Silver Plan
, which charges $500 every month, and call it Silver USD monthly
.
using ChargeBee.Api;
using ChargeBee.Models;
ApiConfig.Configure("{site}","{site_api_key}");
EntityResult result = ItemPrice.Create()
.Id("silver-plan-USD-monthly")
.ItemId("silver-plan")
.Name("silver USD monthly")
.PricingModel(PricingModelEnum.PerUnit)
.Price(50000)
.ExternalName("silver USD")
.PeriodUnit(PeriodUnitEnum.Month)
.Period(1)
.Request();
ItemPrice itemPrice = result.ItemPrice;
Step 7: Create a subscription
We’re now ready to create a subscription for Acme Eastern
. Let’s subscribe the customer to 5
units of Silver USD monthly
.
using ChargeBee.Api;
using ChargeBee.Models;
ApiConfig.Configure("{site}","{site_api_key}");
EntityResult result = Subscription.CreateWithItems("acme-east")
.SubscriptionItemItemPriceId(0,"silver-plan-USD-monthly")
.SubscriptionItemQuantity(0,5)
.Request();
Subscription subscription = result.Subscription;
Customer customer = result.Customer;
Card card = result.Card;
Invoice invoice = result.Invoice;
List<UnbilledCharge> unbilledCharges = result.UnbilledCharges;
The endpoint responds with a payload that carries the following objects:
- The subscription that was created.
- The customer for whom it was created.
- The details of the card that was charged.
- The invoice that was generated for the charge.
- A sample response is provided below:
{
"subscription": {
"id": "16Bjl4Sa1y72u3Ppw",
"billing_period": 1,
"billing_period_unit": "month",
"customer_id": "acme-east",
"status": "active",
"current_term_start": 1623416481,
"current_term_end": 1626008481,
"next_billing_at": 1626008481,
"created_at": 1623416481,
"started_at": 1623416481,
"activated_at": 1623416481,
"updated_at": 1623416481,
"has_scheduled_changes": false,
"resource_version": 1623416481565,
"deleted": false,
"object": "subscription",
"currency_code": "USD",
"subscription_items": [
{
"item_price_id": "silver-plan-USD-monthly",
"item_type": "plan",
"quantity": 5,
"unit_price": 50000,
"amount": 250000,
"free_quantity": 0,
"object": "subscription_item"
}
],
"due_invoices_count": 0,
"mrr": 0
},
"customer": {
"id": "acme-east",
"auto_collection": "on",
"net_term_days": 0,
"allow_direct_debit": false,
"created_at": 1623294986,
"taxability": "taxable",
"updated_at": 1623294986,
"pii_cleared": "active",
"resource_version": 1623294986772,
"deleted": false,
"object": "customer",
"card_status": "valid",
"promotional_credits": 0,
"refundable_credits": 0,
"excess_payments": 0,
"unbilled_charges": 0,
"preferred_currency_code": "USD",
"primary_payment_source_id": "pm_AzyuEASZtkKjx1wxQ",
"payment_method": {
"object": "payment_method",
"type": "card",
"reference_id": "tok_AzyuEASZtkKjl1wxP",
"gateway": "chargebee",
"gateway_account_id": "gw_AzZhVYSZlfWhVmD9",
"status": "valid"
}
},
"card": {
"status": "valid",
"gateway": "chargebee",
"gateway_account_id": "gw_AzZhVYSZlfWhVmD9",
"iin": "411111",
"last4": "1111",
"card_type": "visa",
"funding_type": "credit",
"expiry_month": 12,
"expiry_year": 2022,
"created_at": 1623294986,
"updated_at": 1623294986,
"resource_version": 1623294986774,
"object": "card",
"masked_number": "************1111",
"customer_id": "acme-east",
"payment_source_id": "pm_AzyuEASZtkKjx1wxQ"
},
"invoice": {
"id": "12",
"customer_id": "acme-east",
"subscription_id": "16Bjl4Sa1y72u3Ppw",
"recurring": true,
"status": "paid",
"price_type": "tax_exclusive",
"date": 1623416481,
"due_date": 1623416481,
"net_term_days": 0,
"exchange_rate": 1.0,
"total": 250000,
"amount_paid": 250000,
"amount_adjusted": 0,
"write_off_amount": 0,
"credits_applied": 0,
"amount_due": 0,
"paid_at": 1623416481,
"updated_at": 1623416481,
"resource_version": 1623416481559,
"deleted": false,
"object": "invoice",
"first_invoice": true,
"amount_to_collect": 0,
"round_off_amount": 0,
"new_sales_amount": 250000,
"has_advance_charges": false,
"currency_code": "USD",
"base_currency_code": "USD",
"is_gifted": false,
"term_finalized": true,
"tax": 0,
"line_items": [
{
"id": "li_16Bjl4Sa1y73T3Ppy",
"date_from": 1623416481,
"date_to": 1626008481,
"unit_amount": 50000,
"quantity": 5,
"amount": 250000,
"pricing_model": "per_unit",
"is_taxed": false,
"tax_amount": 0,
"object": "line_item",
"subscription_id": "16Bjl4Sa1y72u3Ppw",
"customer_id": "acme-east",
"description": "Silver USD",
"entity_type": "plan_item_price",
"entity_id": "silver-plan-USD-monthly",
"tax_exempt_reason": "tax_not_configured",
"discount_amount": 0,
"item_level_discount_amount": 0
}
],
"sub_total": 250000,
"linked_payments": [
{
"txn_id": "txn_16Bjl4Sa1y74A3Ppz",
"applied_amount": 250000,
"applied_at": 1623416481,
"txn_status": "success",
"txn_date": 1623416481,
"txn_amount": 250000
}
],
"dunning_attempts": [],
"applied_credits": [],
"adjustment_credit_notes": [],
"issued_credit_notes": [],
"linked_orders": []
}
}
What to do next?
You have successfully created a subscription.
Now, you can explore more subscription and billing use cases by checking out our help docs, guides and developer resources.
We're always happy to help you with any questions you might have!
support@chargebee.com