Avatar
0
tvd12 Enlightened
tvd12 Enlightened
Code tạo QR cho Việt Nam Bank viết bằng typescript
Code tạo QR cho Việt Nam Bank viết bằng typescript
  • Answer
qr
Remain: 5
1 Answer
Avatar
tvd12 Enlightened
tvd12 Enlightened
export class QrCodeHelper { 
    async gennerateQRCode(bankCode: string, bankAccount: string, amount: string, message: string) {
        let bankId: string | undefined = bankInfo.find(x => x.code == bankCode)?.bin || '';

        let path12StringBuilder: StringBuilder = new StringBuilder()
            .append('00')
            .append(bankId.length.toString().padStart(2, '0'))
            .append(bankId)
            .append('01')
            .append(bankAccount.length.toString().padStart(2, '0'))
            .append(bankAccount);
        
        let path11StringBuilder: StringBuilder = new StringBuilder()
            .append('0010A000000727')
            .append('01')
            .append(path12StringBuilder.length.toString().padStart(2, '0'))
            .appendStringBuilder(path12StringBuilder)
            .append('0208QRIBFTTA');
        
        let path1StringBuilder: StringBuilder = new StringBuilder()
            .append('38')
            .append(path11StringBuilder.length.toString().padStart(2, '0'))
            .appendStringBuilder(path11StringBuilder);
        
        let path21StringBuilder: StringBuilder = new StringBuilder()
            .append('08')
            .append(message.length.toString().padStart(2, '0'))
            .append(message);
        
        let path2StringBuilder: StringBuilder = new StringBuilder()
            .append('5303704')
            .append('54')
            .append(amount.length.toString().padStart(2, '0'))
            .append(amount)
            .append('5802VN')
            .append('62')
            .append(path21StringBuilder.length.toString().padStart(2, '0'))
            .appendStringBuilder(path21StringBuilder);
        
        let stringBuilder: StringBuilder = new StringBuilder()
            .append('000201')
            .append('010212')
            .appendStringBuilder(path1StringBuilder)
            .appendStringBuilder(path2StringBuilder)
            .append('6304');
        
        let qrCodeContent = stringBuilder
            .append(this.generateCheckSum(stringBuilder.toString()).toUpperCase())
            .toString();
        
        console.log(qrCodeContent);
        
        let qrCode = await QRCode.toDataURL(qrCodeContent);
        
        return (qrCode);
    }    

    generateCheckSum(text: string): string {
        let crc: number = 0xFFFF;
        let polynomial: number = 0x1021;
        let bytes: Buffer = Buffer.from(text);

        for (let b of bytes) {
            for (let i = 0; i < 8; i++) {
                let bit: boolean = ((b >> (7 - i) & 1) === 1);
                let c15: boolean = ((crc >> 15 & 1) === 1);
                crc <<= 1;
                if (c15 !== bit) crc ^= polynomial;
            }
        }
        
        return (crc & 0xFFFF).toString(16);
    }
}
  • 0
  • Reply
cho e hỏi về cái class: StringBuilder

a viết ntn đó ạ @@

 –  Tu Zitt 1692182849000
Em có thể tham khảo của bạn này nhé: https://github.com/dothanhtungoffice/qr-code-application/blob/master/src/helpers/string-builder.ts  –  tvd12 1692238594000