Architecture
Settlement and Pix
Payment plans, the transfer-minimizing algorithm and how static Pix "copia e cola" codes are built.
The breakdown says what each person owes the bill. Settlement answers the question people actually ask at the end of the night: who sends money to whom?
Payment plans
bill.payment records how the bill was paid at the venue:
| Mode | Meaning | What the app shows |
|---|---|---|
each | Everyone paid their own share at the table. | Per-person totals with a "mark as paid" toggle (and a PAGO stamp). No transfers. |
single | One person paid everything. | Everybody else owes that person exactly their total. |
custom | Several people put money in (two cards, cash + card…). | Amount inputs per person; once they add up to the total, the transfers that even everyone out. |
paidAmounts(bill, breakdown) turns any plan into "how much each person handed over". In custom mode, while the amounts don't add up to the grand total, computeSettlement returns the difference and no transfers — the UI shows "R$ 12,30 still missing" instead of guessing.
Minimizing transfers
Each person's balance is paid − owed. Positive balances are creditors, negative are debtors. minimizeTransfers then repeats:
- Take the debtor who owes the most and the creditor who is owed the most.
- Transfer
min(debt, credit)between them. - Drop whoever reached zero.
This produces at most n − 1 transfers. For the shapes that matter here — one payer, or a couple of payers — it is optimal. (The general minimum-transfers problem is NP-hard; a greedy pass is the standard trade-off.) Ties follow bill order and the output is sorted by receiver, so the list doesn't reshuffle between renders.
The test suite checks the property that matters: after applying every transfer, every balance is exactly zero.
Settled flags
bill.settled is a set of keys:
- in
eachmode, a person id ("already paid the waiter"); - otherwise a transfer key
from>to(seetransferKey).
Switching the payment plan clears all flags, since the old keys no longer describe real transfers. A person is "square" (and gets the stamp on the receipt) when every one of their outgoing transfers is settled — see settledPeople in features/bill/format.ts.
Pix
Pix is Brazil's instant payment system. When the bill currency is BRL and a receiver has a Pix key, each transfer gets:
- a copia e cola button — copies a payload the payer pastes into their bank app, with the amount already filled in;
- a QR code of the same payload, for when you're sitting across the table.
The payload (static BR Code)
buildPixPayload in src/lib/pix.ts implements the static flavor of the BR Code (EMV® QRCPS-MPM), as specified in Banco Central's Manual de Padrões para Iniciação do Pix. Each field is ID + two-digit length + value:
| ID | Field | Value |
|---|---|---|
00 | Payload format | 01 |
26 | Merchant account | 00 GUI br.gov.bcb.pix · 01 key · 02 optional message |
52 | Category code | 0000 |
53 | Currency | 986 (BRL) |
54 | Amount | e.g. 50.50 (omitted when unknown) |
58 | Country | BR |
59 | Receiver name | up to 25 chars, accents stripped |
60 | City | up to 15 chars (defaults to BRASIL) |
62 | Additional data | 05 txid *** |
63 | CRC | CRC-16/CCITT-FALSE over everything before it, including 6304 |
Implementation notes:
- The merchant-account template may not exceed 99 characters, so the optional message is trimmed to whatever fits next to the key.
- Keys are normalized the way banks expect: phones become
+55DDDNUMBER, CPF/CNPJ lose punctuation, e-mails and random keys are lowercased. An 11-digit number is treated as a CPF only if its check digits are valid; otherwise it's a phone. The UI shows the detected type so people can catch mistakes. - The test suite reproduces the reference payload from the BCB manual byte for byte (CRC
1D3D), and checks the CRC-16 check value for"123456789"(29B1).
Privacy of Pix keys
A Pix key is stored only in the bill (in localStorage) and travels only inside share links that the organizer chooses to send. It is never sent to a server by Splitmize.