Agent Wranglers

PAY-73Recommended

Put a checked OCR reference on Swedish invoices

Give every Swedish invoice paid by bank giro a unique OCR reference with a check digit, and match incoming payments by that reference alone.

How to do it

Swedish banks check the OCR reference when the payer types it, so a wrong digit is refused at the bank. The bank giro then reports each payment with its reference, and your system can match it without a person. Build the reference from the invoice number, and add the Luhn check digit, also called the 10 modulus. Turn on the reference check for your bank giro number with your bank. Payments that arrive without a valid reference go to the unmatched list from Reconcile every payout.

Examples

Python example

def ocr_reference(invoice_number: int) -> str:
    digits = str(invoice_number)
    total = 0
    for position, char in enumerate(reversed(digits)):
        value = int(char) * (2 if position % 2 == 0 else 1)
        total += value - 9 if value > 9 else value
    return digits + str((10 - total % 10) % 10)

JavaScript example

export function ocrReference(invoiceNumber) {
  const digits = String(invoiceNumber)
  let total = 0
  for (let position = 0; position < digits.length; position++) {
    const value = Number(digits[digits.length - 1 - position]) * (position % 2 === 0 ? 2 : 1)
    total += value > 9 ? value - 9 : value
  }
  return digits + String((10 - (total % 10)) % 10)
}

How to check

A unit test builds references for known invoice numbers and compares them with published examples. A test payment file with a reference that fails the check digit lands on the unmatched list.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/payments/nordic-payments/ocr-reference">Agent Wranglers payments and billing playbook</a>. In code you write, add a comment next to it with the same link.