Billing SDK

Subscriptions and invoices

Send customers, plans, invoices and transactions with faurya.billing so Faurya can show MRR, subscribers and churn.

Subscriptions and invoices

To see MRR, subscriber counts, churn and plan changes, Faurya needs to know what you bill and what you collect. With the SDK you send it the same objects your billing system already has:

ObjectWhat it tells FauryaSDK
CustomerWho paysfaurya.billing.customers
PlanHow often a subscription bills (monthly, yearly…)faurya.billing.plans
InvoiceWhat you billed, line by linefaurya.billing.invoices
TransactionWhat money moved: payments, refunds, chargebacksfaurya.billing.transactions
Subscription eventCancellations, pauses and scheduled changesfaurya.billing.subscriptionEvents

There is no "create subscription" call. A subscription appears when an invoice line bills it, and becomes a counted subscriber once the first invoice is paid.

Two ideas that make it easy

1. You choose every id. Every object is saved under your own id — your customer id, your invoice number, your payment id. You never store an id from Faurya.

2. Every call is safe to repeat. upsert means "make Faurya's copy look like this". Sending the same thing twice changes nothing, so you can re-run a sync job, replay a webhook or retry after a crash without creating duplicates.

const res = await faurya.billing.customers.upsert("cus_42", { email: "ada@example.com" });

res.created;      // true the first time, false on every later call
res.data;         // the customer as Faurya stored it
res.warnings;     // anything worth knowing that did not stop the save
res.request_id;   // quote this to support

Your first subscriber, end to end

Say Ada signs up for your Pro plan at $49 a month and pays. That is four calls, in this order:

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

const faurya = createFauryaBilling();
const { billing } = faurya;

// 1. The customer — include the visitor id so revenue is attributed
await billing.customers.upsert("cus_42", {
  email: "ada@example.com",
  name: "Ada Lovelace",
  faurya_visitor_id: visitorId,
});

// 2. The plan — a name and a cadence, no price
await billing.plans.upsert("plan_pro_monthly", {
  name: "Pro",
  interval_unit: "month",
  interval_count: 1,
});

// 3. The invoice — the subscription line creates the subscription
await billing.invoices.upsert("inv_1042", {
  customer_external_id: "cus_42",
  currency: "USD",
  invoiced_at: "2026-03-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-03-01T00:00:00Z",
      service_period_end: "2026-04-01T00:00:00Z",
    },
  ],
});

// 4. The payment — this makes the invoice paid and Ada a subscriber
await billing.transactions.upsert("txn_7781", {
  invoice_external_id: "inv_1042",
  type: "payment",
  amount: 49,
  occurred_at: "2026-03-01T09:00:05Z",
});

Ada now counts as a subscriber with $49 MRR, attributed to the traffic source that brought her in.

Order matters. Customers and plans first, then invoices, then transactions. An invoice naming a customer Faurya hasn't seen yet is refused with CUSTOMER_NOT_FOUND. (The bulk import is the exception — it sorts the order out for you.)

Customers

await billing.customers.upsert("cus_42", {
  email: "ada@example.com",
  name: "Ada Lovelace",
  billing_address: { country: "GB", city: "London" },
  faurya_visitor_id: visitorId,
  faurya_session_id: sessionId,
});

Customer updates are partial — send only what changed:

You sendResult
Field left outKept as it was
Field set to nullCleared
Field set to a valueReplaced
await billing.customers.upsert("cus_42", { email: "ada@newdomain.com" }); // name and address kept
await billing.customers.upsert("cus_42", { name: null });                 // name cleared

Adding faurya_visitor_id is what links the customer's revenue to their visits. You can add it later, too — for example, the next time the customer logs in. If you get a VISITOR_PENDING warning, the visit hasn't been processed yet; the link completes on its own.

Plans

A plan is a name and a billing cadence. It has no price — prices live on invoice lines, so one plan works for every currency and discount.

await billing.plans.upsert("plan_pro_yearly", { name: "Pro (yearly)", interval_unit: "year", interval_count: 1 });
await billing.plans.upsert("plan_pro_quarterly", { name: "Pro (quarterly)", interval_unit: "month", interval_count: 3 });

interval_unit is day, week, month or year. Once an invoice uses the plan, its cadence can no longer change — create a new plan instead. Renaming is always fine.

Invoices

An invoice is what you billed. Each line is one of:

  • subscription — a recurring charge for a service period. Creates or renews a subscription.
  • trial — a trial period. Counts as service having started.
  • one_time — anything else: setup fees, add-ons, credits packs.
await billing.invoices.upsert("inv_1043", {
  customer_external_id: "cus_42",
  currency: "USD",
  invoiced_at: "2026-03-01T09:00:00Z",
  invoice_number: "INV-1043",
  line_items: [
    {
      external_id: "seat_line",
      type: "subscription",
      amount: 98,              // the line total, tax included
      quantity: 2,             // for reporting; amount is NOT multiplied by it
      tax_amount: 16.33,       // the part of amount that is tax
      subscription_external_id: "sub_901",
      plan_external_id: "plan_pro_monthly",
      service_period_start: "2026-03-01T00:00:00Z",
      service_period_end: "2026-04-01T00:00:00Z",
    },
    { external_id: "setup_fee", type: "one_time", amount: 20 },
  ],
});

Good to know:

  • Amounts are in major units (49.99), tax included, always positive.
  • While an invoice is unpaid, an upsert replaces the whole invoice. To fix it, send it again in full — lines you leave out are removed.
  • Once money has moved against it, the invoice is locked. Only due_at, invoice_number and invoice_url can change. To correct it, void it and send a new one.
  • The status is calculated for you from its transactions: open, paid, partially_paid, refunded…

Void or write off an invoice

// It never should have counted — remove it from every report
await billing.invoices.setStatus("inv_1043", "voided");

// It stays billed, but you'll never collect it
await billing.invoices.setStatus("inv_1043", "written_off");

Both are final.

Transactions

A transaction is money that moved against an invoice.

await billing.transactions.upsert("txn_7790", {
  invoice_external_id: "inv_1043",
  type: "payment",
  amount: 118,
  result: "succeeded",              // or "failed" / "pending"
  occurred_at: "2026-03-01T09:00:05Z",
  fee: 3.72,                        // optional processor fee
  metadata: { provider: "my-gateway" },
});
  • type is payment, refund, chargeback or chargeback_reversal. Amounts are always positive; the type says which way the money went.
  • A failed charge is still worth sending (result: "failed") — it shows dunning without counting as revenue.
  • A pending payment can later be updated to succeeded or failed by upserting the same id again. You can also fill in fee and metadata later. The type, amount, currency and invoice can never change — record a refund or reversal instead.

Refunds and chargebacks are covered in Subscription lifecycle.

Check what Faurya sees

When a number in your dashboard surprises you, ask Faurya what a subscription adds up to:

const { data: sub } = await billing.subscriptions.retrieve("sub_900");

sub.status;                    // e.g. "active"
sub.monthly_recurring_amount;  // 49
sub.counts_as_subscriber;      // true or false

if (!sub.counts_as_subscriber) {
  console.log(sub.not_counted_reason); // e.g. "NOT_COMMENCED"
}

The most common reason is NOT_COMMENCED: the first invoice was sent but never paid. Faurya treats an unpaid first invoice as a checkout that may never complete. Record the payment (or send a trial line or a subscription_start event) and the subscription starts counting.

not_counted_reasonWhat to do
NOT_COMMENCEDRecord the payment for the first invoice.
NO_STATESCheck that the invoice line actually saved.
NOT_IN_FORCE_YETExpected for a start scheduled in the future.
CANCELLED, EXPIRED, COMPLETEDExpected after churn.

Every object can also be read back with retrieve(id): customers, plans, invoices (with their lines and transactions) and transactions.

A typical integration

Most apps wire the SDK into the places their billing already happens:

When this happens in your appCall
A user signs up or changes their emailcustomers.upsert
You create or rename a planplans.upsert
You issue an invoice (signup, renewal, upgrade)invoices.upsert
A charge succeeds or failstransactions.upsert with type: "payment"
You refund a chargetransactions.upsert with type: "refund"
A user cancels or pausessubscriptionEvents.record

Because every call is idempotent, a simple and robust pattern is to call the SDK from your existing payment webhooks and to re-send freely on failure.

Next steps