Skip to content

Integrate to Chargebee Metered Billing #7970

Description

@emyller

Spiked from #7923

Currently overage billing happens in charge_for_api_call_count_overages, and uses OrganisationSubscriptionInformationCache to push overage charges to Chargebee as 100K addons (set up in Chargebee). The goal of this issue is to integrate with Chargebee's proper tool for this: Metered Billing, and have Chargebee handle the overage billing logic natively.

The scope is limited to adding an integration that co-exists with the current overage logic, while we transition to Metered Billing using a FoF feature flag chargebee_metered_billing. Responsibility is further cleaned up after GA (see #7971).

Note

It seems that Chargebee is moving towards Usage-Based Billing and may sunset Metered Billing in the future. Regardless, we decided to move forward with Metered Billing because:

  • As of today, UBB is not yet generally available in Chargebee production.
  • We encountered issues while experimenting with UBB, and had our account set to Metered Billing automatically.
  • Integration footprint is lightweight.

Acceptance criteria

  • Live site in Chargebee has Metered Billing enabled and a single metered add-on with a per-unit price. See Chargebee setup below.
  • Usage data is pushed to Chargebee via Usage.create (Metered Billing) every hour:
    • Only for customers having chargebee_metered_billing enabled.
    • Attach the add-on to the subscription (update_for_items) idempotently so Chargebee tracks overages.
    • Pull data from InfluxDB data (kept as usage storage) via get_total_events_count.
    • Push data points with dedupe key {subscription_id}:{period_start} on a replace policy, i.e. each data point is the accumulated overage for the subscription and period, in the add-on's unit of 1000 calls: round((get_total_events_count() − allowed_30d_api_calls) / 1000) (allowance is already sourced from Chargebee).
  • Overages are calculated and charged by Chargebee — the task charge_for_api_call_count_overages (current overage logic) ignores subscriptions with chargebee_metered_billing enabled.
  • Turning chargebee_metered_billing on for a customer mid-term means the next invoice uses Metered Billing data.

Chargebee setup

Click to expand
# Prereqs: item family `flagsmith` exists; Metered Billing enabled on the site.
# env: CB_SITE, CB_API_KEY

# 1. Metered overage add-on (unit = 1000 calls)
curl -s "https://$CB_SITE.chargebee.com/api/v2/items" -u "$CB_API_KEY:" \
  -d id=api-calls-overage \
  -d name="API Calls Overage" \
  -d type=addon \
  -d item_family_id=flagsmith \
  -d metered=true \
  -d usage_calculation=sum_of_usages \
  -d unit="1000 calls"

# 2. Per-unit price: 5c per 1000 calls (= $50 / 1M). Repeat per currency sold.
curl -s "https://$CB_SITE.chargebee.com/api/v2/item_prices" -u "$CB_API_KEY:" \
  -d id=api-calls-overage-USD \
  -d name="API Calls Overage (USD)" \
  -d item_id=api-calls-overage \
  -d currency_code=USD \
  -d pricing_model=per_unit \
  -d price=5 \
  -d period=1 \
  -d period_unit=month

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions