Introducing the Chargebee CLI

Create a purchase

Open in Postman

Creates a purchase resource. A purchase can contain one or more of the following:

  • subscriptions (a subscription resource consists of item prices such that at least one of the item prices belongs to an item of type plan.)
  • group of one-time charges (aka charge item prices)

When you call this API, the invoices for the subscription(s) and one-time charge(s) are created immediately and not left unbilled . The exception is a subscription group whose subscription starts in trial, which is possible when trial support for purchases is enabled for the site. No charges are raised for such a group until the trial ends. When no group in the purchase raises charges, no invoice is created and purchase.invoice_ids is empty.

Note

Providing shipping_addresses[] is required when the Orders feature has been enabled.

Specifying purchase_item groups

When creating a purchase, you must specify the group or index to which each item price belongs. You can do this by setting the purchase_items[index] for each item price. Item prices with the same purchase_items[index] belong to the same group. The grouping of item prices allows you to specify the discounts[] applicable for each group and indicate which item prices should be added to any subscriptions you want to create. Groups can be one of two types:

  • Subscription groups
  • One-time charge groups

The following subsections describe the types of groups in detail.

Note

You can specify up to 10 groups,

  • with a recommended subscription group of 5. To increase this limit to a maximum of 8, contact eap@chargebee.com.
  • with a maximum of 10 one-time charge groups by default.

The total limit for group items for a single purchase is 60.

Subscription groups

To create a subscription, specify a subscription group. A subscription group is a group of item prices that contains exactly one item price of type plan. Multi-plan subscriptions are not supported: a group cannot contain more than one plan item price. To create multiple subscriptions, provide multiple subscription groups. Plans in different groups can belong to different item families. When item families are enabled, item prices in one group must belong to the same item family as that group's plan.

Note

A subscription group can have up to 20 non-plan item prices. To increase this limit to a maximum of 60, contact eap@chargebee.com.

Custom Fields

Purchase API supports custom fields of Subscriptions, use the following format to specify custom fields in Purchase API: subscription_info[custom_field].

One-time charge groups

A one-time charge group is a group of charge item prices (i.e. item prices belonging to items of type charge). Charge item prices can be added to subscription groups as well. The charges within and across each one-time group must be unique.

Note

  • A one-time charge group can have up to 20 item prices. To increase this limit to a maximum of 60, contact eap@chargebee.com.
  • A charge item price can only be added to a single one-time charge group. However, it can be part of multiple subscription groups.

Applying discounts

Discounts, both manual discounts and coupons, can be applied to groups by specifying the discounts[] array. You cannot target one item price in a group. A manual discount applies to the invoice for the group. A coupon with apply_on set to invoice_amount applies to the invoice sub_total. A coupon configured for specific item prices applies only to those item prices.

The following table describes the method of application based on whether discounts[index][i] is provided:

discounts[index][i] is provideddiscounts[index][i] is not provided
Coupons
  • The coupon is applied exclusively to the invoice for group i.
  • The coupon is applied exclusively to the invoice created immediately upon invoking this API.
  • If group i is a subscription group, then the coupon is applied to invoices for subscription renewals based on coupon attributes such as duration_type and max_redemptions.
  • The coupon must have apply_on set to invoice_amount. A coupon configured for specific item prices is rejected unless you pass discounts[index][i].
  • The coupon is applied to the first invoice only, irrespective of coupon.duration_type or coupon.max_redemptions.
  • The coupon is not applied to later invoices.
Manual discounts
  • The manual discount is applied exclusively to the invoice for group i.
  • The manual discount is applied exclusively to the invoice created immediately upon invoking this API.
  • The manual discount is not applied to subsequent invoices, such as those generated upon subscription renewal.
  • The manual discount is applied only to the first invoice for the purchase.
  • The manual discount is not applied to later invoices.

Note

For a subscription group whose subscription is in trial, no invoice is created for the group when this API is invoked. A discount with discounts[index][i] set to that group is kept on the subscription and applied when the subscription is first invoiced. A discount without discounts[index][i] applies only to the first invoice, so it cannot be passed when the purchase creates no invoice.

Limitations

Item prices in a subscription group are validated the same way as in Create a subscription. This operation also has the following limitations.

Item prices

  • A subscription group contains exactly one item price of type plan. You cannot create one subscription with more than one plan item price. Multi-plan subscriptions are not supported.
  • Plans in different subscription groups can belong to different item families. When item families are enabled, item prices in one group must belong to the same item family as that group's plan.
  • You cannot set a service period for a charge. This operation has no equivalent of subscription_items[service_period_days].

Subscriptions

  • You cannot set free_period.
  • You cannot set start_date. Each subscription starts immediately. A group can start in trial only when trial support for purchases is enabled and you set subscription_info[trial_end].
  • Calendar billing settings for the site apply. You cannot override them with billing_alignment_mode.
  • Each subscription uses the customer's auto_collection. You cannot override it for the subscription.
  • Only the current term is charged. You cannot set terms_to_charge.
  • You cannot create ramps or schedule a subscription change in this request. After the subscriptions are created, use Create a subscription ramp or Update a subscription.
  • The purchase resource cannot be changed after it is created. Update each subscription with the subscription operations.

Multiple business entities

This operation does not accept business_entity_id. Calling it on a site where multiple business entities are enabled returns an error.

Invoices

  • When the purchase raises invoiceable charges and consolidated invoicing is enabled for the customer, Chargebee consolidates those charges. Subscription differences can still produce more than one invoice. When consolidated invoicing is disabled, each subscription gets its own invoice. A trial-only purchase, where every subscription group starts in trial and no one-time charges are raised, creates no invoice and purchase.invoice_ids is empty.
  • Invoices for subscriptions and one-time charges are created immediately and are not left unbilled. A subscription group that starts in trial is the exception: no charges are raised for that group until the trial ends.
  • This operation does not accept a net-term parameter. Net terms already set on the customer still apply. When those terms are greater than zero, the invoice is posted and payment collection waits until the due date. When the terms are zero and the customer's auto_collection is on, Chargebee attempts to collect payment while closing the invoice. The operation fails if the customer has no valid payment method, or if collection fails. When auto_collection is off, the invoice is closed with payment due.

Sample Request

Open in Postman

Sample Result[JSON]

URL Format

POST https://[site].chargebee.com/api/v2/purchases

Input Parameters

customer_id
required, string, max chars=50

The unique identifier of the customer that made this purchase.

payment_source_id
optional, string, max chars=40

Payment source attached to this purchase. If present, the customer's payment sources won't be used to collect any payment for this purchase.

replace_primary_payment_source
optional, boolean, default=true

Indicates whether the primary payment source is replaced with this payment source. If a payment_intent object is included in the request, replace_primary defaults to true. For all other cases, the default is false .

invoice_info
Parameters for invoice_info
pass parameters as invoice_info[<param name>]
payment_schedule
Parameters for payment_schedule
pass parameters as payment_schedule[<param name>]
statement_descriptor
Parameters for statement_descriptor
pass parameters as statement_descriptor[<param name>]
payment_intent
Parameters for payment_intent
pass parameters as payment_intent[<param name>]
purchase_items[0..n]
Parameters for purchase_items. Multiple purchase_items can be passed by specifying unique indices.
pass parameters as purchase_items[<param name>][<idx:0..n>]
item_tiers[0..n]
Parameters for item_tiers. Multiple item_tiers can be passed by specifying unique indices.
pass parameters as item_tiers[<param name>][<idx:0..n>]
shipping_addresses[0..n]
Parameters for shipping_addresses. Multiple shipping_addresses can be passed by specifying unique indices.
pass parameters as shipping_addresses[<param name>][<idx:0..n>]
discounts[0..n]
Parameters for discounts. Multiple discounts can be passed by specifying unique indices.
pass parameters as discounts[<param name>][<idx:0..n>]
subscription_info[0..n]
Parameters for subscription_info. Multiple subscription_info can be passed by specifying unique indices.
pass parameters as subscription_info[<param name>][<idx:0..n>]
contract_terms[0..n]
Parameters for contract_terms. Multiple contract_terms can be passed by specifying unique indices.
pass parameters as contract_terms[<param name>][<idx:0..n>]

Returns

Purchase object

Resource object representing purchase