Avatar
1
tvd12 Enlightened
tvd12 Enlightened
Code python tạo mã QR theo chuẩn TCCS 03:2018/NHNNVN
Code python tạo mã QR theo chuẩn TCCS 03:2018/NHNNVN
  • Answer
python qrcode vietqr
Remain: 5
1 Answer
Avatar
tvd12 Enlightened
tvd12 Enlightened
import qrcode

def generate_check_sum(text):
    crc = 0xFFFF  # initial value
    polynomial = 0x1021  # 0001 0000 0010 0001 (0, 5, 12)
    bytes_ = text.encode("utf-8")
    for b in bytes_:
        for i in range(8):
            bit = ((b >> (7 - i) & 1) == 1)
            c15 = ((crc >> 15 & 1) == 1)
            crc <<= 1
            if c15 ^ bit:
                crc ^= polynomial
    return format(crc & 0xFFFF, "x")

bank_code = "vietinbank"
bank_account = "1013772070"
amount = "100000"
message = "Hello"
bank_id_by_code = {"vcb": "970436", "vietinbank": "970415"}
bank_id = bank_id_by_code.get(bank_code)
part12_builder = (
    "00" + "{:02d}".format(len(bank_id)) + bank_id + "01" + "{:02d}".format(len(bank_account)) + bank_account
)
part11_builder = (
    "0010A000000727"
    + "01"
    + "{:02d}".format(len(part12_builder))
    + part12_builder
    + "0208QRIBFTTA"
)
part1_builder = "38" + "{:02d}".format(len(part11_builder)) + part11_builder
part21_builder = "08" + "{:02d}".format(len(message)) + message
part2 = (
    "5303704"
    + "54"
    + "{:02d}".format(len(amount))
    + amount
    + "5802VN"
    + "62"
    + "{:02d}".format(len(part21_builder))
    + part21_builder
)
builder = "000201" + "010212" + part1_builder + part2 + "6304"
qrcode_content = builder + generate_check_sum(builder).upper()
print(qrcode_content)
img = qrcode.make(qrcode_content) 
img.save("my_qrcode.png") 
  • 1
  • Reply
thank anh  –  toilahtc 1682650323000