Integrating with Chargebee
This page assumes that you already have a Chargebee account created, have metered billing enabled, and are able to create plans. To sign-up and create a Chargebee account, visit the Chargebee website.
If metered billing is not enabled, you’ll need to contact support@chargebee.com to enable the feature on your Chargebee site.
How it works
A billing meter is created via the Moesif UI for a specific metric and filter criteria that you want to charge for.
- The filter criteria defines what the meter should include such as specific endpoints, certain customers, or other filter criteria.
- The metric is what is metered. It could be number of API calls, unique users, an aggregation on a header or body field, or any other metric trackable by Moesif.
Once the billing meter is created, Moesif will automatically meter the usage for each company and charge your customers via Chargebee. Moesif billing meters support a variety of billing models including prepaid, postpaid, Pay As You Go (PAYG) and more. If you have different features or metrics you need to charge on, you can create multiple meters in Moesif, each linked to a different Plans and Prices in Chargebee. The integration also syncs subscription and revenue data from Chargebee to Moesif so you can understand how API usage translates to revenue.
Prerequisites
In order to integrate Chargebee with Moesif, there are a few prerequisites which must be completed. These include:
- Creating an account with Chargebee
- Creating a plan in Chargebee
The plan must have This plan is metered set to true in order for usage to be updated correctly by Moesif. It is also recommended that the process charges for shared usage data by field be set to sum of usage values during period.
Configuring the Chargebee integration
Moesif’s connection with Chargebee involves 2-way communication. Moesif has created a simple way to get all the info you need and to configure certain variables for Chargebee in a single screen.The Chargebee setup details screen can be accessed a few ways, including:
Through Settings > Extensions
and then selecting install on the Chargebee extension
Through the Billing Meters screen Edit Billing Providers dropdown
And through the Create New Billing Meter screen
Once you’ve opened the configuration screen through one of the routes above, you can get the info needed to add the Moesif Webhook to Chargebee, inputs to plug the Chargebee API into Moesif, and customize your Customer ID source in a single place.
Adding the Moesif Webhook to Chargebee
In Chargebee, a Webhook needs to be added so that Chargebee can send subscription updates to Moesif. To do this you’ll need to make sure you are logged into your Chargebee account. Once logged in, use the left-side menu to click on the Settings > Configure Chargebee menu item.
Once you’re on the Configure Chargebee page click on Webhooks item in the scrollable list.
Then, Click on the Add Webhook button.
Lastly, add the Moesif Webhook URL into the Webhook URL field and the Moesif Application ID into the Username field. Then click on the Create button.
If not selected, you will need to enable Protect webhook URL with basic authentication.
The Endpoint URL value you add to Chargebee should also contain your Moesif Application ID. The generated URL in the Moesif installation page will already contain the correct Application ID and have it appended to the URL.
Now that the webhook is created, Chargebee also allows you to test it. This can be done by clicking on the Test Webhook button.
Click Test URL. The results should show Successfully received OK response from the webhook.
Configuring the Chargebee extension in Moesif
In Moesif, you will need to plug in the Chargebee API details so that Moesif can post usage details to Chargebee to bill upon. These details can be entered into the Chargebee API Version, Chargebee API Key, and Site fields on the Chargebee Setup Details screen.
Currently, Moesif only supports v2 of the Chargebee API.
The Chargebee API Key can be found by navigating to the Configure Chargebee screen and clicking on API Keys from the scrollable menu.
On the API Keys screen, copy the full_access_key_v1.
Copy the value listed under full_access_key_v1 and paste it into the Chargebee API Key field on the Chargebee Setup Details screen.
Lastly, grab the Site name from your URL bar in your browser and paste it into the Site field.
The filled out entries will look similar to this.
Once completed, be sure to click Save.
Set the Id Mapping
The Id Mapping Configuration enables you to specify the correct Chargebee subscription field that contains the Moesif Company Id. Moesif will use this to link your Subscriptions in Chargebee with Companies in Moesif. By default this is subscription.id
, but you’ll likely need to modify this unless you use Chargebee’s default subscription ids to identify customers on your API. Typically, this is a metadata field or Custom Field added to the Chargebee Subscription object like company_id
or tenant_id
.
Correct linking of Moesif companies to Chargebee subscriptions is required for metered billing to function.
While not required, you can also link Chargebee Customers to Moesif User Objects. By default, this is customer.id
, but can be changed to any field present on the Customer object in Chargebee such as a metadata field or custom field.
An example of how to do this is via a custom field in Chargebee. For instance, both the Customer and Subscription could have a user_id and company_id object that enable you to save custom fields in Chargebee. Once your custom field is created, you can add data to the Customer object programmatically through the Chargebee APIs or through the Chargebee UI. Once populated, you will see the value populated in the custom field in the UI.
The same can be seen for the Subscription object as well.
Now, you’d want to make sure that the data you enter into the Custom Fields match whatever value you have in the Moesif UserId and/or CompanyId fields. Based on the custom fields above, we could map the values into Moesif by going to the Chargebee configuration screen and changing the values in the Set Id Mapping fields to match.
If you are using metadata fields, the configuration will look like this.
It’s important to remember that the values you see under the Moesif CompanyId and UserId should be what you are mapping to in Chargebee through this configuration. This is how usage is synced for the user/company.
Linking a plan in Chargebee to a billing meter
When creating a new billing meter, in the Link To section you can specify Chargebee as the Billing Provider and then select the Item Plan and Item Price you’d like the usage to be linked to.
When creating a billing meter linked to Chargebee, you can select the reporting period. You can select “hourly” for testing purposes. However for production, we recommend daily reporting as Chargebee has a limit of 50k usage records for the lifetime of each subscription.