qris statis to dinamis
Script Node.js untuk mengubah QRIS Statis menjadi QRIS Dinamis + generate QR Code PNG
29 September 20261102 file
qris.jsjavascript · 81 baris
// main.jsimport QRCode from 'qrcode';class Parser { constructor() { this.qris = ""; } parseQris(qris, amount) { const amountLength = amount.length; const tag54 = `54${String(amountLength).padStart(2, "0")}${amount}`; qris = this.removeTag54(qris); const tag63Position = qris.indexOf("6304"); if (tag63Position === -1) throw new Error("tag 63 not found"); const modifiedPayload = qris.slice(0, tag63Position) + tag54 + qris.slice(tag63Position); const payloadWithoutCrc = modifiedPayload.slice(0, -4); const newCrc = this.calculateCrc(payloadWithoutCrc); this.qris = `${payloadWithoutCrc}${newCrc}`; return this.qris; } removeTag54(qris) { let position = 0; let result = ""; while (position < qris.length) { if (position + 2 > qris.length) throw new Error("invalid qris"); const tag = qris.substring(position, position + 2); position += 2; if (position + 2 > qris.length) throw new Error("invalid qris"); const lengthStr = qris.substring(position, position + 2); const length = parseInt(lengthStr, 10); if (Number.isNaN(length)) throw new Error("invalid qris"); position += 2; if (position + length > qris.length) throw new Error("invalid qris"); const value = qris.substring(position, position + length); position += length; if (tag !== "54") result += tag + String(length).padStart(2, "0") + value; } return result; } calculateCrc(payload) { let crc = 0xffff; const polynomial = 0x1021; for (let i = 0; i < payload.length; i++) { crc ^= payload.charCodeAt(i) << 8; for (let j = 0; j < 8; j++) { crc = (crc & 0x8000) ? ((crc << 1) ^ polynomial) & 0xffff : (crc << 1) & 0xffff; } } return crc.toString(16).toUpperCase().padStart(4, "0"); }}// — pakai di sini —const parser = new Parser();const result = parser.parseQris( "PASTE_QRIS_STATIS_DISINI", // ganti dengan QRIS statis "50000" // ganti dengan nominal (contoh: 50000 = Rp 50.000));// Simpan sebagai file PNGawait QRCode.toFile('qris-dinamis.png', result, { errorCorrectionLevel: 'M', width: 512, margin: 2});console.log('QR saved: qris-dinamis.png');README.mdmarkdown · 199 baris
Komentar
Masuk untuk ikut berkomentar.
Belum ada komentar.