UNPKG

dynamic-vietqr

Version:
88 lines (87 loc) 3.21 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.VietQr = void 0; const crc16ccitt_1 = __importDefault(require("crc/crc16ccitt")); class VietQr { constructor(accountOrCardNumber, bnbId) { this.accountOrCardNumber = accountOrCardNumber; this.bnbId = bnbId; } formatString(input) { const length = input.length; if (!isNaN(length)) { const formattedLength = length < 10 ? `0${length}${input}` : `${length}${input}`; return formattedLength; } return input; } dynamicIBFTToAccount(amount, message) { return this.qrGeneration({ amount, service: "QRIBFTTA", message, dynamic: true, }); } dynamicIBFTToCard(amount, message) { return this.qrGeneration({ amount, service: "QRIBFTTC", message, dynamic: true, }); } staticIBFTToAccount() { return this.qrGeneration({ service: "QRIBFTTA", dynamic: false }); } staticIBFTToCard() { return this.qrGeneration({ service: "QRIBFTTC", dynamic: false }); } qrGeneration({ amount, service, message, dynamic, }) { const payloadFormatIndicator = "000201"; let point = "11"; if (dynamic) { point = "12"; } const pointofInitiationMethod = `01${this.formatString(point)}`; const GUID = `00${this.formatString("A000000727")}`; const bnbIdCode = `00${this.formatString(this.bnbId)}`; const account = `01${this.formatString(this.accountOrCardNumber)}`; const paymentNetworkSpecific = `01${this.formatString(bnbIdCode + account)}`; const serviceCode = `02${this.formatString(service)}`; const merchantAccountInformation = `38${this.formatString(GUID + paymentNetworkSpecific + serviceCode)}`; const transactionCurrency = `53${this.formatString("704")}`; let transactionAmount = ""; if (amount) { transactionAmount = `54${this.formatString(amount)}`; } const countryCode = `58${this.formatString("VN")}`; let additional = ""; if (message) { const regex = /^[a-zA-Z0-9 ]{1,25}$/; if (!regex.test(message)) { throw Error("Message max 25 characters or only letters and numbers and space."); } const purpose = `08${this.formatString(message)}`; additional = `62${this.formatString(purpose)}`; } const CRC = `6304`; const value = payloadFormatIndicator + pointofInitiationMethod + merchantAccountInformation + transactionCurrency + transactionAmount + countryCode + additional + CRC; let crc = (0, crc16ccitt_1.default)(value).toString(16); if (crc.length === 3) { crc = `0${crc}`; } return `${value}${crc.toLocaleUpperCase()}`; } } exports.VietQr = VietQr;