Skip to content

Instantly share code, notes, and snippets.

@ptm108
Created February 9, 2021 06:29
Show Gist options
  • Save ptm108/d92deb094e1af4c10d5cd8250e3ac66f to your computer and use it in GitHub Desktop.
Save ptm108/d92deb094e1af4c10d5cd8250e3ac66f to your computer and use it in GitHub Desktop.
SGQR payload generator
from pycrc.algorithms import Crc
def generate_qr_string(uen, is_uen, company_name, transaction_id, amount, editable, expiry):
merchant_values = [
'00' + str(len('SG.PAYNOW')).zfill(2) + 'SG.PAYNOW',
'01' + str(len(is_uen)).zfill(2) + is_uen, # 0 for mobile, 2 for UEN
'02' + str(len(uen)).zfill(2) + uen, # uen / mobile number
'03' + str(len(editable)).zfill(2) + editable, # 1 = editable, 0 = not editable
'04' + str(len(expiry)).zfill(2) + expiry, # YYYYMMDD
]
merchant_info = ''.join(merchant_values)
additional_value = ['01' + str(len(transaction_id)).zfill(2) + transaction_id] # bill/ref number
additional_info = ''.join(additional_value)
values = [
'00' + str(len('01')).zfill(2) + '01',
'01' + str(len('12')).zfill(2) + '12',
'26' + str(len(merchant_info)).zfill(2) + merchant_info,
'52' + str(len('0000')).zfill(2) + '0000', # merchant category code, not used
'53' + str(len('702')).zfill(2) + '702', # SGD currency
'54' + str(len(amount)).zfill(2) + amount, # transaction amount
'58' + str(len('SG')).zfill(2) + 'SG', # 2 letter country code
'59' + str(len(company_name)).zfill(2) + company_name, # company name
'60' + str(len('Singapore')).zfill(2) + 'Singapore', # Merchant City
'62' + str(len(additional_info)).zfill(2) + additional_info,
'6304'
]
qr_string = ''.join(values)
# Sample code from https://pycrc.org
crc = Crc(width=16, poly=0x1021,
reflect_in=False, xor_in=0xffff,
reflect_out=False, xor_out=0x0000)
my_crc = crc.bit_by_bit_fast(qr_string) # calculate the CRC, using the bit-by-bit-fast algorithm.
crc_data_upper = ('{:04X}'.format(my_crc))
final_string = qr_string + crc_data_upper
return final_string
# end def
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment