In Chargebee you can have additional 'custom' fields associated with the customer. They are very useful for tracking additional information about a customer or a subscription. Once the custom fields are created, they can be accessed through the admin console and using the API.
Click here to learn more about custom fields in Chargebee.
In this tutorial we will be explaining about accessing and updating custom fields using the API.
'Honey Comics', our demo application, is a fictitious online comic book store providing a subscription service for comics. We send comic books every week to subscribers. In order to let customers try the service, we provide them with a free trial period for two weeks.
In addition to the basic account information such as email, phone number we need the genre (fiction, action, adventure, etc.) that the user is interested in. We also need to get their date of birth as we want to send a surprise package of comics as a birthday gift.
To try out the tutorial yourself, you'll need the following:
The Honey Comics signup form is a simple form requiring only basic account information. We also get the two additional information (genre and date of birth).
Here's the code for the genre field in signup form :
<label for="city">Comics Genre</label> <select class="form-control" name="customer[cf_comics_type]"> <option> Adventures </option> <option> Action </option> <option> Fantasy </option> <option> Horror </option> <option> Romance </option> </select>
Now lets switch to the server side implementation
We first have to download and import the client library of our choice. Then we need to configure the client library with our test site and our test api key.
On the server side we get the signup details, do the necessary validations (which is skipped in demo) and then call the Create Subscription API to create the subscription.
Note: The custom fields are passed under customer resource.
Here's how we validate user inputs and handle API call errors in this demo:
Client Side Validation: We use the jQuery form validation plugin to check whether the user's input in fields(email, zip code and phone number) is valid or not.
Server Side Validation: As this is a demo app we have skipped the server side validation of all input parameters. But we recommend you to perform the validation at your end.
General API Errors: Chargebee might return error responses due to various reasons such as invalid configuration. To identify specific reasons for all error responses you can check the API documentation and also take a look at the error handler file to check how these errors can be handled.