Reference generation with valid checksum #429
Replies: 1 comment 1 reply
-
Hi @stoem 👋 Thank you for trying to improve SwissQRBill. I think you may have used a wrong function to calculate the checksum. SwissQRBill provides the function The correct checksum for the QR Reference I have verified that using various different online generators that all mark the checksum
This is what const checksum = calculateQRReferenceChecksum("00000000000000000000012345"); // -> 7 The function function calculateLuhnChecksum(number) {
let sum = 0;
let shouldDouble = false; // Start with the second to last digit
for (let i = number.length - 1; i >= 0; i--) {
let digit = parseInt(number.charAt(i), 10);
if (shouldDouble) {
digit *= 2;
if (digit > 9) {
digit -= 9;
}
}
sum += digit;
shouldDouble = !shouldDouble;
}
// Calculate the checksum digit that makes the sum a multiple of 10
let checksum = (10 - (sum % 10)) % 10;
return checksum;
}
calculateLuhnChecksum("00000000000000000000012345") // -> 9 The |
Beta Was this translation helpful? Give feedback.
-
I've had some real problems getting the 27 digit long reference I created validated within SwissQRBill.
Our system generates a normal invoice number such as 12345 and I simply padded this with leading zeros (up to 26 digits), then used the following function (which follows the Luhn or Modulus 10 algorithm) to generate the checksum.
However this failed against the mod10 validation in this project. I asked ChatGPT about it and it told me that
I asked ChatGPT to write me an input function that validates against this project. Here's what it came up with (and this seems to work for my purposes):
I then use this function to feed in my invoice number such as 12345 and get a reference that validates.
Maybe this helps someone.
Beta Was this translation helpful? Give feedback.
All reactions