# Invoices

A billing invoice is an immutable, numbered document. The relational fields
you see in responses (totals, dates, status) mirror a frozen, schema-valid
payload snapshotted at issue time; the PDF renders from that snapshot.

## Lifecycle

`draft → issued → sent → paid`, with `partially_paid` for an issued or sent
document some cash was applied to with a balance left, and `overdue` for an
issued, sent or partially paid document still unpaid past the day it fell due
(its `dueDate`, or the issue day of one due on receipt; an overdue document can
still be sent), and three corrective exits:

- `voided` — the document was cancelled before money moved (`voidedAt`,
  `voidReason`). A document with cash applied (`paidMinor > 0`) cannot be
  voided; reverse it with a credit note.
- written off — collection was abandoned (`writtenOffAt`, `writeOffReason`).
- credit note — a separate document (`invoiceType: "credit_note"`) naming the
  original in `originalInvoiceNumber`.

**Partially paid, and overdue wins.** `partially_paid` means "some cash
applied, a balance remains, not yet late". It is reached only from `issued` or
`sent`, by a payment that leaves a balance; another part payment leaves it
`partially_paid`; the payment that clears the balance (or leaves a shortfall
within the matching tolerance) makes it `paid`. Past its due day it becomes
`overdue` like any open document, and a part payment on an `overdue` document
leaves it `overdue`; a part payment on an `issued` or `sent` document already
past its due day leaves that status as it is until the overdue sweep's next run makes it
`overdue` — so "late" is always spelled `overdue`, and
`paidMinor` says how much of a late document was paid. Only cash moves a document to
`partially_paid`: stored credit drawn at issue does not (it shows in
`creditSettledMinor` and on the hosted page). A partially paid document carries
no payment link — partial payments reach bank-collected documents only — so it
is not sendable, cannot be voided (issue a credit note), and can be written off
for what remains.

`paidVia` records how a paid document settled: the source of the payment that
settled it — `payment_link` (the card rail — either the buyer visiting the
hosted page, or an off-session charge on their saved card when the seller has
auto-collection enabled), `bank_transfer` (a deposit on the collection account
named on the document), `existing_payment` (an already-settled payment used
to issue the invoice), or `quickbooks` (a payment the seller recorded in their
QuickBooks books against the invoice Fynex pushed there) — or `credit` (stored
credit covered it in full at issue). A document paid from several sources reads
as the one that settled it; `GET /invoices/{invoiceId}/applications` lists
every payment.

A document can therefore reach `paid` without you doing anything, and can
appear on `GET /invoices` without anyone creating it: whether terms and
metered usage are invoiced on a schedule, and whether saved cards are charged
off-session, are platform switches. See **Sandbox & testing** for
the switches themselves.

## Numbering

Every issued document carries TWO numbers, and they are not interchangeable.

`invoiceNumber` (`UK2607AB-2607AAC`) is the document's identity: unique across
every Fynex invoice family, allocated from the shared per-(agreement, month)
sequence, and **also the bank payment reference** — it is the only string a
buyer may quote on a transfer, and it is what `bankTransfer.paymentDescription`
returns.

`customerDocumentNumber` (`0000042`) is the document's position in that
customer's own series: cumulative for the life of the (seller, customer) pair,
never reset, and counted independently for each customer. Credit notes take the
next position like any other document, and a voided document keeps the one it
had. It is a convenience reference printed as "Your document no." — unique only
within the pair, so it is never a lookup key and never a payment reference.
Absent on documents issued before the series existed; those are not backfilled.

## Numbering

An invoice number is `<agreement>-<YYMM><order>`: your agreement number, a
dash, the issue month as `YYMM`, and a three-letter order within that month
(`AAA`, `AAB`, …) — for example `UK2607AA-2608AAB` is the second document
issued in August 2026 under agreement `UK2607AA`. The series is allocated
inside the issuing transaction, so it is gapless per month, and it is shared
with top-up invoices so the two document families can never collide. Numbers
are what `corrects` and `originalInvoiceNumber` reference, so store them
verbatim — do not parse meaning out of the order suffix.

`customerName` and `customerEmail` carry who the document was addressed to at
issue — taken from the frozen payload, so a customer later renaming themselves
does not rewrite an issued legal record. They are returned by the
single-invoice read and by `send`, and omitted from list rows.

## Endpoints

- `GET /billing-api/v1/invoices` — list, newest first, paged with
  `cursor`/`limit`. Filters: `contractId`, `sellerCustomerId`, `status`,
  `invoiceType` (`standard`, `credit_note`, `simplified`, `modified`),
  `origin` (`recurring`, `usage`, `milestone`, `project`, `one_time`, `adhoc`,
  `marketplace`; `subscription` is a deprecated alias of `recurring`),
  `corrects` (credit notes against an invoice number), `issuedFrom`/`issuedTo`
  (half-open date range).
- `POST /billing-api/v1/invoices` — compose, number and (by default) send a
  document in one call, behind a required `Idempotency-Key`. It is issued and
  immutable the moment the call answers `201`; the same key replays the
  document with `200` and no link, and a key whose request differs in its
  contract, customer, currency, `invoiceType`, `dueDate`, net line total or
  line count answers `422` naming the field. There are no drafts on this API:
  send explicit lines with `unitPriceMinor`. `invoiceType` accepts `standard`
  (the default), `simplified` and `modified`; **`credit_note` is refused with
  `400`** — a correction is raised against the document it corrects, which this
  request cannot express, and there is no `originalInvoiceNumber` field on it.
  Correct a mistake in the dashboard.
- `GET /billing-api/v1/invoices/{invoiceId}` — one invoice with its lines.
- `GET /billing-api/v1/invoices/{invoiceId}/pdf` — the rendered PDF.
- `POST /billing-api/v1/invoices/{invoiceId}/send` — materialize the
  collection instrument: creates the invoice's hosted payment link and returns
  the invoice together with `paymentLinkUrl`. **It emails your customer**
  whenever the document carries a buyer email; there is no per-request
  suppression in v1, so the first call is customer-facing. The invoice is
  itself the collection anchor, so retries and simultaneous calls return the
  one existing link and do not send another email — no idempotency header is
  needed. Not applicable (`422`) to bank-transfer documents, to documents
  outside `issued`/`sent`/`overdue` (a `draft`, a `partially_paid` one, or one
  already `paid`, `voided` or `written_off`), or to documents with nothing left
  to collect.
- `GET /billing-api/v1/invoices/{invoiceId}/applications` — every payment and
  credit drawdown applied to the document, oldest first (see **Applying
  money**).

## Invoicing an already-settled payment

Use the existing `POST /billing-api/v1/invoices` operation with
`sourcePaymentId` and **explicit `send: false`**. The dashboard HTTP route
`POST /billing/v1/invoices` accepts the same fields with `sellerAccountUuid`.
This is an explicit repair/issuance operation, not an automatic historical
backfill. It does not enable recurring invoices or change ordinary checkout.

The customer and contract must already exist and belong to the authenticated
seller. Supply the ordinary invoice party, line and tax fields; the full computed
invoice total and currency must equal the source payment. Do not derive a tax
exemption or the supplier identity merely from the payment method. Normal issuer
configuration and jurisdiction validation still apply.

```json
{
  "contractId": 42,
  "sellerCustomerId": 7,
  "sourcePaymentId": 1402,
  "send": false,
  "jurisdiction": "UK",
  "currency": "EUR",
  "buyer": {
    "name": "Example Customer",
    "email": "buyer@example.com",
    "addressLine1": "22 High Street",
    "city": "Manchester",
    "postalCode": "M1 1AE",
    "country": "GB"
  },
  "lineItems": [{
    "description": "Previously purchased service",
    "quantity": "1",
    "unitPriceMinor": 14999,
    "taxCategory": "zero",
    "taxRatePercent": "0"
  }]
}
```

The example is illustrative, not a command to execute against production. The
public endpoint still requires seller authentication and an `Idempotency-Key`.
Dashboard writes still require the seller's own non-impersonated session.

Number allocation, immutable invoice creation, source-payment binding and cash
allocation are atomic. Success returns a paid invoice with `sourcePaymentId`,
`paidVia: "existing_payment"`, the full `paidMinor`, and zero remaining balance.
The invoice issue date is the current seller-calendar date; `paidAt` and the
allocation's `receivedAt` retain the source capture time. `dateOfSupply` is a
separate business fact, not inferred from payment time.

No payment link, provider charge, stored-credit drawdown or customer email is
created. A source payment is not a request to "mark paid" without evidence:
foreign, deleted, uncollected, already-linked, refunded/ambiguous-refund or
disputed payments are refused. Only standard ad-hoc/one-time invoices are supported; draft issuance, credit notes, simplified/modified invoices and automatic contract lanes are not. Partial and multi-payment invoices are outside
this operation's scope. Repeating a source payment cannot issue a second invoice;
a changed document is refused rather than silently replacing the first one.

After issuance, read the invoice and `/invoices/{invoiceId}/applications`, verify
the source payment ID, amount and paid balance, then download the official PDF.
Do not independently edit a downloaded PDF's number, issuer or tax lines: the
stored payload is the issued document. No SQL-based numbering or allocation is
part of this workflow.

## Amounts

`grandTotalMinor` is the legal document total. `collectibleMinor` is what
collection asks the customer for: the grand total less `creditAppliedMinor`
(stored credit drawn down at issue).

`paidMinor` is the cash applied so far — hosted payments and matched bank
transfers; it is the document's amount paid (`amount_paid ≡ paidMinor`, the
name is kept). A bank transfer may pay part of a document: an `issued` or
`sent` document becomes `partially_paid` (an `overdue` one stays `overdue`),
`collectibleMinor − paidMinor` (`remainingMinor`, the outstanding) is what is
still asked for, and an `InvoicePartiallyPaid` webhook fires per payment. One
transfer can pay several documents only when its remittance text quotes each
invoice number; it is applied in the order written. Money beyond what the
quoted documents owe, or a transfer none of them can take (another currency,
another account, already paid), is held as unapplied and never auto-credited.
A short transfer settles the document when the shortfall is within the
seller's matching tolerance, which is capped at 1% of the document's remaining
balance.

A `paid` document does **not** always report `paidMinor = collectibleMinor`.
It can report `remainingMinor > 0`, or even `paidMinor = 0`, when it was:

- settled within the matching tolerance: `paidMinor` is the cash received and
  the shortfall stays in `remainingMinor`;
- paid before cash was recorded per payment: documents settled before this
  field existed report `0`;
- settled through a payment link whose collecting payment no longer exists on
  the link: the status moves, but no cash is recorded. (A read of that payment
  that merely fails is retried on the next sweep; the document is not settled
  until it succeeds.)

`status` (and `paidVia`) is what says a document is settled; `paidMinor` is
only the cash recorded against it.

`creditSettledMinor` reports only the part discharged by that stored credit —
it is **not** a payment total, and a card- or transfer-paid invoice reports
`0`. Neither amount changes when the invoice is paid: `status` and `paidVia`
are what tell you the outcome.

`outstandingMinor` is the one to age in an AR report. `collectibleMinor` is
frozen at issue and stays there, so it keeps asking for the full amount after a
credit note, a write-off or any other adjustment; `outstandingMinor` is derived
from the adjustment ledger on every read and reports what is genuinely still
owed (`0` for a voided document). It is omitted when the adjustment ledger is
unavailable — absence means **unknown**, not `0`.

## Applying money

This is the one place the payment-application policy is written down.

**Applications.** Every payment applied to a document is recorded as its own
application: the document, the amount, when the money arrived, and the rail it
came through (`payment_link`, `bank_transfer`, `quickbooks`). The sum of a
document's cash applications is `paidMinor`. Stored credit drawn at issue is
not a payment and is never one of those records; `GET
/invoices/{invoiceId}/applications` lists it beside them as `kind: credit`
(net of anything a cancellation handed back), and it sums to
`creditSettledMinor`. `paidVia` is a projection: the source of the
application that settled the document.

**Fynex-collected money (payment link, bank transfer).**

1. Money beyond what a document still owes is applied up to its remaining
   balance; the excess is held as **unapplied** money (`overpayment`). Money
   no quoted document can take is held unapplied with its reason. It is never
   an error and never auto-credited — a person decides.
2. A transfer short of the remaining balance by no more than the seller's
   matching tolerance (capped at 1% of that balance) settles the document;
   a larger shortfall part-pays it.
3. A hosted payment that collects less than the link asked still settles the
   document (a known limitation: the link cannot ask for a different amount).
4. One transfer can pay several documents only when its remittance text quotes
   each invoice number; it is applied in the order written.
5. Money in another currency than the document's is never applied (there is no
   FX source); it is held unapplied.

**Payments recorded in QuickBooks** (`source: quickbooks`). That money is in
the seller's own books, not in a Fynex account, so it is **never** held as
unapplied money here: an application larger than what the document still owes
lands only the remaining balance (the excess stays in QuickBooks), and one on
a document that is paid, voided, written off or fully credited lands nothing.
A shortfall within the matching tolerance settles, as for a transfer.

### Payments recorded in QuickBooks

When the seller's QuickBooks company is connected, invoices are pushed there
(`billing.quickbooks.push_invoices`), and
`billing.allocations.from_quickbooks_payments` is on, a payment the seller
records **in QuickBooks** against a pushed invoice is applied to the Fynex
invoice within minutes:

- Only what QuickBooks itself applied counts: a payment line linked to the
  invoice, with its amount. A payment merely linked to the invoice without an
  amount, a deposit, and credit memos apply nothing.
- Each QuickBooks payment applies to a Fynex invoice **once**, whatever is
  re-synced. It is listed by the applications read with
  `reference.quickBooksPaymentId`.
- The status follows the same rules as any other payment: `issued`/`sent` →
  `partially_paid` while a balance remains, → `paid` (`paidVia: quickbooks`,
  `paidAt` = the payment's date in QuickBooks) when nothing remains or the
  shortfall is within tolerance; an `overdue` document stays `overdue` until
  paid. `InvoicePartiallyPaid` / `InvoicePaid` fire as usual.
- **Fynex's own payments are never counted twice.** Every payment Fynex
  collects is recorded in QuickBooks with the reference `FYNEX-<8 hex>`; when
  that record syncs back it is recognised and ignored. A `FYNEX-` reference
  that names no Fynex payment of that amount on that invoice is set aside for
  a person.
- **A part payment on a document collected by payment link is not applied**:
  the live link would still ask for the full amount and cannot be shrunk. A
  payment that settles such a document is applied, and its link is retired.
- Invoices QuickBooks itself issued (never pushed by Fynex) are never touched;
  their paid state stays QuickBooks'.
- Nothing is recorded back into QuickBooks for these payments.
- An amount changed, or a payment deleted, in QuickBooks **after** it was
  applied changes nothing here (payments are append-only); it is flagged for a
  person.
