> ## Documentation Index
> Fetch the complete documentation index at: https://docs.integrateledger.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Product lines: plan groups

> Sell several products to one customer, each with its own tiers, on one Stripe invoice. A customer holds one base plan per group and upgrades within it.

> **Starter for the editor, and Campaigns Pro for email, on one invoice.**

Sell each product line with its own ladder of plans. A plan's `group` names its product line: a customer holds one base plan in each group, so they can be on Starter for your editor and on Campaigns Pro at the same time, and move up each ladder on its own. Every paid plan the customer holds rides on one Stripe bill, so they get one invoice.

## The catalog

Plans without a `group` are in the `default` group. Give each other product line a group of its own:

```ts ledger.config.ts theme={"system"}
import { defineCatalog, feature, plan } from "integrateledger-cli";

export default defineCatalog({
	features: [
		feature({ id: "documents", name: "Documents", type: "metered" }),
		feature({ id: "emails", name: "Emails", type: "metered" }),
	],
	plans: [
		plan({
			id: "starter",
			name: "Starter",
			monthlyCredits: 0,
			priceCents: 1900,
			items: [{ featureId: "documents", included: 100 }],
		}),
		plan({
			id: "pro",
			name: "Pro",
			monthlyCredits: 0,
			priceCents: 4900,
			items: [{ featureId: "documents", unlimited: true }],
		}),
		plan({
			id: "campaigns_starter",
			name: "Campaigns Starter",
			monthlyCredits: 0,
			priceCents: 1900,
			group: "campaigns",
			items: [{ featureId: "emails", included: 5000 }],
		}),
		plan({
			id: "campaigns_pro",
			name: "Campaigns Pro",
			monthlyCredits: 0,
			priceCents: 4900,
			group: "campaigns",
			items: [{ featureId: "emails", included: 50000 }],
		}),
	],
});
```

```json ledger.catalog.json theme={"system"}
{
  "schemaVersion": 1,
  "features": [
    { "id": "documents", "name": "Documents", "type": "metered" },
    { "id": "emails", "name": "Emails", "type": "metered" }
  ],
  "plans": [
    { "id": "starter", "name": "Starter", "monthlyCredits": 0, "priceCents": 1900,
      "items": [{ "featureId": "documents", "included": 100 }] },
    { "id": "pro", "name": "Pro", "monthlyCredits": 0, "priceCents": 4900,
      "items": [{ "featureId": "documents", "unlimited": true }] },
    { "id": "campaigns_starter", "name": "Campaigns Starter", "monthlyCredits": 0,
      "priceCents": 1900, "group": "campaigns",
      "items": [{ "featureId": "emails", "included": 5000 }] },
    { "id": "campaigns_pro", "name": "Campaigns Pro", "monthlyCredits": 0,
      "priceCents": 4900, "group": "campaigns",
      "items": [{ "featureId": "emails", "included": 50000 }] }
  ]
}
```

| Field | What it sets |
| - | - |
| `group` | The plan's product line: 1 to 60 lowercase letters, digits, `_` or `-`, starting with a letter or digit. `default` when omitted. Changing it publishes a new plan version |

`GET /v1/plans` lists each plan with its `group`. Starter and Campaigns Starter can be held together; Starter and Pro are two rungs of one ladder.

## Attach a second product line

Attach the first plan as usual. To add a paid plan in another group, attach it with an `Idempotency-Key`, as for an [add-on](https://docs.integrateledger.com/pricing/add-ons.md#attach-it): it joins the customer's Stripe bill of the same interval, prorated for the rest of the period.

```typescript TypeScript theme={"system"}
const change = await billing.attach(
	{ customerId, planId: "campaigns_starter" },
	{ headers: { "Idempotency-Key": `campaigns_${purchase.id}` } }, // saved when the customer confirmed
);
if ("itemChangeId" in change && change.paymentUrl) {
	return res.redirect(303, change.paymentUrl);
}
```

```python Python theme={"system"}
change = billing.attach(
    customer_id=customer_id,
    plan_id="campaigns_starter",
    idempotency_key=f"campaigns_{purchase.id}",  # saved when the customer confirmed
)
if "itemChangeId" in change and change.get("paymentUrl"):
    return redirect(change["paymentUrl"], code=303)
```

```bash cURL theme={"system"}
curl https://api.integrateledger.com/v1/billing/attach \
  -H "Authorization: Bearer $LEDGER_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: campaigns_pc_311" \
  -d '{"customerId": "user_123", "planId": "campaigns_starter"}'
```

```json Response theme={"system"}
{
  "itemChangeId": 52,
  "status": "applied",
  "paymentUrl": null,
  "item": { "planId": "campaigns_starter", "quantity": 1 }
}
```

`status` reads as for an add-on: `applied` once the prorated invoice is paid, `pending_payment` with a `paymentUrl` when the card needs the customer, `processing` while Stripe settles, and `closed` when it was never paid.

## How it bills

* **One invoice.** A paid plan in another group is an item on the customer's existing Stripe subscription of its interval. From the next renewal, one invoice carries every product line.
* **Prorated now.** Attaching charges the plan's price for the unused share of the period. Its credits and allowances are prorated the same way, rounded down, then granted in full at each renewal.
* **Free plans stay free.** A free plan in another group runs in Ledger and renews on its own period. Paying for a plan in one group ends free plans in that group only, so a customer on the free editor plan keeps it while they buy Campaigns Pro.
* **First paid plan.** When the customer holds only free plans, a paid plan in any group goes through [Checkout or the saved card](https://docs.integrateledger.com/billing/checkout-and-attach.md), as a first paid plan does.

## How it counts in check and track

`check`, `track` and reservations combine every plan the customer holds, in every group, with their add-ons:

* **Allowances add up.** Included units of the same feature across plans add together.
* **Unlimited wins.** An unlimited item on any plan makes the feature unlimited.
* **Booleans.** A feature is on when any plan includes it.
* **Overage.** A metered feature's overage is priced by the newest plan the customer holds that prices it, in the order the plans were attached. Units already recorded keep the rate they were recorded at. Credit overage is priced by the current plan.

`currentPlan` in [GET /v1/customers](https://docs.integrateledger.com/api-reference/customers/get.md) is the `default` group's plan, and its bill sets the billing period, balance alerts and entity allowances.

## Upgrade within a group

Each product line moves on its own. [Upgrade](https://docs.integrateledger.com/api-reference/billing/upgrade.md) to `campaigns_pro` and Ledger replaces the customer's plan in the `campaigns` group and leaves Starter as it is. A plan riding on another product line's bill moves to the new Stripe Price in one prorated change, with the period kept, and the answer has the `itemChangeId` and `status` of an attach. [Upgrades and downgrades](https://docs.integrateledger.com/billing/upgrades-and-downgrades.md#within-a-group) covers both directions.

## Rules

* **One base plan per group.** Attaching a plan into a group the customer already holds returns `409 subscription_exists`: upgrade or downgrade within the group instead.
* **Upgrades and downgrades stay in a group.** A target in a group the customer holds nothing in returns `400 plan_not_in_group`: attach it instead.
* **One bill interval.** Paid product lines share the bill of one interval. A paid plan at another interval than the customer's paid bill returns `409 plan_interval_differs`.
* **Downgrades of a second product line.** A plan riding on another product line's bill moves up by upgrade (`409 downgrade_unavailable_for_item`).
* **An `Idempotency-Key`** goes with a paid plan joining an existing bill (`400 idempotency_key_required`).
* **Add-ons have no group** (`add_on_has_no_group`): an add-on joins the bill of its interval beside every product line.

## Related

* [Plans per workspace or user](https://docs.integrateledger.com/pricing/entity-plans.md): a plan on each entity, on the same invoice.
* [Recurring add-ons](https://docs.integrateledger.com/pricing/add-ons.md): extras beside a base plan.
* [Plans and items](https://docs.integrateledger.com/concepts/plans-and-items.md): every plan field.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.