Managing charges and discounts in Chargebee

(Comments)

To add more info to the previous blog post on chargebee, let us look at how to override the charges that have already been set in a plan. It is often a case that we want to provide incentives (discounts, trials etc.) to our customers to singup to our service.


Chargebee lets us override the charges (setup charges, monthly charges, trail period etc.) applied to a subscription. This can be achieved while creating a checkout page. If you remember, our create_checkout() method in the module chargebee_utils.py looks like this.

import chargebee
from chargebee import InvalidRequestError


def create_checkout(customer, plan_id):
    # plan_data is where you override the charges
    plan_data = {
            'plan_id': plan_id
        }
    checkout_data = ({
        'subscription': plan_data,
        'customer': {
            'id': customer.id
        }
    })

    try:
        checkout = chargebee.HostedPage.checkout_new(checkout_data)
        if checkout.hosted_page.state == 'created':
            return checkout.hosted_page
    except InvalidRequestError:
        return None

Overriding the prices is as simple as making changes to the dictionary plan_data. Change it to

plan_data = {
    'plan_id': plan_id,
    'setup_fee': 29900, # Change the value to what you need
    'plan_unit_price': 19900
}

Note that prices are all specified in cents, not dollars. That was simple. But how do we add a trial period? Again it is same as specifying other charges, except that we need to specify the time of expiry of trial as unix timestamp in seconds. So, we convert the date x days after today to unix timestamp in seconds before setting trial_end.

from datetime import datetime, timedelta
from django.utils.dateformat import format

trial_days = 30 # Number of trial days
trial_end = int(format(datetime.today() + timedelta(trial_days), 'U'))

Now we have the trial_end as unix timetamp in seconds. We just have to specify this one in the plan_data.

plan_data = {
    'plan_id': plan_id,
    # Other parameters,
    'trial_end': trial_end
}

To make trail end on date change, rather than precisely after x days including hours, minutes and seconds, we can calculate trial_end as trial_end = int(format(datetime.today().date() + timedelta(trial_days), 'U')).


Providing discounts with coupons


Chargebee also provides us with coupon facility which can be applied to the subscription, either at hosted page creation or directly by user at the time of checkout. A coupon can be used to provide users with special discounts in percentages or flat discounts. Chargebee give a firm control on how coupons operate. See the coupons documentation for all the available options.


We can create a coupon from Product Catalog -> Coupons -> CREATE A NEW COUPON. Fill in the details and save.
Create counpon
Fill in coupon details

We also need to enable discount code field for the plan's hosted page. Select the plan page from Product Catalog -> Plans. In the Hosted Page section click on Customize Hosted Page (you can visit the hosted page by following view page link).


Edit checkout page
Enable Discount Code field in the Field Configurations section on the left.
Enable discount code field

Save and visit the hosted page. We will see the 'Discount Code' field in the payment page.
alt text
Verify that the coupon code is working by filling the field and clicking on apply. You should see that the coupon is applied.
Coupon applied

We can distribute the coupon code to our users so that they can avail the offer. We can even control the plans the code is applicable to, number of times the coupon can be used etc. Chargebee gives us full control.

Comments

Recent Posts

Archive

2022
2021
2020
2019
2018
2017
2016
2015
2014

Tags

Authors

Feeds

RSS / Atom