UNPKG

modify-qris

Version:

Modify QRIS Generator is a javascript based libraries that enabling flexible payments with customizable amounts for improved efficiency and user experience

247 lines (246 loc) 10.7 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.QRIS = void 0; const path = require('path'); const qrcode_1 = require("qrcode"); const enums_1 = require("./enums"); const qris_1 = require("./helpers/qris"); const reader_1 = require("./helpers/reader"); const entities_1 = require("./entities"); const errors_1 = require("./errors"); class QRIS { constructor(config) { // init default this.sourceType = enums_1.SourceType.CODE; if (config.sourceType) { this.sourceType = config.sourceType; } if (config.sourceValue) { this.sourceValue = config.sourceValue; } if (config.merchantName) { this.setMerchantName(config.merchantName); } if (config.merchantAddress) { this.setMerchantAddress(config.merchantAddress); } if (config.merchantPostalCode) { this.setMerchantPostalCode(config.merchantPostalCode); } if (config.amount) { this.setAmount(config.amount); } if (config.feeCategory) { this.setFeeCategory(config.feeCategory); } if (config.fee) { this.setFee(config.fee); } } setMerchantName(merchantName) { this.merchantName = merchantName; } setMerchantAddress(merchantAddress) { this.merchantAddress = merchantAddress; } setMerchantPostalCode(merchantPostalCode) { this.merchantPostalCode = merchantPostalCode; } setAmount(amount) { this.amount = amount; } setFeeCategory(feeCategory) { this.feeCategory = feeCategory; } setFee(fee) { this.fee = fee; } setTerminalLabelCategory(terminalLabel) { this.terminalLabel = terminalLabel; } sourceValueIsEmpty() { if ([undefined, ''].includes(this.sourceValue)) { return true; } return false; } generate(sourceValue) { // check value qr code is not empty if (this.sourceValueIsEmpty()) { throw new errors_1.QRISError('Source value is empty.', errors_1.ErrorCode.SOURCE_EMPTY, { source_type: this.sourceType, source_value: this.sourceValue }); } if (!sourceValue) { sourceValue = this.sourceValue; } // parse to object qris let parsedString = qris_1.QRIS.parse(sourceValue); // set qris value if (this.amount) { const amount = this.amount.toString(); const paddedLength = amount.toString().padStart(2, '0'); parsedString.payment_amount = qris_1.QRIS.modifyContent({ tag: enums_1.QRISTags.PAYMENT_AMOUNT, content: amount, data: enums_1.QRISTags.PAYMENT_AMOUNT + paddedLength + amount }, amount); } else { parsedString.payment_amount = null; } if (parsedString.acquirer.tag == enums_1.QRISTags.ACQUIRER) { if (this.merchantName) { parsedString.merchant_name = qris_1.QRIS.modifyContent(parsedString.merchant_name, this.merchantName); } if (this.merchantAddress) { parsedString.merchant_city = qris_1.QRIS.modifyContent(parsedString.merchant_city, this.merchantAddress); } if (this.merchantPostalCode) { parsedString.merchant_postal_code = qris_1.QRIS.modifyContent(parsedString.merchant_postal_code, this.merchantPostalCode); } if (this.amount > 0 && this.feeCategory != '' && this.fee > 0) { const categoryContent = enums_1.CategoryContent.DYNAMIC; const categoryContentLength = categoryContent.toString().padStart(2, '0'); parsedString.category = qris_1.QRIS.modifyContent({ tag: enums_1.QRISTags.CATEGORY, content: categoryContent, data: enums_1.QRISTags.CATEGORY + categoryContentLength + categoryContent }, categoryContent); if (this.feeCategory == enums_1.PaymentFeeCategory.PERCENT && this.fee > 100) { throw new errors_1.QRISError('fee may not exceed 100%', errors_1.ErrorCode.FEE_EXCEEDED, { maxFee: 100, actualFee: this.fee }); } let paymentFeeCategoryTag = enums_1.QRISTags.PAYMENT_FEE_CATEGORY; let paymentFeeCategoryContentLength = ''; let paymentFeeCategoryContent = ''; let paymentFeeTag = ''; if (this.feeCategory == enums_1.PaymentFeeCategory.FIXED) { paymentFeeCategoryContent = enums_1.PaymentFeeCategoryContent.FIXED; paymentFeeTag = enums_1.QRISTags.PAYMENT_FEE_FIXED; } else if (this.feeCategory == enums_1.PaymentFeeCategory.PERCENT) { paymentFeeCategoryContent = enums_1.PaymentFeeCategoryContent.PERCENT; paymentFeeTag = enums_1.QRISTags.PAYMENT_FEE_PERCENT; } paymentFeeCategoryContentLength = paymentFeeCategoryContent.toString().padStart(2, '0'); parsedString.payment_fee_category = qris_1.QRIS.modifyContent({ tag: paymentFeeCategoryTag, content: paymentFeeCategoryContent, data: paymentFeeCategoryTag + paymentFeeCategoryContentLength + paymentFeeCategoryContent }, paymentFeeCategoryContent); if (parsedString.payment_fee_category.tag != "") { const paymentFee = this.fee.toString(); const paddedLengthFee = paymentFee.toString().padStart(2, '0'); parsedString.payment_fee = qris_1.QRIS.modifyContent({ tag: paymentFeeTag, content: paymentFee, data: paymentFeeTag + paddedLengthFee + paymentFee }, paymentFee); } } } if (this.terminalLabel) { const terminalLabelContent = this.terminalLabel; const terminalLabelContentLength = terminalLabelContent.toString().padStart(2, '0'); parsedString.additional_information.detail.terminal_label = qris_1.QRIS.modifyContent({ tag: enums_1.AdditionalInformationTags.TERMINAL_LABEL, content: terminalLabelContent, data: enums_1.AdditionalInformationTags.TERMINAL_LABEL + terminalLabelContentLength + terminalLabelContent }, terminalLabelContent); } // refresh data additional information const additionalInformationContent = (0, entities_1.additionalInformationDetailEntityToString)(parsedString.additional_information.detail); const additionalInformationContentLength = additionalInformationContent.length.toString().padStart(2, '0'); parsedString.additional_information = { tag: enums_1.QRISTags.ADDITIONAL_INFORMATION, content: additionalInformationContent, data: enums_1.QRISTags.ADDITIONAL_INFORMATION + additionalInformationContentLength + additionalInformationContent, detail: parsedString.additional_information.detail, }; // modify qris const modifyQrisParse = qris_1.QRIS.modify(parsedString); // validation qris const isValidQrisErr = qris_1.QRIS.isValid(modifyQrisParse); if (isValidQrisErr.length > 0) { throw new errors_1.QRISError('Validation failed.', errors_1.ErrorCode.VALIDATION_FAILED, isValidQrisErr); } return (0, entities_1.qrisEntityToString)(modifyQrisParse); } readQR() { return __awaiter(this, void 0, void 0, function* () { if (this.sourceType == enums_1.SourceType.IMAGE_PATH) { const content = yield reader_1.QRISReader.readFromFile(this.sourceValue); return this.generate(content); } else { return this.generate(null); } }); } generateQR() { return __awaiter(this, void 0, void 0, function* () { try { return yield this.readQR(); } catch (err) { throw new Error(err); } }); } generateQRBase64() { return __awaiter(this, void 0, void 0, function* () { try { const resultString = yield this.readQR(); return (0, qrcode_1.toDataURL)(resultString).then(url => { return url; }).catch(err => { throw new Error(err); }); } catch (err) { return err; } }); } generateQRTerminal() { return __awaiter(this, void 0, void 0, function* () { try { const resultString = yield this.readQR(); return (0, qrcode_1.toString)(resultString, { type: 'terminal', small: true }).then(url => { return url; }).catch(err => { throw new Error(err); }); } catch (err) { return err; } }); } generateQRFile(output) { return __awaiter(this, void 0, void 0, function* () { try { if (!output) { output = 'output.png'; } const resultString = yield this.readQR(); return (0, qrcode_1.toFile)(output, resultString).then(() => { return output; }).catch(err => { throw new Error(err); }); } catch (err) { throw new Error(err); } }); } } exports.QRIS = QRIS;