taler-typescript-core

Wallet core logic and WebUIs for various components
Log | Files | Refs | Submodules | README | LICENSE

commit 5d5aa758e7a1e9be5d664604a64a8de1c67023a3
parent c46fe5408a83b0855b98cc92fbd5e2d396d828b9
Author: Florian Dold <dold@taler.net>
Date:   Wed, 25 Nov 2026 20:06:01 +0100

web-util: render Swiss payment QR codes with the SIX recognition mark

Size the mark and quiet zone against the encoded matrix. Give banking
QR codes UTF-8 encoding and level M error correction, and expose the
same frame to the merchant and wallet interfaces.

Issue: https://bugs.taler.net/n/12108

Diffstat:
Mpackages/taler-merchant-webui/src/screens/KycAuthInstructionsScreen.tsx | 2+-
Mpackages/taler-merchant-webui/src/screens/screens.test.tsx | 18++++++++++++++----
Mpackages/web-util/src/components/TalerQrCode.test.ts | 11+++++++++--
Mpackages/web-util/src/components/TalerQrCode.tsx | 138+++++++++++++++++++++++++++++++++++++++++++++++++------------------------------
4 files changed, 110 insertions(+), 59 deletions(-)

diff --git a/packages/taler-merchant-webui/src/screens/KycAuthInstructionsScreen.tsx b/packages/taler-merchant-webui/src/screens/KycAuthInstructionsScreen.tsx @@ -768,7 +768,7 @@ export function KycAuthInstructionsScreen({ <div class="space-y-4 pt-1"> {qrCodes.map((q, qIdx) => { const variant = - q.type === "spc" ? "swiss" : "plain"; + q.type === "spc" ? "swiss" : "epc"; const label = q.type === "spc" ? t`Swiss QR-bill` diff --git a/packages/taler-merchant-webui/src/screens/screens.test.tsx b/packages/taler-merchant-webui/src/screens/screens.test.tsx @@ -8888,11 +8888,21 @@ test("the Swiss QR component uses the proportional SIX recognition mark", () => const frame = container.firstElementChild?.firstElementChild as SVGSVGElement; assert.strictEqual(frame.getAttribute("width"), "360"); - assert.strictEqual(frame.getAttribute("viewBox"), "0 0 100 100"); - const cross = frame.querySelector(".swiss-qr-cross") as SVGSVGElement | null; + // This short payload fits in version 1 (21 modules). The white margin + // around the 46-unit matrix must still be at least four modules wide. + const quietZone = (4 * 46) / 21; + assert.deepEqual(frame.getAttribute("viewBox")?.split(" ").map(Number), [ + -quietZone, -quietZone, 46 + 2 * quietZone, 46 + 2 * quietZone, + ]); + const matrix = frame.querySelector("image"); + assert.strictEqual(matrix?.getAttribute("width"), "46"); + assert.strictEqual(matrix?.getAttribute("height"), "46"); + const cross = frame.querySelector(".swiss-qr-cross"); assert.ok(cross); - assert.strictEqual(cross.getAttribute("width"), "15.217"); - assert.strictEqual(cross.getAttribute("height"), "15.217"); + assert.strictEqual(cross.getAttribute("x"), "19.5"); + assert.strictEqual(cross.getAttribute("y"), "19.5"); + assert.strictEqual(cross.getAttribute("width"), "7"); + assert.strictEqual(cross.getAttribute("height"), "7"); render(null, container); document.body.removeChild(container); diff --git a/packages/web-util/src/components/TalerQrCode.test.ts b/packages/web-util/src/components/TalerQrCode.test.ts @@ -18,8 +18,7 @@ import assert from "node:assert/strict"; import test from "node:test"; import { Window } from "happy-dom"; import { h, render } from "preact"; -import { generateQrDataUrl } from "./TalerQrCode.js"; -import { TalerQrCode } from "./TalerQrCode.js"; +import { generateQrDataUrl, TalerQrCode } from "@gnu-taler/web-util/browser"; function installDom(): Window { const window = new Window({ url: "https://bank.example/" }); @@ -51,6 +50,14 @@ test("QR generation reports oversized input as an error", () => { assert.deepEqual(generateQrDataUrl("x".repeat(100_000)), { type: "error" }); }); +test("the largest Swiss symbol holds 997 UTF-8 bytes at level M in version 25", () => { + const text = "é".repeat(498) + "x"; + const result = generateQrDataUrl(text, "swiss"); + assert.equal(result.type, "success"); + assert.equal(result.moduleCount, 117); + assert.deepEqual(generateQrDataUrl(text + "x", "swiss"), { type: "error" }); +}); + test("the shared component renders the Taler frame and reports copied URIs", async () => { const window = installDom(); const container = document.createElement("div"); diff --git a/packages/web-util/src/components/TalerQrCode.tsx b/packages/web-util/src/components/TalerQrCode.tsx @@ -16,6 +16,7 @@ import { VNode, h } from "preact"; import qrcode from "qrcode-generator"; +import swissQrMark from "@assets/svg/swiss-qr-flag.svg"; export interface TalerQrCodeLabels { copy: string; @@ -23,12 +24,21 @@ export interface TalerQrCodeLabels { generationFailed: string; } +type QrCodeVariant = "taler" | "swiss" | "epc" | "plain"; + +interface GeneratedQrCode { + type: "success"; + variant: QrCodeVariant; + url: string; + moduleCount: number; +} + export interface TalerQrCodeProps { url: string; copyUrl?: string; size?: number; alt?: string; - variant?: "taler" | "swiss" | "plain"; + variant?: QrCodeVariant; labels?: Partial<TalerQrCodeLabels>; } @@ -53,17 +63,28 @@ const TALER_QR_LINE_COLORS = Array.from({ length: 41 }, (_, index) => { export function generateQrDataUrl( text: string, -): { type: "success"; url: string } | { type: "error" } { + variant: QrCodeVariant = "taler", +): GeneratedQrCode | { type: "error" } { try { - const qr = qrcode(0, "H"); - qr.addData(text, "Byte"); + // SIX IG QR-bill §6.2 caps a binary, level-M symbol at version 25. + const maxBytes = variant === "swiss" ? 997 : 1273; + if (text.length > maxBytes) return { type: "error" }; + const bytes = new TextEncoder().encode(text); + if (bytes.length > maxBytes) return { type: "error" }; + const banking = variant === "swiss" || variant === "epc"; + const qr = qrcode(0, banking ? "M" : "H"); + // qrcode-generator's Byte mode takes one character per byte. + qr.addData(String.fromCharCode(...bytes), "Byte"); qr.make(); const svgTag = qr.createSvgTag({ scalable: true, - margin: 1, + // The bank frame supplies the entire quiet zone outside the matrix. + margin: banking ? 0 : 1, }); return { type: "success", + variant, + moduleCount: qr.getModuleCount(), url: `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svgTag)}`, }; } catch { @@ -235,60 +256,82 @@ function TalerFrame({ ); } -function StandardFrame({ - qrUrl, +export function QrCodeFrame({ + code, size, alt, - swiss, }: { - qrUrl: string; + code: GeneratedQrCode; size: number; alt: string; - swiss: boolean; }): VNode { + const { variant } = code; + if (variant === "taler") { + return <TalerFrame qrUrl={code.url} size={size} alt={alt} />; + } + if (variant === "plain") { + return ( + <svg + data-taler-qr-code + class="h-auto max-w-full" + width={size} + height={size} + viewBox="0 0 100 100" + role="img" + aria-label={alt} + > + <title>{alt}</title> + <rect width="100" height="100" rx="8" fill="#f3f4f6" stroke="#d1d5db" /> + <rect x="3" y="3" width="94" height="94" rx="5" fill="#ffffff" /> + <image href={code.url} x="5" y="5" width="90" height="90" /> + </svg> + ); + } + // Use the SIX reference dimensions as SVG units. Keep four modules clear + // even on sparse codes where that exceeds the recommended 5 mm margin. + const matrixSize = 46; + const quietZone = Math.max(5, (4 * matrixSize) / code.moduleCount); + const sizeWithBorder = matrixSize + 2 * quietZone; return ( <svg data-taler-qr-code - class="h-auto max-w-full" + class={ + variant === "swiss" + ? "h-auto max-w-full print:w-[var(--swiss-qr-size)] print:max-w-none" + : "h-auto max-w-full" + } + style={ + variant === "swiss" + ? { "--swiss-qr-size": `${sizeWithBorder}mm` } + : undefined + } width={size} height={size} - viewBox="0 0 100 100" + viewBox={`${-quietZone} ${-quietZone} ${sizeWithBorder} ${sizeWithBorder}`} role="img" aria-label={alt} > <title>{alt}</title> - <rect width="100" height="100" rx="8" fill="#f3f4f6" stroke="#d1d5db" /> - <rect x="3" y="3" width="94" height="94" rx="5" fill="#ffffff" /> - <image - href={qrUrl} - x="5" - y="5" - width="90" - height="90" - preserveAspectRatio="xMidYMid meet" + <rect + x={-quietZone} + y={-quietZone} + width={sizeWithBorder} + height={sizeWithBorder} + fill="#fff" /> - {swiss ? ( - <svg + <image href={code.url} width={matrixSize} height={matrixSize} /> + {variant === "swiss" && ( + <image class="swiss-qr-cross" - x="42.3915" - y="42.3915" - width="15.217" - height="15.217" - viewBox="0 0 19.8 19.8" + data-testid="swiss-payment-cross" + href={swissQrMark} + x="19.5" + y="19.5" + width="7" + height="7" aria-hidden="true" - > - <polygon points="18.3,0.7 1.6,0.7 0.7,0.7 0.7,1.6 0.7,18.3 0.7,19.1 1.6,19.1 18.3,19.1 19.1,19.1 19.1,18.3 19.1,1.6 19.1,0.7" /> - <rect x="8.3" y="4" width="3.3" height="11" fill="#ffffff" /> - <rect x="4.4" y="7.9" width="11" height="3.3" fill="#ffffff" /> - <polygon - points="0.7,1.6 0.7,18.3 0.7,19.1 1.6,19.1 18.3,19.1 19.1,19.1 19.1,18.3 19.1,1.6 19.1,0.7 18.3,0.7 1.6,0.7 0.7,0.7" - fill="none" - stroke="#ffffff" - stroke-width="1.4357" - stroke-miterlimit="10" - /> - </svg> - ) : undefined} + /> + )} </svg> ); } @@ -302,7 +345,7 @@ export function TalerQrCode({ labels: labelOverrides, }: TalerQrCodeProps): VNode { const labels = { ...DEFAULT_LABELS, ...labelOverrides }; - const qrResult = generateQrDataUrl(url); + const qrResult = generateQrDataUrl(url, variant); const textToCopy = copyUrl || url; const handleCopy = async (button: HTMLButtonElement): Promise<void> => { @@ -313,16 +356,7 @@ export function TalerQrCode({ return ( <div class="inline-flex max-w-full flex-col items-center gap-4"> {qrResult.type === "success" ? ( - variant === "taler" ? ( - <TalerFrame qrUrl={qrResult.url} size={size} alt={alt} /> - ) : ( - <StandardFrame - qrUrl={qrResult.url} - size={size} - alt={alt} - swiss={variant === "swiss"} - /> - ) + <QrCodeFrame code={qrResult} size={size} alt={alt} /> ) : ( <svg data-taler-qr-code