Architecture

Settlement and Pix

Payment plans, the transfer-minimizing algorithm and how static Pix "copia e cola" codes are built.

A documentação técnica está em inglês.

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:

ModeMeaningWhat the app shows
eachEveryone paid their own share at the table.Per-person totals with a "mark as paid" toggle (and a PAGO stamp). No transfers.
singleOne person paid everything.Everybody else owes that person exactly their total.
customSeveral 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:

  1. Take the debtor who owes the most and the creditor who is owed the most.
  2. Transfer min(debt, credit) between them.
  3. 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 each mode, a person id ("already paid the waiter");
  • otherwise a transfer key from>to (see transferKey).

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:

IDFieldValue
00Payload format01
26Merchant account00 GUI br.gov.bcb.pix · 01 key · 02 optional message
52Category code0000
53Currency986 (BRL)
54Amounte.g. 50.50 (omitted when unknown)
58CountryBR
59Receiver nameup to 25 chars, accents stripped
60Cityup to 15 chars (defaults to BRASIL)
62Additional data05 txid ***
63CRCCRC-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.

Editar esta página no GitHub