> The full documentation index: https://palbase.studio/llms.txt

# Payers & Invoices

Your server can bill a payer it names itself — a company, a team, a workspace — not only a signed-in user: save their cards, charge one with nobody at the screen, and send them a taxed invoice. Every call here takes the project's secret key (`createServer().payments` in `palbase/server`, `Palbase.payments` in `palbase/backend`); who may manage which payer's cards is your app's rule.

## Payers

A payer is any `customer_id`: a user's id, or a name of 1 to 128 characters your server gives a company or a team. `updateCustomer` (its profile) or `addCustomerMethod` (its first card) makes it; until then every other call about it answers `404 customer_not_found`.

```ts
import { createServer } from "palbase/server";

const server = createServer({ url: process.env.PALBASE_URL!, secretKey: process.env.PALBASE_SECRET_KEY! });

await server.payments.updateCustomer({
  path: { customer_id: "acme" },
  body: {
    name: "Acme GmbH",
    email: "billing@acme.example",
    billing_address: { line1: "Unter den Linden 1", city: "Berlin", postal_code: "10117", country: "DE" },
    tax_id: "DE123456789",
    tax_id_type: "eu_vat",
  },
});
```

The profile is kept whole, as given. On a Stripe channel it is written on the payer's Stripe customer — name, email, phone, billing address and, with `tax_id_type`, the tax number — and Stripe prints it on their invoices. A tax number or an address Stripe refuses answers `tax_id_invalid` or `billing_address_invalid`, and the profile is not changed. Before the payer has a Stripe customer, the profile is kept and Stripe's refusal comes with the call that makes it (`addCustomerMethod`, or an invoice). `getCustomer` reads it back; `deleteCustomer` forgets the payer (their Stripe customer and cards go first; a payer with an invoice still open is not deleted: `409 customer_has_open_invoices`).

## Saved cards

`addCustomerMethod` opens Stripe's own page that saves a card, nothing charged, and answers it as a [NextAction](/docs/payments/checkout) to send whoever holds the card to. The card is listed once Stripe tells it saved.

```ts
import { createServer } from "palbase/server";

const server = createServer({ url: process.env.PALBASE_URL!, secretKey: process.env.PALBASE_SECRET_KEY! });

const { data: next } = await server.payments.addCustomerMethod({
  path: { customer_id: "acme" },
  body: { return_url: "https://app.example.com/billing", make_default: true, collect_billing_address: true },
});
// next.next_action.url: Stripe's page for the card holder
```

| Call | What it does |
|---|---|
| `listCustomerMethods` | The payer's active cards: each channel's default first, then the newest. Each carries `is_default` and the card's `fingerprint`. |
| `setCustomerDefaultMethod` | Makes a card the default on its channel. The first card saved is the default; when the default is removed, the oldest card left takes its place. |
| `deleteCustomerMethod` | Takes the card off Stripe's vault and the list. |
| `createCustomerPortal` | Opens Stripe's billing portal for the payer. For a day after, the list follows what the payer adds or removes there; the default stays Palbase's. |

iyzico keeps no card for the project: on an iyzico channel `addCustomerMethod` answers `setup_only_unsupported` ([iyzico](/docs/payments/iyzico)).

A saved card is charged with nobody at the screen by a PaymentIntent your server confirms with `off_session: true` and the card's `method_id`: a bank that asks its holder to confirm declines it with its own code, and nothing waits on them ([Checkout](/docs/payments/checkout)).

## Invoices

`createCustomerInvoice` makes the payer an invoice at Stripe — Stripe's own: its number, its page, its PDF and its e-mail are Stripe's — and charges it to a saved card. Each line's net amount is its `quantity` times its `unit_amount`; with `automatic_tax` (the default) [Stripe Tax](https://docs.stripe.com/tax) computes the tax on top, from the payer's billing address.

```ts
import { createServer } from "palbase/server";

const server = createServer({ url: process.env.PALBASE_URL!, secretKey: process.env.PALBASE_SECRET_KEY! });

const { data: invoice } = await server.payments.createCustomerInvoice({
  path: { customer_id: "acme" },
  body: {
    currency: "EUR",
    lines: [
      { description: "Pro plan, November", unit_amount: 9500, tax_code: "txcd_10103001" },
      { description: "Card fee", unit_amount: 500 },
    ],
    // method_id: a saved card; the payer's default card on the channel when left out
  },
});
// invoice.status "paid", invoice.tax 1900, invoice.total 11900, invoice.number, invoice.hosted_url
```

| Field | What it says |
|---|---|
| `lines` | 1 to 50: `description`, `unit_amount` (the net price of one, in minor units), `quantity` (1), `tax_code` (Stripe Tax's `txcd_…`; the account's default when left out). |
| `collection_method` | `charge_automatically` (the default): a saved card is charged. `send_invoice`: the payer pays on its page within `days_until_due` (30); `sendInvoice` has Stripe e-mail it to them. |
| `pay` | `false` leaves a card's invoice open, to pay later. |
| `automatic_tax` | `false` makes it without tax. |
| `description`, `footer`, `reference`, `metadata` | Yours: shown on it, or kept with it. |

An invoice is `creating`, then `draft`, while it is made, then `open`, then `paid`, `void` or `uncollectible`; `refused` is one Stripe refused to make: nothing is owed on it, and it is not listed. A declined card answers `402` with the card's decline code, and the invoice stays `open` beside the error: `payInvoice` charges it again — with another card when you name its `method_id` — and Palbase counts each charge in `attempt_count`. Running through the payer's cards in turn is your app's call.

| Call | What it does |
|---|---|
| `getInvoice` / `listCustomerInvoices` | Read one, or the payer's, newest first. |
| `payInvoice` | Charges an open or uncollectible invoice, with nobody at the screen. A `send_invoice` invoice needs a `method_id` named (`422 method_required`), or is paid on its page. |
| `sendInvoice` | Has Stripe e-mail an open or uncollectible `send_invoice` invoice to the payer (a card's invoice: `409 invoice_status_conflict`). |
| `voidInvoice` | Nothing more is owed on an open or uncollectible invoice (a paid one is refunded instead: `409 invoice_status_conflict`). |
| `markInvoiceUncollectible` | Marks an open invoice as not expected to be paid; it can still be paid. |

What the payer pays on the invoice's page, or in parts at Stripe's dashboard, is read back: `amount_paid`, and `paid` once it is all paid. The invoice's payment is a payment of kind `invoice` (`payment_intent_id`): refund it like any payment ([Refunds](/docs/payments/refunds-disputes)), and the invoice's `refunded_amount` and `payments.invoice.refunded` follow. An invoice paid in parts is refunded charge by charge: a refund larger than one charge holds answers `409 refund_spans_payments`. A dispute of its charge names the invoice (`payer_invoice_id`) and the card (`card_fingerprint`).

Its events are `payments.invoice.finalized`, `.paid`, `.payment_failed`, `.voided`, `.uncollectible` and `.refunded` ([Webhook events](/docs/payments/server-sdk#webhook-events)).

## Where invoices are made

Invoices are made on Stripe channels alone; `listChannels` says whose provider makes them (`capabilities.invoices`). A Stripe channel without the permission below still answers `409 invoice_permission_required`. An iyzico channel answers `422 invoices_not_supported`: iyzico has no invoices of its own.

- A Stripe channel connected with a restricted key needs **Invoices: Write** on the key.
- A channel connected through Palbase's [Stripe App](/docs/payments/stripe) needs the app's invoice permission: an account that installed the app before it asked for it accepts the new permission in its Stripe dashboard. Until then the channel answers `409 invoice_permission_required`.
- With `automatic_tax`, Stripe Tax must be set up on the account, with a registration where the payer is taxed; an address Stripe cannot place answers `billing_address_invalid`.

## From the CLI

The same calls, for an operator at a terminal (the environment's developer role, asked about first on a production environment):

```sh
palbase payments:payers acme                                   # the profile
palbase payments:payers:put acme --file profile.json           # write it whole
palbase payments:payers:cards acme                             # its cards, defaults first
palbase payments:payers:cards:add acme --return-url https://app.example.com/billing --default
palbase payments:payers:cards:default acme <method-id>         # make a card the default
palbase payments:payers:cards:remove acme <method-id>
palbase payments:payers:portal acme --return-url https://app.example.com/billing
palbase payments:invoices:create acme --file invoice.json --card <method-id>
palbase payments:invoices acme                                 # its invoices, newest first
palbase payments:invoices:get <invoice-id>
palbase payments:invoices:pay <invoice-id> --card <method-id>  # charge again, with another card
palbase payments:invoices:send <invoice-id>                    # e-mail a send_invoice invoice
palbase payments:invoices:void <invoice-id>
palbase payments:invoices:uncollectible <invoice-id>
palbase payments:payers:delete acme                            # forget the payer
```

`invoice.json` is the body of `createCustomerInvoice` (`currency`, `lines`, and the rest of the fields above); `--card` names the card in place of a `method_id` in it.

## Related

- [Checkout & PaymentSheet](/docs/payments/checkout) — PaymentIntents and `off_session`
- [Refunds & Disputes](/docs/payments/refunds-disputes)
- [Stripe](/docs/payments/stripe) — keys, the Stripe App, and permissions
- [Server & Backend SDK](/docs/payments/server-sdk)
