Billing SDK

Subscription lifecycle

Recipes for renewals, trials, upgrades, failed charges, refunds, chargebacks, cancellations and pauses with the Faurya Billing SDK.

Subscription lifecycle

Recipes for everything that happens to a subscription after signup. Each one continues the example from Subscriptions and invoices: customer cus_42 on sub_900, Pro monthly at $49.

import { createFauryaBilling } from "@faurya/billing";

const { billing } = createFauryaBilling();

The rule of thumb: anything you bill is an invoice, any money that moves is a transaction, and any decision an invoice can't express — cancelling, pausing, a change that starts later — is a subscription event.

Renew a subscription

A renewal is just the next invoice for the same subscription, plan and price, covering the next period. Faurya recognises it as a renewal on its own.

await billing.invoices.upsert("inv_1100", {
  customer_external_id: "cus_42",
  currency: "USD",
  invoiced_at: "2026-04-01T09:00:00Z",
  line_items: [
    {
      external_id: "line_1",
      type: "subscription",
      amount: 49,
      subscription_external_id: "sub_900",
      plan_external_id: "plan_pro_monthly",
      service_period_start: "2026-04-01T00:00:00Z",
      service_period_end: "2026-05-01T00:00:00Z",
    },
  ],
});

await billing.transactions.upsert("txn_7801", {
  invoice_external_id: "inv_1100",
  type: "payment",
  amount: 49,
});

Start with a free trial

Bill the trial as a trial line with amount 0. A trial counts as service having started, so the subscription is counted right away.

await billing.invoices.upsert("inv_trial_42", {
  customer_external_id: "cus_42",
  currency: "USD",
  invoiced_at: "2026-03-01T09:00:00Z",
  line_items: [
    {
      external_id: "trial",
      type: "trial",
      amount: 0,
      subscription_external_id: "sub_900",
      plan_external_id: "plan_pro_monthly",
      service_period_start: "2026-03-01T00:00:00Z",
      service_period_end: "2026-03-15T00:00:00Z",
    },
  ],
});

When the trial converts, send the first paid invoice for the same sub_900, with its service period starting where the trial ended.

Upgrade, downgrade or change seats

Bill the new terms on the same subscription id. Faurya compares each period with the one before it:

  • a different plan shows as a plan change;
  • the same plan at a different price or quantity shows as an update (expansion or contraction).
// Ada moves to Business from her next billing period
await billing.invoices.upsert("inv_1200", {
  customer_external_id: "cus_42",
  currency: "USD",
  invoiced_at: "2026-05-01T09:00:00Z",
  line_items: [
    {
      external_id: "line_1",
      type: "subscription",
      amount: 99,
      subscription_external_id: "sub_900",
      plan_external_id: "plan_business_monthly",
      service_period_start: "2026-05-01T00:00:00Z",
      service_period_end: "2026-06-01T00:00:00Z",
    },
  ],
});

Billing a prorated mid-cycle change? Mark the line with prorated: true (and optionally a proration_type). See invoice line fields.

If you bill the change later than it happens (for example, "your plan changes at the next renewal"), record it as a scheduled update instead:

await billing.subscriptionEvents.record("evt_downgrade_42", {
  event_type: "subscription_update_scheduled",
  customer_external_id: "cus_42",
  subscription_external_id: "sub_900",
  event_date: "2026-04-20T10:00:00Z",      // when Ada asked
  effective_date: "2026-05-01T00:00:00Z",  // when it takes hold
  plan_external_id: "plan_starter_monthly",
  currency: "USD",
  amount: 19,                              // the new recurring amount per cycle
});

A charge fails

Say the May invoice for the Business plan (inv_1200) fails to charge. Record the failure. The invoice stays unpaid, nothing counts as revenue, and an already-running subscription stays active while you retry (dunning).

await billing.transactions.upsert("txn_7900_attempt1", {
  invoice_external_id: "inv_1200",
  type: "payment",
  amount: 99,
  result: "failed",
});

// The retry succeeds a few days later — new id, new attempt
await billing.transactions.upsert("txn_7900_attempt2", {
  invoice_external_id: "inv_1200",
  type: "payment",
  amount: 99,
  result: "succeeded",
});

If the retries never succeed and you give up collecting it, write the invoice off instead:

await billing.invoices.setStatus("inv_1200", "written_off");

Refund a payment

A refund is its own transaction that names the payment it gives back. Amounts are positive — the type says it's money going out.

await billing.transactions.upsert("ref_301", {
  invoice_external_id: "inv_1042",
  type: "refund",
  amount: 20,                                  // partial refund of the $49 payment
  original_transaction_external_id: "txn_7781",
});

A refund for less than the payment is marked is_partial_refund: true. A refund can never exceed the payment it refers to.

Chargebacks and disputes

Same shape as a refund:

// The bank pulled the money back
await billing.transactions.upsert("cb_12", {
  invoice_external_id: "inv_1100",
  type: "chargeback",
  amount: 49,
  original_transaction_external_id: "txn_7801",
});

// You won the dispute
await billing.transactions.upsert("cb_12_won", {
  invoice_external_id: "inv_1100",
  type: "chargeback_reversal",
  amount: 49,
  original_transaction_external_id: "txn_7801",
});

Cancel a subscription

Most apps cancel at the end of the paid period. Record it as scheduled — the subscription stays active (and counted) until the effective date:

await billing.subscriptionEvents.record("evt_cancel_42", {
  event_type: "subscription_cancellation_scheduled",
  customer_external_id: "cus_42",
  subscription_external_id: "sub_900",
  event_date: "2026-04-18T16:30:00Z",      // when Ada clicked "cancel"
  effective_date: "2026-05-01T00:00:00Z",  // when access ends
});

To end it right now, use subscription_cancelled with the same date for both:

await billing.subscriptionEvents.record("evt_cancel_now_42", {
  event_type: "subscription_cancelled",
  customer_external_id: "cus_42",
  subscription_external_id: "sub_900",
  event_date: "2026-04-18T16:30:00Z",
  effective_date: "2026-04-18T16:30:00Z",
});

The customer changes their mind

Undo a scheduled cancellation with a retraction. You never edit or delete an event — you withdraw it with a new one.

await billing.subscriptionEvents.record("evt_uncancel_42", {
  event_type: "scheduled_subscription_cancellation_retracted",
  customer_external_id: "cus_42",
  subscription_external_id: "sub_900",
  event_date: "2026-04-25T09:00:00Z",
  effective_date: "2026-04-25T09:00:00Z",
});

To withdraw one specific event of any type, use subscription_event_retracted with retracted_event_id: "evt_...".

Pause and resume

A paused subscription is still a subscriber.

await billing.subscriptionEvents.record("evt_pause_42", {
  event_type: "subscription_paused",
  customer_external_id: "cus_42",
  subscription_external_id: "sub_900",
  event_date: "2026-06-01T00:00:00Z",
  effective_date: "2026-06-01T00:00:00Z",
});

await billing.subscriptionEvents.record("evt_resume_42", {
  event_type: "subscription_resumed",
  customer_external_id: "cus_42",
  subscription_external_id: "sub_900",
  event_date: "2026-07-01T00:00:00Z",
  effective_date: "2026-07-01T00:00:00Z",
});

Two dates on every event

Every subscription event has:

  • event_date — when the decision was made. Can't be in the future.
  • effective_date — when it takes effect. Can be in the future.

Reports use effective_date to decide what was in force on a given day, so a cancellation scheduled for next month doesn't reduce today's MRR.

All event types

event_typeUse it when
subscription_startA subscription begins (also proves it started, even before payment)
subscription_start_scheduledA subscription will begin on a future date
subscription_updatedPrice, quantity or plan changes now
subscription_update_scheduledPrice, quantity or plan changes later
subscription_cancelledService ends now
subscription_cancellation_scheduledService will end on a future date
subscription_pausedService is paused
subscription_resumedService resumes
scheduled_subscription_start_retractedWithdraw the latest scheduled start
scheduled_subscription_update_retractedWithdraw the latest scheduled update
scheduled_subscription_cancellation_retractedWithdraw the latest scheduled cancellation
subscription_event_retractedWithdraw one specific event (retracted_event_id)

The four start/update types also need plan_external_id, currency and amount (the recurring amount per billing cycle). TypeScript tells you which fields each type needs.

Events can't be changed once recorded. Re-sending the same id with the same body is fine (nothing happens); a different body throws a FauryaConflictError with code EVENT_IMMUTABLE. Use a retraction instead.

Fix a mistake

MistakeFix
Wrong line on an unpaid invoiceinvoices.upsert the same id again, in full
Wrong line on a paid invoiceinvoices.setStatus(id, "voided"), then send a corrected invoice with a new id
Wrong payment amountRefund it, then record the right payment
Wrong subscription eventRetract it, then record the right one