UNPKG

promptparse

Version:

All-in-one JS library for PromptPay & EMVCo QR Codes

677 lines 17.3 kB
"use strict"; function _array_like_to_array(arr, len) { if (len == null || len > arr.length) len = arr.length; for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i]; return arr2; } function _array_with_holes(arr) { if (Array.isArray(arr)) return arr; } function _class_call_check(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _create_class(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _iterable_to_array_limit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){ _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally{ try { if (!_n && _i["return"] != null) _i["return"](); } finally{ if (_d) throw _e; } } return _arr; } function _non_iterable_rest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _sliced_to_array(arr, i) { return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest(); } function _type_of(obj) { "@swc/helpers - typeof"; return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; } function _unsupported_iterable_to_array(o, minLen) { if (!o) return; if (typeof o === "string") return _array_like_to_array(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen); } var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __defNormalProp = function(obj, key, value) { return key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value: value }) : obj[key] = value; }; var __export = function(target, all) { for(var name in all)__defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = function(to, from, except, desc) { if (from && (typeof from === "undefined" ? "undefined" : _type_of(from)) === "object" || typeof from === "function") { var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined; try { var _loop = function() { var key = _step.value; if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: function() { return from[key]; }, enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); }; for(var _iterator = __getOwnPropNames(from)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop(); } catch (err) { _didIteratorError = true; _iteratorError = err; } finally{ try { if (!_iteratorNormalCompletion && _iterator.return != null) { _iterator.return(); } } finally{ if (_didIteratorError) { throw _iteratorError; } } } } return to; }; var __toCommonJS = function(mod) { return __copyProps(__defProp({}, "__esModule", { value: true }), mod); }; var __publicField = function(obj, key, value) { return __defNormalProp(obj, (typeof key === "undefined" ? "undefined" : _type_of(key)) !== "symbol" ? key + "" : key, value); }; // src/generate/index.ts var generate_exports = {}; __export(generate_exports, { ProxyType: function() { return ProxyType; }, anyId: function() { return anyId; }, billPayment: function() { return billPayment; }, botBarcode: function() { return botBarcode; }, slipVerify: function() { return slipVerify; }, trueMoney: function() { return trueMoney; }, trueMoneySlipVerify: function() { return trueMoneySlipVerify; } }); module.exports = __toCommonJS(generate_exports); // src/utils/checksum.ts var TABLE = [ 0, 4129, 8258, 12387, 16516, 20645, 24774, 28903, 33032, 37161, 41290, 45419, 49548, 53677, 57806, 61935, 4657, 528, 12915, 8786, 21173, 17044, 29431, 25302, 37689, 33560, 45947, 41818, 54205, 50076, 62463, 58334, 9314, 13379, 1056, 5121, 25830, 29895, 17572, 21637, 42346, 46411, 34088, 38153, 58862, 62927, 50604, 54669, 13907, 9842, 5649, 1584, 30423, 26358, 22165, 18100, 46939, 42874, 38681, 34616, 63455, 59390, 55197, 51132, 18628, 22757, 26758, 30887, 2112, 6241, 10242, 14371, 51660, 55789, 59790, 63919, 35144, 39273, 43274, 47403, 23285, 19156, 31415, 27286, 6769, 2640, 14899, 10770, 56317, 52188, 64447, 60318, 39801, 35672, 47931, 43802, 27814, 31879, 19684, 23749, 11298, 15363, 3168, 7233, 60846, 64911, 52716, 56781, 44330, 48395, 36200, 40265, 32407, 28342, 24277, 20212, 15891, 11826, 7761, 3696, 65439, 61374, 57309, 53244, 48923, 44858, 40793, 36728, 37256, 33193, 45514, 41451, 53516, 49453, 61774, 57711, 4224, 161, 12482, 8419, 20484, 16421, 28742, 24679, 33721, 37784, 41979, 46042, 49981, 54044, 58239, 62302, 689, 4752, 8947, 13010, 16949, 21012, 25207, 29270, 46570, 42443, 38312, 34185, 62830, 58703, 54572, 50445, 13538, 9411, 5280, 1153, 29798, 25671, 21540, 17413, 42971, 47098, 34713, 38840, 59231, 63358, 50973, 55100, 9939, 14066, 1681, 5808, 26199, 30326, 17941, 22068, 55628, 51565, 63758, 59695, 39368, 35305, 47498, 43435, 22596, 18533, 30726, 26663, 6336, 2273, 14466, 10403, 52093, 56156, 60223, 64286, 35833, 39896, 43963, 48026, 19061, 23124, 27191, 31254, 2801, 6864, 10931, 14994, 64814, 60687, 56684, 52557, 48554, 44427, 40424, 36297, 31782, 27655, 23652, 19525, 15522, 11395, 7392, 3265, 61215, 65342, 53085, 57212, 44955, 49082, 36825, 40952, 28183, 32310, 20053, 24180, 11923, 16050, 3793, 7920 ]; function crc16xmodem(data) { var crc = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 0; var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined; try { for(var _iterator = data[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){ var i = _step.value; var b = i.charCodeAt(0); var n = (b ^ crc >> 8) & 255; crc = TABLE[n] ^ crc << 8; } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally{ try { if (!_iteratorNormalCompletion && _iterator.return != null) { _iterator.return(); } } finally{ if (_didIteratorError) { throw _iteratorError; } } } return (crc ^ 0) & 65535; } // src/lib/tlv.ts function encode(tags) { var payload = ""; var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined; try { for(var _iterator = tags[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){ var tag2 = _step.value; payload += tag2.id; payload += ("00" + tag2.length).slice(-2); if (tag2.subTags) { payload += encode(tag2.subTags); continue; } payload += tag2.value; } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally{ try { if (!_iteratorNormalCompletion && _iterator.return != null) { _iterator.return(); } } finally{ if (_didIteratorError) { throw _iteratorError; } } } return payload; } function checksum(payload) { var upperCase = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : true; var sum = crc16xmodem(payload, 65535).toString(16); sum = upperCase ? sum.toUpperCase() : sum.toLowerCase(); sum = ("0000" + sum).slice(-4); return sum; } function withCrcTag(payload, crcTagId) { var upperCase = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : true; payload += ("00" + crcTagId).slice(-2); payload += "04"; payload += checksum(payload, upperCase); return payload; } function tag(tagId, value) { return { id: tagId, length: value.length, value: value }; } // src/generate/promptpay/BillPayment.ts function billPayment(param) { var billerId = param.billerId, amount = param.amount, ref1 = param.ref1, ref2 = param.ref2, ref3 = param.ref3; var tag30 = [ tag("00", "A000000677010112"), tag("01", billerId), tag("02", ref1) ]; if (ref2) { tag30.push(tag("03", ref2)); } var payload = [ tag("00", "01"), tag("01", !amount ? "11" : "12"), tag("30", encode(tag30)), tag("53", "764"), tag("58", "TH") ]; if (amount) { payload.push(tag("54", Number(amount).toFixed(2))); } if (ref3) { payload.push(tag("62", encode([ tag("07", ref3) ]))); } return withCrcTag(encode(payload), "63"); } // src/lib/BOTBarcode.ts var BOTBarcode = /*#__PURE__*/ function() { function _BOTBarcode(billerId, ref1) { var ref2 = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : null, amount = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : null; _class_call_check(this, _BOTBarcode); __publicField(this, "billerId"); __publicField(this, "ref1"); __publicField(this, "ref2"); __publicField(this, "amount"); this.billerId = billerId; this.ref1 = ref1; this.ref2 = ref2; this.amount = amount; } _create_class(_BOTBarcode, [ { key: "toString", value: function toString() { var _this = this, billerId = _this.billerId, ref1 = _this.ref1, ref2 = _this.ref2, amount = _this.amount; var amountStr = amount ? String(Number(amount.toFixed(2)) * 100) : "0"; return "|".concat(billerId, "\r").concat(ref1, "\r").concat(ref2 !== null && ref2 !== void 0 ? ref2 : "", "\r").concat(amountStr); } }, { /** * Converts BOT Barcode to PromptPay QR Tag 30 (Bill Payment) * * This method works for some biller, depends on destination bank * * @returns QR Code payload */ key: "toQrTag30", value: function toQrTag30() { var _this = this, billerId = _this.billerId, ref1 = _this.ref1, ref2 = _this.ref2, amount = _this.amount; return billPayment({ billerId: billerId, ref1: ref1, ref2: ref2 !== null && ref2 !== void 0 ? ref2 : void 0, amount: amount !== null && amount !== void 0 ? amount : void 0 }); } } ], [ { key: "fromString", value: function fromString(payload) { if (!payload.startsWith("|")) { return null; } var data = payload.slice(1).split("\r", 4); if (data.length != 4) { return null; } var _data = _sliced_to_array(data, 4), billerId = _data[0], ref1 = _data[1], ref2 = _data[2], amount = _data[3]; return new _BOTBarcode(billerId, ref1, ref2.length > 0 ? ref2 : null, amount != "0" ? Number((parseInt(amount) / 100).toFixed(2)) : null); } } ]); return _BOTBarcode; }(); // src/generate/bot/BOTBarcode.ts function botBarcode(param) { var billerId = param.billerId, ref1 = param.ref1, ref2 = param.ref2, amount = param.amount; return new BOTBarcode(billerId, ref1, ref2, amount).toString(); } // src/generate/promptpay/AnyID.ts var ProxyType = /* @__PURE__ */ function(ProxyType2) { ProxyType2["MSISDN"] = "01"; ProxyType2["NATID"] = "02"; ProxyType2["EWALLETID"] = "03"; ProxyType2["BANKACC"] = "04"; return ProxyType2; }(ProxyType || {}); function anyId(param) { var type = param.type, target = param.target, amount = param.amount; if (type == "MSISDN") { target = ("0000000000000" + target.replace(/^0/, "66")).slice(-13); } var tag29 = encode([ tag("00", "A000000677010111"), tag(ProxyType[type], target) ]); var payload = [ tag("00", "01"), tag("01", !amount ? "11" : "12"), tag("29", tag29), tag("53", "764"), tag("58", "TH") ]; if (amount) { payload.push(tag("54", Number(amount).toFixed(2))); } return withCrcTag(encode(payload), "63"); } // src/generate/promptpay/SlipVerify.ts function slipVerify(param) { var sendingBank = param.sendingBank, transRef = param.transRef; var payload = [ tag("00", encode([ tag("00", "000001"), tag("01", sendingBank), tag("02", transRef) ])), tag("51", "TH") ]; return withCrcTag(encode(payload), "91"); } // src/utils/encoder.ts function encodeTag81(message) { return message.split("").map(function(c) { return c.charCodeAt(0).toString(16).padStart(4, "0"); }).join("").toUpperCase(); } // src/generate/truemoney/TrueMoney.ts function trueMoney(param) { var mobileNo = param.mobileNo, amount = param.amount, message = param.message; var tag29 = encode([ tag("00", "A000000677010111"), tag("03", "14000".concat(mobileNo)) ]); var payload = [ tag("00", "01"), tag("01", !amount ? "11" : "12"), tag("29", tag29), tag("53", "764"), tag("58", "TH") ]; if (amount) { payload.push(tag("54", Number(amount).toFixed(2))); } if (message) { payload.push(tag("81", encodeTag81(message))); } return withCrcTag(encode(payload), "63"); } // src/generate/truemoney/TrueMoneySlipVerify.ts function trueMoneySlipVerify(param) { var eventType = param.eventType, transactionId = param.transactionId, date = param.date; var payload = [ tag("00", encode([ tag("00", "01"), tag("01", "01"), tag("02", eventType), tag("03", transactionId), tag("04", date) ])) ]; return withCrcTag(encode(payload), "91", false); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ProxyType: ProxyType, anyId: anyId, billPayment: billPayment, botBarcode: botBarcode, slipVerify: slipVerify, trueMoney: trueMoney, trueMoneySlipVerify: trueMoneySlipVerify }); //# sourceMappingURL=index.js.map