The Swiss QR-bill
In Switzerland a bill comes with a payment slip at the bottom of the page – and since 2020 that slip is the QR-bill (the old orange and red slips have been gone since autumn 2022). Its QR code holds everything a bank needs: which account, how much, in which currency, from whom, and a reference that tells the business which invoice was paid. A banking app scans it, and nobody types a 27-digit number any more.
Two gems, both pure Ruby: qr-bills writes QR-bills, swiss_qr reads them back and checks every rule of the standard; it finds the code in an image or a PDF with qr_reader. The bike shop and its customer are made up, and the account numbers are the test numbers from the QR-bill's specification – no money moves here.
install_gem "qr-bills"
require "qr-bills"
params = QRBills.get_qr_params
params[:bill_type] = QRBills.get_qrbill_with_qr_reference_type
params[:output_params][:format] = "qrcode" # just the code, no HTML
params[:qrcode_format] = "svg"
bill = params[:bill_params]
bill[:amount] = 84.50
bill[:currency] = "CHF"
bill[:reference_type] = "QRR"
bill[:reference] = "21 00000 00003 13947 14300 09017"
bill[:additionally_information] = "Invoice 2026-042, chain and brake pads"
bill[:creditor][:iban] = "CH44 3199 9123 0008 8901 2"
bill[:creditor][:address].merge!(
name: "Velo Speck GmbH", street_name: "Musterstrasse", building_number: "1",
postal_code: "8000", town: "Seldwyla", country: "CH"
)
bill[:debtor][:address].merge!(
name: "Pia Muster", street_name: "Beispielweg", building_number: "7",
postal_code: "8000", town: "Seldwyla", country: "CH"
)
svg = QRBills.generate(params)[:output]
show_image svg, alt: "The bill's QR code, with a Swiss cross in the middle"
download_file svg, "qr-code.svg"What happened?
QRBills.get_qr_paramshands out a Hash with every field, most of them empty; we fill them in.billis the part inside,params[:bill_params]– changing it changesparamstoo, it is the same Hash.- The bill type: with a QR reference (
QRR), with a creditor reference (SCOR, starts with RF) or without a reference (NON). - A QR-IBAN – its 5th to 9th digits are between 30000 and 31999 – is the account for bills with a QR reference. The reference has 27 digits, the last one a check digit worked out from the others. It tells the business which invoice was paid.
- The IBAN goes in
bill[:creditor][:iban]: the address has a field for it as well, but the gem reads it from here. QRBills.generatereturns a Hash; its:outputis the SVG as a String.show_imageshows it,download_fileoffers it as a file.
And what is in the code? Plain text, one field per line:
payload = QRGenerator.build_payload(bill)
puts payloadThat is the whole secret: SPC (Swiss Payments Code), version 0200, character set 1 (UTF-8), then the account, the creditor with address type S (structured: street and number in fields of their own), seven empty lines (kept for an "ultimate creditor" that is not allowed yet), amount, currency, the debtor, reference type and reference, the message and EPD – End Payment Data. The lines are separated by CR LF, as in e-mails.
On paper the QR code sits in a payment slip of a fixed size: the bottom 105 mm of an A4 page, on the left the receipt that the payer keeps, on the right the payment part with the code, 46 mm square. Prawn (lesson 67) draws it:
install_gem "prawn"
require "prawn"
params[:qrcode_format] = "png"
File.binwrite("qr.png", QRBills.generate(params)[:output].to_blob)
mm = 72 / 25.4 # PDF counts in points: 1 mm is 2.83 points
to = bill[:creditor][:address]
by = bill[:debtor][:address]
payable_to = [bill[:creditor][:iban], to[:name], "#{to[:street_name]} #{to[:building_number]}",
"#{to[:postal_code]} #{to[:town]}"]
payable_by = [by[:name], "#{by[:street_name]} #{by[:building_number]}",
"#{by[:postal_code]} #{by[:town]}"]
amount = format("%.2f", bill[:amount])
Prawn::Document.generate("invoice.pdf", page_size: "A4", margin: 0) do
text_box "Velo Speck GmbH\nInvoice 2026-042", at: [20 * mm, 277 * mm], size: 14
dash(3)
stroke_horizontal_line 0, bounds.width, at: 105 * mm # the bill: the bottom 105 mm
stroke_vertical_line 0, 105 * mm, at: 62 * mm # receipt | payment part
undash
field = lambda do |x, y, heading, lines|
text_box heading, at: [x * mm, y * mm], size: 6, style: :bold
text_box lines.join("\n"), at: [x * mm, (y - 3) * mm], size: 8
end
text_box "Receipt", at: [5 * mm, 100 * mm], size: 11, style: :bold
field.(5, 93, "Account / Payable to", payable_to)
field.(5, 70, "Reference", [bill[:reference]])
field.(5, 60, "Payable by", payable_by)
text_box "Payment part", at: [67 * mm, 100 * mm], size: 11, style: :bold
image "qr.png", at: [67 * mm, 88 * mm], width: 46 * mm
field.(118, 100, "Account / Payable to", payable_to)
field.(118, 77, "Reference", [bill[:reference]])
field.(118, 67, "Additional information", [bill[:additionally_information]])
field.(118, 55, "Payable by", payable_by)
[5, 67].each do |x| # currency and amount are on both parts
field.(x, 37, "Currency", [bill[:currency]])
field.(x + 15, 37, "Amount", [amount])
end
end
show_pdf "invoice.pdf"The coordinates are millimetres times mm; in PDF, y counts up from the bottom edge, so 105 * mm is the line to tear along. field is a small lambda: a heading in bold 6 point, the lines in 8 point below it – the sizes the standard asks for. This time qr-bills hands out the code as a PNG, because Prawn places PNG and JPEG images. It is a ChunkyPNG::Image as in lesson 50. The real standard has more rules – fonts, corner marks for an empty amount, the scissors symbol – and qr-bills can also write the whole bill as HTML (output_params[:format] = "html").
Now the other way round: someone receives invoice.pdf and wants to know what to pay. swiss_qr finds the code with qr_reader, decodes it and takes the text apart:
install_gem "swiss_qr"
install_gem "qr_reader" # finds and decodes the QR code
require "swiss_qr"
paid = SwissQr.read("invoice.pdf")
puts paid.creditor.lines
puts paid.formatted_iban, paid.qr_iban?
puts "#{paid.currency} #{paid.formatted_amount}"
puts paid.formatted_reference
puts paid.unstructured_message
paid.debtor.nameSwissQr.read takes a file name, an IO such as StringIO.new(bytes) or an image like ChunkyPNG's, and returns the first QR-bill it finds as a SwissQr::Bill: an immutable object (a Data, lesson 113) with the amount as a BigDecimal (lesson 64) and the addresses as objects of their own. Other QR codes on the page – a link to a web shop, say – are skipped.
swiss_qr does not just read, it checks every rule too: the check digits of the IBAN and of the reference, the lengths, the character set. A typo in the reference is caught:
typo = payload.sub("09017", "09071") # two digits swapped
begin
SwissQr.parse(typo)
rescue SwissQr::InvalidPayload => e
puts e.message
end
SwissQr.valid?(payload)gem install qr-bills swiss_qr qr_reader. swiss_qr comes with a command as well: swiss_qr invoice.pdf prints the bill as JSON. For real bills you need an IBAN or QR-IBAN of your own from your bank; accounting software usually builds the QR reference from a customer and an invoice number, and QRBills.create_creditor_reference turns a number of your own into an RF reference.
CH58 0079 1123 0008 8901 2. An ordinary IBAN goes without a QR reference, so take the bill type without a reference (QRBills.get_qrbill_without_reference_type, reference_type "NON", the reference stays empty). Make a QR-bill for CHF 12.30 to Kaz Muster, Bahnhofstrasse 2, 8000 Seldwyla, show its QR code as a PNG with show_image and read it back with SwissQr.read. Leave the bill you read as the last value, or print its IBAN and amount.# Glace Fuchs: a QR-bill without a reference for CHF 12.30,
# shown as a PNG and read back with SwissQr.read