JourneyJoker/payment/voucher/helpers.py

18 lines
539 B
Python
Raw Permalink Normal View History

2021-05-22 06:38:30 +00:00
from dbsettings.functions import getValue
from random import SystemRandom
2022-10-28 14:35:26 +00:00
import baluhn
2021-05-22 06:38:30 +00:00
def generate_voucher_code(prefix=getValue("payment.voucher.prefix", "9011"), length=getValue("payment.voucher.length", 16)):
if length <= len(str(prefix)):
raise ValueError("Voucher code length must be longer than its prefix!")
length_randpart = length - len(str(prefix)) - 1
base_code = prefix
for i in range(length_randpart):
base_code += str(SystemRandom().randint(0, 9))
2022-10-28 14:35:26 +00:00
return baluhn.generate(base_code)