I’ve been looking around at different subscription management and billing solutions, and Chargebee appears to have a very powerful platform. While Stripe is nice and simple, it appears to lack many features required for B2B SaaS sales, multi-year upfront billing being a great example. One of the other really nice things about Chargebee is the ability to provide customers with a billing portal, which can simply be enabled through REST, and by providing someone a link that opens in a new page, or embedding it in an iFrame for a pretty seamless experience.
After a few days of playing around with the Chargebee API and with some help from @dorilama I’m happy to publish this for the community to use. It may seem like a lot of steps, but it is really not difficult at all!
How to Integrate Chargebee with Weweb
Initial Setup
-
Create an account on Chargebee.com. They have a nice and simple Test environment that you can work with, absolutely free. It even takes sample payments so it makes it easy to test without really signing up for any other services.
-
Create an API in Chargebee, you will need a full access key.
-
Under Settings → Checkout & Self Service Portal, Make sure to enable the Single Sign on API
- There should be some sample customers and plans in Chargebee, but if there aren’t for some reason create a customer, you will need to use the CustomerID as part of this.
Backend Setup
The Chargebee REST API can be found here, it’s the reference to where you make every API call into their platform, very powerful. I’m not going to detail things out here too much, but will give you a general idea of what you need to do.
-
When you create a new account for someone when they sign-up, you will want to call the Chargebee API and create a new account in Chargebee for the customer account.
-
The API will return the Customer ID, you will want to store this in your database for reference later on.
-
If you already have customers, you will need to somehow create accounts in Chargebee, either manually, or programmatically, and associate the Customer ID with your customer accounts.
API Key
-
First, you’ll want to download Insomnia API client. The reason for this is that they encode the Chargebee API key, that is the API key you will need to feed into Weweb for Authentication. I lost a lot of time figuring this out, I don’t know what encoding is done here, just that it works. This may be some bug in WeWeb, I’m not really sure.
-
In Insomnia, setup Basic Authentication, and then put your API key in there.
- On the Top Right go to Send, Click the Dropdown and go to ‘Generate Client Code’
You will want to copy this encoded text and use it for your Authentication Key
- Now that you have the encoded API key, it’s time to get to work in Weweb
WeWeb Setup to Initiate the Portal
-
To make a request to Chargebee from WeWeb, you will need to have the REST API Plugin Installed.
-
I setup a Workflow that does three things.
- Creates the Portal Session
- Stores the Returned Portal URL
- Stores the Session ID (You will need to use this to log users out of portal on WeWeb Logout)
- Create the Portal Session
API URL: https://yourdomain.chargebee.com/api/v2/portal_sessions
The customer[id] is the customer ID in Chargebee, ideally this was created when you created a new customer in your application. I’m just fetching this from my database as it’s associated to the logged in customer. The Authorization key is the encoded key you retrieved from Insomnia. Make sure to have basic in front of the key to enable Basic AUTH required by Chargebee API.
- Store the URL Returned from Creating the Portal
- Store the Session ID
-
At this point you are effectively done. Now every time you need someone to access your Chargebee portal, you need to run this workflow (You can’t reuse a generated URL twice). Whenever you want someone to access the portal you simply run the workflow, capture the stored URL and send the user to that link. For really clean integration, I setup a Modal that pops up, has an embedded iFrame and uses that URL, no need to send them to another page/tab - it’s completely embedded in the application.
-
For cleanup make sure to call the logout API and pass it the most recently retrieved session generated from your workflow.
That’s it, your done. Now if you want more advanced integration, such as fetching user subscription information, enforcing licensing, etc, you just call Chargebee API and reference subscription information for that customer account.