Bank transfer details
ClosingShows one set of bank details, picked from the payer's country and the invoice currency: sort code and account for a UK payer in sterling, IBAN and BIC for SEPA in euros, ABA routing and account for a US payer in dollars, and IBAN and BIC with a SWIFT note for anyone else. Every rail at once confuses payers. Each detail is checked (IBAN mod-97, the ABA 3-7-1 checksum, sort code shape), and a failed one is withheld rather than printed. The reference is fitted to the rail's limit and character set: 18 for Bacs and Faster Payments, 80 for ACH, 140 for SEPA and SWIFT. A longer one falls back to the customer reference rather than being cut short by the payer's bank. The account name prints exactly as given, for Confirmation of Payee. When the details have changed, a fixed warning to verify by phone comes first.
npx dxcl add bank-transfer-details Voorbeelddata
de JSON waarmee dit voorbeeld is gebouwd{
"payee": {
"accountName": "Northern Facilities Management Services (Holdings) Ltd",
"uk": {
"sortCode": "60-16-13",
"accountNumber": "31926819"
},
"iban": "GB29 NWBK 6016 1331 9268 19",
"bic": "NWBKGB2L",
"us": {
"achRouting": "121000358",
"wireRouting": "026009593",
"accountNumber": "000123456789"
},
"detailsChangedOn": "2026-11-03"
},
"payer": {
"country": "GB"
},
"invoice": {
"currency": "GBP",
"reference": "INV-2026-11-004817/REV-2",
"customerReference": "NFMS-004817"
}
} Gerenderd voorbeeld
elke opstelling op zichzelf gerenderdarrangement="stacked" standaard arrangement="table" arrangement="split" Opgeloste nodes
wat een renderer krijgt — 1 nodesbank-transfer section De bestanden
wat installeren schrijft; het gekozen bestand is de componentnodes/bank-transfer-details.node.tsx import { arrange, Section, type Section as SectionComponent, useDeriver, useState } from "docxcelerate/template";
import type { BankTransferDetailsLines } from "../arrangements/bank-transfer-details/lines.ts";
import { split } from "../arrangements/bank-transfer-details/split.arrangement.tsx";
import { stacked } from "../arrangements/bank-transfer-details/stacked.arrangement.tsx";
import { table } from "../arrangements/bank-transfer-details/table.arrangement.tsx";
import bankTransferDetails from "../derivers/bank-transfer-details.deriver.ts";
/**
* One way to pay this invoice by bank transfer: the one that suits this payer,
* with only details that have passed their checks.
*
* One rail, not all of them: a payer shown a sort code, an IBAN and a routing
* number picks the wrong one. The payer's country and the invoice currency
* decide it: sort code and account for a UK payer in sterling, IBAN and BIC for
* SEPA in euros, ABA routing and account for a US payer in dollars, and IBAN and
* BIC with a SWIFT note for anyone else.
*
* Checked, then printed: a transposed digit would only be caught at the payer's
* bank, or not at all. A detail that fails is withheld and the payer falls to
* the next rail that can be printed honestly, and to a "call us" line when
* there is none. A rail is never printed half-filled.
*
* The reference is fitted to the rail: banks cut a longer one short without
* saying so and the payment cannot then be matched. When the invoice reference
* does not fit, the customer reference is printed instead. Never a truncated
* one: when neither fits, the document fails.
*
* The account name is printed as given, because UK Confirmation of Payee
* compares it character for character.
*
* When the details changed recently, a fixed warning to confirm them by phone
* comes first, with no number in it: a forged invoice would print the
* fraudster's. It is never generated text.
*
* All of this is decided once, in one deriver; arithmetic and string work on
* request data throw at publish time without one. This file and the
* arrangements, in `arrangements/bank-transfer-details/`, only place the lines.
*
* Installed by `dxcl add bank-transfer-details`, which also installs its deriver.
*/
/** What this component reads. Add these fields to your document data type. */
export interface BankTransferDetailsData {
/** Who is being paid, and every account they can be paid into. */
payee: {
/**
* The account name exactly as the bank holds it. Only outer whitespace is
* trimmed: no case change, no abbreviation, no "Ltd" to "Limited". UK
* Confirmation of Payee compares it character for character. Required: the
* deriver throws when it is missing or blank.
*/
accountName: string;
/**
* UK domestic details, used for a UK payer paying in GBP. Spaces and
* hyphens are ignored. Missing, or either part invalid, means the UK rail
* is not offered and the payer is sent to IBAN and BIC instead.
*/
uk?: {
/** Six digits, in any of `601613`, `60-16-13` or `60 16 13`. Printed `60-16-13`. */
sortCode: string;
/** Exactly eight digits. A shorter UK account number must be zero-padded by the caller. */
accountNumber: string;
};
/**
* The IBAN, with or without spaces, in any case. Checked with ISO 13616
* mod-97 and printed upper case in groups of four. Missing or invalid
* means no SEPA and no international rail.
*/
iban?: string;
/**
* The BIC (SWIFT code), 8 or 11 characters. Printed upper case, without
* spaces, as given (an 8-character BIC is not padded with XXX). Optional
* for SEPA: the IBAN alone is printed when it is missing or invalid.
* Required for the international rail: without a valid one, that rail is
* not offered.
*/
bic?: string;
/**
* US details, used for a US payer paying in USD. Spaces and hyphens are
* ignored. Missing, or an invalid ACH routing or account number, means the
* US rail is not offered.
*/
us?: {
/** The ABA routing number for ACH: nine digits that pass the 3-7-1 checksum. */
achRouting: string;
/**
* The routing number for wires, when the bank uses a different one.
* Missing, or the same as `achRouting`, means one routing number serves
* both. Invalid means it is withheld and only ACH is offered.
*/
wireRouting?: string;
/** The account number: 4 to 17 digits. */
accountNumber: string;
};
/**
* The ISO 8601 date these details changed, when they differ from the
* details on this payer's previous invoice. Set it for invoices issued
* while the change is new, and remove it once the payer has paid to the
* new account: the component cannot see invoice history. When it is
* present, the verify-by-phone warning is printed above the details. A
* value that is not YYYY-MM-DD still prints the warning, without the date.
* Missing or empty means no warning.
*/
detailsChangedOn?: string;
};
/** Who is paying. */
payer: {
/**
* ISO 3166-1 alpha-2 country code, any case. `UK` is accepted as `GB`.
* Missing or unrecognised means the payer is treated as international.
*/
country?: string;
};
/** The invoice being paid. */
invoice: {
/**
* ISO 4217 code, any case: `GBP`, `EUR`, `USD`. Together with the country,
* it picks the rail. Required: the deriver throws unless it is three
* letters.
*/
currency: string;
/**
* The reference the payer should quote, usually the invoice number.
* Printed when it fits the chosen rail. Required: the deriver throws when
* it is missing or blank.
*/
reference: string;
/**
* A shorter reference for when `reference` does not fit the rail, such as
* the customer's account number. Printed only in that case. When neither
* reference fits, the deriver throws rather than print one the bank will
* cut short.
*/
customerReference?: string;
};
}
/**
* Where the lines sit.
*
* - `stacked`: one labelled line under another. The default.
* - `table`: labels in a narrow column, values beside them.
* - `split`: where to send the money on the left, the reference to quote ranged right.
*/
export type BankTransferDetailsArrangement = "stacked" | "table" | "split";
/** Props for {@linkcode BankTransferDetails}. */
export interface BankTransferDetailsProps {
/** Where the lines sit. Defaults to `"stacked"`. */
arrangement?: BankTransferDetailsArrangement;
}
export const BankTransferDetails: SectionComponent<BankTransferDetailsProps> = async ({ arrangement }) => {
const [payee] = useState((data: BankTransferDetailsData) => data.payee);
const [payer] = useState((data: BankTransferDetailsData) => data.payer);
const [invoice] = useState((data: BankTransferDetailsData) => data.invoice);
const details = await useDeriver(bankTransferDetails, [payee, payer, invoice]);
const lines: BankTransferDetailsLines = {
warning: details.warning,
method: details.method,
accountName: details.accountName,
sortCode: details.sortCode,
routingLabel: details.routingLabel,
routing: details.routing,
wireRouting: details.wireRouting,
accountNumber: details.accountNumber,
iban: details.iban,
bic: details.bic,
reference: details.reference,
};
return (
<Section id="bank-transfer" title="How to pay">
{arrange(arrangement, {
stacked: () => stacked(lines),
table: () => table(lines),
split: () => split(lines),
})}
</Section>
);
}; derivers/bank-transfer-details.deriver.ts import { deriver } from "docxcelerate/template";
/**
* Decides which bank details a payer sees, checks them, and fits the payment
* reference to the rail. Installed by `dxcl add bank-transfer-details`.
*
* It is self-contained on purpose: it travels to the engine as source text, so
* a module-level constant or helper would be a ReferenceError there. The country
* list, the character sets and every helper are declared inside `run`.
*/
export default deriver({
name: "bankTransferDetails",
run: (payee: unknown, payer: unknown, invoice: unknown) => {
// deno-lint-ignore no-explicit-any
const pay = payee as any;
// deno-lint-ignore no-explicit-any
const per = payer as any;
// deno-lint-ignore no-explicit-any
const inv = invoice as any;
if (typeof pay?.accountName !== "string" || pay.accountName.trim() === "") {
throw new Error("payee.accountName is required");
}
if (typeof inv?.reference !== "string" || inv.reference.trim() === "") {
throw new Error("invoice.reference is required");
}
if (typeof inv.currency !== "string" || !/^[A-Za-z]{3}$/.test(inv.currency.trim())) {
throw new Error("invoice.currency must be a three-letter ISO 4217 code");
}
const eea = [
"AT", "BE", "BG", "HR", "CY", "CZ", "DK", "EE", "FI", "FR", "DE", "GR", "IE", "IT", "LV",
"LT", "LU", "MT", "NL", "PL", "PT", "RO", "SK", "SI", "ES", "SE", "IS", "LI", "NO",
];
// Bacs and Faster Payments; the SWIFT basic set is shared by SEPA, MT103 and ACH.
const ukSet = /^[A-Z0-9 .\-/&]+$/;
const swiftSet = /^[A-Za-z0-9/\-?:().,'+ ]+$/;
const limits: Record<string, { max: number; set: RegExp; text: string; upper: boolean }> = {
uk: { max: 18, set: ukSet, text: "A-Z, 0-9, space and . - / &", upper: true },
sepa: { max: 140, set: swiftSet, text: "A-Z, a-z, 0-9, space and / - ? : ( ) . , ' +", upper: false },
international: { max: 140, set: swiftSet, text: "A-Z, a-z, 0-9, space and / - ? : ( ) . , ' +", upper: false },
// 80 is ACH CCD+ addenda, tighter than Fedwire's 4x35, so one reference fits both.
us: { max: 80, set: swiftSet, text: "A-Z, a-z, 0-9, space and / - ? : ( ) . , ' +", upper: false },
};
let country = String(per?.country ?? "").trim().toUpperCase();
if (country === "UK") country = "GB";
const currency = inv.currency.trim().toUpperCase();
// Spaces and hyphens ignored; null unless all digits and an allowed length.
const digits = (s: unknown, min: number, max: number): string | null => {
if (typeof s !== "string") return null;
const d = s.replace(/[\s-]/g, "");
return /^\d+$/.test(d) && d.length >= min && d.length <= max ? d : null;
};
const ibanOf = (s: unknown): string | null => {
if (typeof s !== "string") return null;
const v = s.replace(/\s+/g, "").toUpperCase();
if (!/^[A-Z]{2}\d{2}[A-Z0-9]{11,30}$/.test(v)) return null;
const moved = v.slice(4) + v.slice(0, 4);
let rem = 0;
for (const ch of moved) {
const code = ch.charCodeAt(0);
const value = code >= 65 ? String(code - 55) : ch;
for (const d of value) rem = (rem * 10 + Number(d)) % 97;
}
return rem === 1 ? v.replace(/(.{4})(?=.)/g, "$1 ") : null;
};
const bicOf = (s: unknown): string | null => {
if (typeof s !== "string") return null;
const v = s.replace(/\s+/g, "").toUpperCase();
return /^[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}([A-Z0-9]{3})?$/.test(v) ? v : null;
};
const abaOf = (s: unknown): string | null => {
const d = digits(s, 9, 9);
if (d === null) return null;
const n = d.split("").map(Number);
const sum = 3 * (n[0] + n[3] + n[6]) + 7 * (n[1] + n[4] + n[7]) + (n[2] + n[5] + n[8]);
return sum % 10 === 0 ? d : null;
};
const sortCode = digits(pay.uk?.sortCode, 6, 6);
const ukAccount = digits(pay.uk?.accountNumber, 8, 8);
const iban = ibanOf(pay.iban);
const bic = bicOf(pay.bic);
const ach = abaOf(pay.us?.achRouting);
const usAccount = digits(pay.us?.accountNumber, 4, 17);
let rail = "none";
if (country === "GB" && currency === "GBP" && sortCode !== null && ukAccount !== null) rail = "uk";
else if (eea.includes(country) && currency === "EUR" && iban !== null) rail = "sepa";
else if (country === "US" && currency === "USD" && ach !== null && usAccount !== null) rail = "us";
else if (iban !== null && bic !== null) rail = "international";
const lines = {
warning: null as string | null,
method: "We cannot print our bank details on this invoice. Please call us, on a number you already have for us, to arrange payment.",
accountName: null as string | null,
sortCode: null as string | null,
routingLabel: null as string | null,
routing: null as string | null,
wireRouting: null as string | null,
accountNumber: null as string | null,
iban: null as string | null,
bic: null as string | null,
reference: null as string | null,
};
if (rail === "none") return lines;
lines.accountName = pay.accountName.trim();
if (rail === "uk") {
lines.method =
"Pay by bank transfer (Faster Payments or Bacs). Enter the account name exactly as it is shown here: your bank checks it against the account before you pay.";
lines.sortCode = (sortCode as string).replace(/(\d\d)(\d\d)(\d\d)/, "$1-$2-$3");
lines.accountNumber = ukAccount;
} else if (rail === "sepa") {
lines.method = "Pay by SEPA credit transfer, in euros.";
lines.iban = iban;
lines.bic = bic;
} else if (rail === "us") {
const wire = abaOf(pay.us.wireRouting);
const given = pay.us.wireRouting !== undefined && pay.us.wireRouting !== null && pay.us.wireRouting !== "";
const separate = wire !== null && wire !== ach;
// Never imply the ACH number works for wires when the caller said it does not.
lines.method = given && wire === null
? "Pay by ACH transfer, in US dollars."
: "Pay by ACH or wire transfer, in US dollars.";
lines.routingLabel = given && wire === null || separate ? "ACH routing number" : "Routing number (ACH and wire)";
lines.routing = ach;
lines.wireRouting = separate ? wire : null;
lines.accountNumber = usAccount;
} else {
lines.method = "Pay by international (SWIFT) transfer, in " + currency + ".";
lines.iban = iban;
lines.bic = bic;
}
// Banks cut a long reference short without saying so, so one that does not
// fit is replaced by the customer reference, never truncated or cleaned.
const rule = limits[rail];
const fit = (s: unknown): string | null => {
if (typeof s !== "string") return null;
let v = s.trim().replace(/\s+/g, " ");
if (v === "") return null;
if (rule.upper) v = v.toUpperCase();
return v.length <= rule.max && rule.set.test(v) ? v : null;
};
const reference = fit(inv.reference) ?? fit(inv.customerReference);
if (reference === null) {
throw new Error(
"invoice.reference does not fit a " + rail + " payment reference (" + rule.max + " characters from " +
rule.text + "), and no invoice.customerReference that does was given",
);
}
lines.reference = reference;
const changed = pay.detailsChangedOn;
if (typeof changed === "string" && changed !== "") {
const then = /^\d{4}-\d{2}-\d{2}/.test(changed) ? new Date(changed.slice(0, 10) + "T00:00:00Z") : null;
const tail = " Before you pay, call us on a number you already have for us, not one from this invoice or an email, and check the details below with us.";
// UTC, so the date does not shift by a day on the engine. A date that
// does not parse still warns: a malformed date never suppresses it.
lines.warning = then !== null && !Number.isNaN(then.getTime())
? "Our bank details changed on " +
new Intl.DateTimeFormat(country === "US" ? "en-US" : "en-GB", { dateStyle: "long", timeZone: "UTC" }).format(then) +
"." + tail
: "Our bank details have changed since our last invoice." + tail;
}
return lines;
},
}); arrangements/bank-transfer-details/lines.ts /**
* What bank transfer details hands each of its arrangements: every line it may
* print, already decided. An arrangement places these and decides nothing, so
* all three print the same lines under the same conditions.
*
* A field that is `null` is a line this payer does not get. Test it in the JSX,
* `{lines.iban && …}`, so the test is compiled into a condition the engine
* settles per document.
*
* Installed by `dxcl add bank-transfer-details`, with the arrangements.
*/
export interface BankTransferDetailsLines {
/** The changed-details warning, or null. Always placed first. */
warning: string | null;
/** One sentence naming how to pay, or saying the details cannot be shown. Always present. */
method: string;
/** The account name exactly as held, or null when no details are printed. */
accountName: string | null;
/** `60-16-13`, UK rail only. */
sortCode: string | null;
/** "Routing number (ACH and wire)" or "ACH routing number", US rail only. */
routingLabel: string | null;
/** The ACH routing number, US rail only. */
routing: string | null;
/** The wire routing number, only when valid and different from the ACH one. */
wireRouting: string | null;
/** The account number, UK or US rail. */
accountNumber: string | null;
/** The IBAN in groups of four, SEPA or international rail. */
iban: string | null;
/** The BIC, SEPA (when valid) or international rail. */
bic: string | null;
/** The reference to quote, already fitted to the rail, or null when no details are printed. */
reference: string | null;
} arrangements/bank-transfer-details/stacked.arrangement.tsx import { Paragraph, type Yield } from "docxcelerate/template";
import type { BankTransferDetailsLines } from "./lines.ts";
/**
* One labelled line under another, down the text column. The default: it
* reads at any width, and nothing has to line up with anything.
*/
export const stacked = (lines: BankTransferDetailsLines): Yield => (
<>
{lines.warning && <Paragraph id="bank-transfer-warning">{lines.warning}</Paragraph>}
<Paragraph id="bank-transfer-method">{lines.method}</Paragraph>
{lines.accountName && <Paragraph id="bank-transfer-account-name">Account name: {lines.accountName}</Paragraph>}
{lines.sortCode && <Paragraph id="bank-transfer-sort-code">Sort code: {lines.sortCode}</Paragraph>}
{lines.routing && <Paragraph id="bank-transfer-routing">{lines.routingLabel}: {lines.routing}</Paragraph>}
{lines.wireRouting && (
<Paragraph id="bank-transfer-wire-routing">Wire routing number: {lines.wireRouting}</Paragraph>
)}
{lines.accountNumber && (
<Paragraph id="bank-transfer-account-number">Account number: {lines.accountNumber}</Paragraph>
)}
{lines.iban && <Paragraph id="bank-transfer-iban">IBAN: {lines.iban}</Paragraph>}
{lines.bic && <Paragraph id="bank-transfer-bic">BIC: {lines.bic}</Paragraph>}
{lines.reference && <Paragraph id="bank-transfer-reference">Payment reference: {lines.reference}</Paragraph>}
</>
); arrangements/bank-transfer-details/table.arrangement.tsx import { Cell, Paragraph, Row, Table, type Yield } from "docxcelerate/template";
import type { BankTransferDetailsLines } from "./lines.ts";
/**
* Labels in a narrow column with the values beside them. A payer copying digits
* into a banking app finds every value starting at the same place, and an IBAN
* or routing number is never broken by a label running into it.
*/
export const table = (lines: BankTransferDetailsLines): Yield => (
<>
{lines.warning && <Paragraph id="bank-transfer-warning">{lines.warning}</Paragraph>}
<Paragraph id="bank-transfer-method">{lines.method}</Paragraph>
{/* 45mm holds the longest label, "Routing number (ACH and wire)", on one line. */}
{/* On the none rail no row prints, and a row-less table is not valid DOCX. */}
{lines.accountName && <Table id="bank-transfer-details" columns={[{ width: 45 }, { width: "auto" }]}>
{lines.accountName && (
<Row id="bank-transfer-account-name">
<Cell>Account name</Cell>
<Cell>{lines.accountName}</Cell>
</Row>
)}
{lines.sortCode && (
<Row id="bank-transfer-sort-code">
<Cell>Sort code</Cell>
<Cell>{lines.sortCode}</Cell>
</Row>
)}
{lines.routing && (
<Row id="bank-transfer-routing">
<Cell>{lines.routingLabel}</Cell>
<Cell>{lines.routing}</Cell>
</Row>
)}
{lines.wireRouting && (
<Row id="bank-transfer-wire-routing">
<Cell>Wire routing number</Cell>
<Cell>{lines.wireRouting}</Cell>
</Row>
)}
{lines.accountNumber && (
<Row id="bank-transfer-account-number">
<Cell>Account number</Cell>
<Cell>{lines.accountNumber}</Cell>
</Row>
)}
{lines.iban && (
<Row id="bank-transfer-iban">
<Cell>IBAN</Cell>
<Cell>{lines.iban}</Cell>
</Row>
)}
{lines.bic && (
<Row id="bank-transfer-bic">
<Cell>BIC</Cell>
<Cell>{lines.bic}</Cell>
</Row>
)}
{lines.reference && (
<Row id="bank-transfer-reference">
<Cell>Payment reference</Cell>
<Cell>{lines.reference}</Cell>
</Row>
)}
</Table>}
</>
); arrangements/bank-transfer-details/split.arrangement.tsx import { Cell, Paragraph, Row, Table, type Yield } from "docxcelerate/template";
import type { BankTransferDetailsLines } from "./lines.ts";
/**
* Where to send the money on the left, the reference to quote ranged right. The
* payer enters the reference in a separate field from the account details, and
* on many invoices this block faces the totals.
*/
export const split = (lines: BankTransferDetailsLines): Yield => (
<>
{lines.warning && <Paragraph id="bank-transfer-warning">{lines.warning}</Paragraph>}
<Paragraph id="bank-transfer-method">{lines.method}</Paragraph>
{/* On the none rail there is nothing to put in the band. */}
{lines.accountName && <Table id="bank-transfer-details" columns={[{ width: "auto" }, { width: 60, align: "right" }]}>
<Row id="bank-transfer-details-row">
<Cell>
{lines.accountName && <Paragraph id="bank-transfer-account-name">Account name: {lines.accountName}</Paragraph>}
{lines.sortCode && <Paragraph id="bank-transfer-sort-code">Sort code: {lines.sortCode}</Paragraph>}
{lines.routing && <Paragraph id="bank-transfer-routing">{lines.routingLabel}: {lines.routing}</Paragraph>}
{lines.wireRouting && (
<Paragraph id="bank-transfer-wire-routing">Wire routing number: {lines.wireRouting}</Paragraph>
)}
{lines.accountNumber && (
<Paragraph id="bank-transfer-account-number">Account number: {lines.accountNumber}</Paragraph>
)}
{lines.iban && <Paragraph id="bank-transfer-iban">IBAN: {lines.iban}</Paragraph>}
{lines.bic && <Paragraph id="bank-transfer-bic">BIC: {lines.bic}</Paragraph>}
</Cell>
<Cell>
{lines.reference && (
<Paragraph id="bank-transfer-reference">Quote this reference: {lines.reference}</Paragraph>
)}
</Cell>
</Row>
</Table>}
</>
);