Card numbers for loyalty and gift cards: Luhn check digits, sequential vs random, and the CSV the printer needs
If you are building a loyalty, gift or membership card system, you will have to decide what number goes on each card before anything is printed. That number ends up in your database, in a barcode, maybe on a magnetic str
If you are building a loyalty, gift or membership card system, you will have to decide what number goes on each card before anything is printed. That number ends up in your database, in a barcode, maybe on a magnetic stripe, and on the face of the card where staff type it in by hand. A few choices made early save a lot of trouble later.
Sequential or random?
Sequential numbers (000001, 000002, ...) are easy to print, easy to check for gaps, and fine for membership cards where the number is only an identifier and the card has no value.
Random numbers are better when the card carries value (gift cards, prepaid balances, loyalty points that can be redeemed). If numbers are sequential, someone holding card 000500 can guess that 000501 exists. Use a cryptographically secure generator, not Math.random() or Python's random module.
A common middle ground is a short fixed prefix for the programme, a random body, and a check digit at the end.
Add a check digit
Staff will key numbers in by hand at a till or front desk. A check digit catches most typing mistakes before they hit your database. The usual choice is the Luhn algorithm (the same one used on payment card numbers). It catches every single-digit error and most swaps of two adjacent digits (09 and 90 is the one swap it misses).
Python:
def luhn_check_digit(payload: str) -> str:
total = 0
for i, ch in enumerate(reversed(payload)):
d = int(ch)
if i % 2 == 0: # these get doubled once the check digit is appended
d *= 2
if d > 9:
d -= 9
total += d
return str((10 - total % 10) % 10)
def luhn_valid(number: str) -> bool:
return luhn_check_digit(number[:-1]) == number[-1]
assert luhn_check_digit("7992739871") == "3"
assert luhn_valid("79927398713")
JavaScript:
function luhnCheckDigit(payload) {
let total = 0;
[...payload].reverse().forEach((ch, i) => {
let d = Number(ch);
if (i % 2 === 0) { d *= 2; if (d > 9) d -= 9; }
total += d;
});
return String((10 - (total % 10)) % 10);
}
luhnCheckDigit("7992739871"); // "3"
Generating a random 16-digit number with a prefix:
import secrets
def random_card_number(prefix: str, length: int = 16) -> str:
body_len = length - len(prefix) - 1
body = prefix + "".join(secrets.choice("0123456789") for _ in range(body_len))
return body + luhn_check_digit(body)
Check each new number against the ones you already issued before you add it to the print file. With 11 random digits a collision is rare, but it only takes one duplicate to cause a support ticket.
Don't start a loyalty number with a prefix that looks like a real payment card range (4 or 5 followed by 15 digits is the obvious one). Some POS systems and card readers will try to treat it as a bank card.
Length and readability
- 12 to 16 digits is enough for most programmes and still fits one line on a CR80 card.
- Print it in groups of four (
6001 5966 4683 4484) so people can read it back. Store it without spaces. - Avoid letters if staff key it in. If you need letters, drop the ones that look like digits (O, I, L, S, B).
Barcodes
If the number is also printed as a barcode, keep the barcode content identical to the printed number, without spaces. Code 128 is the usual choice for variable numbers on cards because it is compact and handles any length. EAN-13 only works if your number is exactly 13 digits and follows its own check digit rule, which is different from Luhn. If you want a QR code instead, encode the number or a URL with the number in the path.
The data file for the printer
The printer needs one row per card, in the order the cards should be printed. A plain CSV is the safest format:
sequence,card_number,barcode
1,6001596646834484,6001596646834484
2,6001207718395525,6001207718395525
Things that go wrong:
- Leading zeros vanish if someone opens the CSV in Excel and saves it again. Send the file as generated, or quote the fields.
-
Long numbers turn into scientific notation in spreadsheets (
6.00159E+15). Same fix. - Duplicates. Run a uniqueness check on the file before you send it.
- No sequence column. Without one, nobody can easily tell if a card is missing from a pack.
Ask the printer to send back a proof of the first few and last few cards, and check those numbers against your file.
I work at PlasticCard.nz, a plastic card supplier in New Zealand. We have longer notes on barcodes, numbering and magnetic stripes and on variable data printing, and these questions come up most with loyalty and gift cards. Corrections from people who run card programmes at scale are welcome in the comments.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.