Skip to main content
attach is the one call that starts a plan or sells a credit pack, so your upgrade button is a single line of code. A free plan starts at once. Anything paid goes through Stripe: Checkout for a new card, or an instant charge on the card the customer already saved. Ledger grants a paid purchase the moment Stripe’s signed webhook confirms the money arrived, so customers get exactly what they paid for.

What attach does

A saved card is the default payment method on the customer’s Stripe customer. When Ledger charges it, the response comes after Stripe has confirmed the invoice, so the plan’s grants are already there. A declined card leaves nothing behind, so the customer can attach again. A card that needs 3-D Secure gets a checkoutUrl for the same subscription, and the customer authenticates on Checkout. A customer holds one base plan per group, and moves between plans in a group without a gap:
  • On a free plan, attach the paid plan. The free plan keeps working until the paid plan’s payment lands, then it ends; free plans in other groups keep running. A failed or abandoned Checkout leaves the free plan as it was.
  • On a paid plan (anything with a Stripe subscription, pay as you go included), move them with an upgrade or downgrade.
  • On a lifetime plan, see below.
  • For another product line, attach a plan in its group beside the first. A paid one joins the customer’s bill.
  • For a workspace or user inside the customer, attach with entityId. See Plans per workspace or user.

Set where Checkout returns

In Settings › Checkout, set a success URL and a cancel URL: the pages in your app that Stripe sends the customer back to. Until you set them, Sandbox returns the customer to the Ledger dashboard, so you can try Checkout from day one. To send the customer back to the page they came from, pass successUrl (and cancelUrl, if it differs) to attach. They replace the saved URLs for that Checkout; one sent alone serves for both. Sandbox also takes http://localhost while you build. Stripe replaces {CHECKOUT_SESSION_ID} in them with the session’s ID.

Attach and redirect

Resolve the customerId on your server from the signed-in user, never from a value the browser sends.
Response
Calling attach again for the same plan or pack while its Checkout is open returns the same checkoutUrl, so a double click is safe. Attaching a different pack expires the unpaid pack Checkout and opens a new one, so the customer always pays for the pack they chose last. Stripe expires an unpaid Checkout after 24 hours; the next attach opens a new one.

Attach with a promotion code

Send one of your promotion codes as promotionCode: Checkout opens with the discount on, or a saved card is charged the discounted price. To let the customer type a code in Checkout instead, send allowPromotionCodes: true.
The plan still grants its full credits and allowances, and an abandoned Checkout gives the code’s use back.

Attach a specific version

Attach starts the plan’s active version. Pass planVersion, a number or a label, to start another one: a legacy version for a customer promised an older price, in Sandbox and Live, or the plan’s draft in Sandbox, to try it before you promote it.
The customer pays that version’s Stripe Price and gets its grants, at every renewal.

When grants land

A plan’s grants are its credits, its metered allowances for the period, and its held limits and boolean features, which apply from the same moment. Ledger checks the plan’s line on the invoice at your full price before tax and discounts, so tax and coupons never shrink a grant. The redirect to your success URL happens at about the same time as the webhook, but it proves nothing: anyone can open that URL. When the customer lands there, read them again from your backend:
  • While listCustomerSubscriptions has an item with status: "pending_checkout", show “Confirming your payment” and reload in a moment. The grants arrive with the payment.
  • Read the subscriptions list, which shows every pending plan. For a customer’s first plan, currentPlan.status is pending_checkout while Checkout is open. A customer moving up from a free plan keeps the free plan as currentPlan until the payment lands, and the pending plan appears in the subscriptions list.

Per-seat plans

A plan with a seat price starts at the seats the customer already holds. Attach reads the customer’s held count and puts it on the first Checkout or invoice beside the plan’s own price.
Team: $49 a month with 3 seats included, then $10 a seat.
A team on your Free plan, which counts seats without pricing them, holds 12 seats and attaches Team:
The first Checkout, or the invoice on a saved card, charges: The 12 seats stay held through the move, so every member keeps working. Later adds and removes are prorated from the moment they’re tracked. A customer who holds no seats at attach pays the plan’s price alone, and their first seat is added to the subscription when it’s tracked.

Lifetime plans

A lifetime plan is paid once. Attach it like any paid plan: with a saved card, Ledger charges it at once on one invoice and returns { "status": "active" }; without one, Checkout takes a single payment at the plan’s one-time Stripe Price. Either way the customer gets a paid invoice, and nothing renews. If the saved card is declined, attach answers 402 payment_failed and Ledger voids the invoice it tried to charge, so nothing is left open to pay later. If the card needs 3-D Secure, Ledger voids that invoice and returns a Checkout URL instead, where the customer authenticates while paying. Attaching again after the customer fixes the card charges one new invoice. A lifetime plan is the customer’s plan for good:
  • To sell more credits on top of it, sell a credit pack.
  • To move the customer onto a recurring plan, attach it with replaceLifetime: true. The lifetime plan keeps working until the new plan is paid for, then ends with its remaining credits, with no refund.

Test it

In Sandbox, pay with Stripe test cards, any future expiry and any CVC: To see renewals without waiting a month, simulate time.

Errors from attach

When something goes wrong

Errors lists every code.

Rules

  • A customer on a paid plan changes plan within its group with an upgrade or downgrade; attach answers 409 subscription_exists.
  • A paid plan in another group, and a paid entity plan, join the customer’s bill of the same interval and need an Idempotency-Key (409 plan_interval_differs, 409 entity_plan_needs_paid_bill).
  • A lifetime plan is bought once (409 plan_already_attached), and only a recurring plan with replaceLifetime: true replaces it (409 lifetime_plan_active).
  • Success and cancel URLs are public https URLs, in Sandbox too; Sandbox also takes http://localhost on an attach.
  • Live needs saved success and cancel URLs, or a successUrl on every attach (400 checkout_urls_not_configured).
  • A customer has one open pack Checkout at a time.
  • Send promotionCode or allowPromotionCodes, not both.
  • A promotionCode covers the plan it’s attached with (422 promotion_code_not_for_plan).
  • A draft attaches in Sandbox; promote it to sell it in Live (409 draft_sandbox_only).
Other ways. Without an SDK, use POST /v1/billing/attach. Build a billing page puts attach behind your plan and pack buttons.