So you just copy pasted the checkdigit calculation?! Did you realize you could ask the barcodes library to do this for you? (and even store it with the checksum instead solving all your problems).