@craynic/qr-platba
Version:
Library to work with Czech QR payments
203 lines (202 loc) • 7.93 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.QrPlatba = void 0;
var ibantools_1 = require("ibantools");
var crc_1 = require("crc");
var Dinero = require("dinero.js");
var lodash_1 = require("lodash");
var QrPlatba = exports.QrPlatba = /** @class */ (function () {
function QrPlatba(account, am, currency) {
if (currency === void 0) { currency = 'CZK'; }
this.message = null;
this.constantSymbol = null;
this.specificSymbol = null;
this.variableSymbol = null;
this.paymentReference = null;
this.receiverName = null;
this.account = this.validateAccount(account);
this.amount = this.validateAmount(am, currency);
}
QrPlatba.prototype.getAccount = function () {
return this.account;
};
QrPlatba.prototype.setAccount = function (account) {
var newQrPlatba = (0, lodash_1.cloneDeep)(this);
newQrPlatba.account = this.validateAccount(account);
return newQrPlatba;
};
QrPlatba.prototype.validateAccount = function (account) {
if (isQrPayAccountString(account)) {
return [account];
}
if (isQrPayAccountArray(account)) {
return account;
}
throw new Error('Invalid account: please provide a string or array of strings (max 3), containing IBAN or IBAN+BIC.');
};
QrPlatba.prototype.getAmount = function () {
return this.amount;
};
QrPlatba.prototype.setAmount = function (amount, currency) {
if (currency === void 0) { currency = null; }
var newQrPlatba = (0, lodash_1.cloneDeep)(this);
newQrPlatba.amount = this.validateAmount(amount, currency !== null && currency !== void 0 ? currency : this.amount.getCurrency());
return newQrPlatba;
};
QrPlatba.prototype.validateAmount = function (amount, currency) {
var dinero = Dinero({ amount: amount, currency: currency });
if (!isQrPayAmountInCents(dinero)) {
throw new Error('Invalid pay amount (AM) in cents!');
}
return dinero;
};
QrPlatba.prototype.getMessage = function () {
return this.message;
};
QrPlatba.prototype.setMessage = function (value) {
if (value === null || isQrPayStringOfLength(value, 0, 60)) {
var newQrPlatba = (0, lodash_1.cloneDeep)(this);
newQrPlatba.message = value;
return newQrPlatba;
}
else {
throw new Error('Invalid message');
}
};
QrPlatba.prototype.getConstantSymbol = function () {
return this.constantSymbol;
};
QrPlatba.prototype.setConstantSymbol = function (value) {
if (value === null || isQrPayNumericStringOfLength(value, 0, 10)) {
var newQrPlatba = (0, lodash_1.cloneDeep)(this);
newQrPlatba.constantSymbol = value;
return newQrPlatba;
}
else {
throw new Error('Invalid constant symbol');
}
};
QrPlatba.prototype.getSpecificSymbol = function () {
return this.specificSymbol;
};
QrPlatba.prototype.setSpecificSymbol = function (value) {
if (value === null || isQrPayNumericStringOfLength(value, 0, 10)) {
var newQrPlatba = (0, lodash_1.cloneDeep)(this);
newQrPlatba.specificSymbol = value;
return newQrPlatba;
}
else {
throw new Error('Invalid specific symbol');
}
};
QrPlatba.prototype.getVariableSymbol = function () {
return this.variableSymbol;
};
QrPlatba.prototype.setVariableSymbol = function (value) {
if (value === null || isQrPayNumericStringOfLength(value, 0, 10)) {
var newQrPlatba = (0, lodash_1.cloneDeep)(this);
newQrPlatba.variableSymbol = value;
return newQrPlatba;
}
else {
throw new Error('Invalid variable symbol');
}
};
QrPlatba.prototype.getPaymentReference = function () {
return this.paymentReference;
};
QrPlatba.prototype.setPaymentReference = function (value) {
if (value === null || isQrPayNumericStringOfLength(value, 0, 16)) {
var newQrPlatba = (0, lodash_1.cloneDeep)(this);
newQrPlatba.paymentReference = value;
return newQrPlatba;
}
else {
throw new Error('Invalid payment reference');
}
};
QrPlatba.prototype.getReceiverName = function () {
return this.receiverName;
};
QrPlatba.prototype.setReceiverName = function (value) {
if (value === null || isQrPayStringOfLength(value, 0, 35)) {
var newQrPlatba = (0, lodash_1.cloneDeep)(this);
newQrPlatba.receiverName = value;
return newQrPlatba;
}
else {
throw new Error('Invalid receiver name');
}
};
QrPlatba.prototype.toString = function () {
return "".concat(this.toStringWithoutChecksum(), "CRC32:").concat(this.getChecksum(), "*");
};
QrPlatba.prototype.getChecksum = function () {
return (0, crc_1.crc32)(this.toStringWithoutChecksum()).toString(16).padStart(8, '0').toUpperCase();
};
QrPlatba.prototype.toStringWithoutChecksum = function () {
return "SPD*".concat(QrPlatba.VERSION, "*").concat(this.getFields().map(function (field) { return "".concat(field[0], ":").concat(field[1]); }).join('*'), "*");
};
QrPlatba.prototype.getFields = function () {
return Array(['ACC', this.getAccount()[0].toString()], ['AM', this.getAmount().toFormat('0.00')], ['CC', this.getAmount().getCurrency()], ['MSG', this.getMessage()], ['X-KS', this.getConstantSymbol()], ['X-SS', this.getSpecificSymbol()], ['X-VS', this.getVariableSymbol()], [
'ALT-ACC',
this.getAccount().length > 1
? this.getAccount().slice(1).join(',')
: null
], ['RF', this.getPaymentReference()], ['RN', this.getReceiverName()]).filter(function (value) { return value[1] !== null && value[1] !== ''; }).map(function (value) {
return [value[0], sanitizePayString(value[1])];
})
.sort(function (a, b) {
if (a[0] < b[0]) {
return -1;
}
if (a[0] > b[0]) {
return 1;
}
if (a[1] < b[1]) {
return -1;
}
if (a[1] > b[1]) {
return 1;
}
return 0;
});
};
QrPlatba.VERSION = '1.2';
return QrPlatba;
}());
var isQrPayAccountString = function (value) {
if (typeof value !== 'string') {
return false;
}
var strParts = value.split('+');
return value.length > 0
&& value.length <= 46
&& strParts.length <= 2
&& (0, ibantools_1.isValidIBAN)(strParts[0])
&& (strParts.length === 1 || (0, ibantools_1.isValidBIC)(strParts[1]));
};
var isQrPayAccountArray = function (values) {
return Array.isArray(values)
&& values.length >= 1
&& values.length <= 3
&& values.reduce(function (previousValue, value) { return isQrPayAccountString(value) && previousValue; }, true);
};
var isQrPayAmountInCents = function (value) {
return value.getAmount() > 0 && value.getAmount() < 10000000 * 100;
};
function sanitizePayString(value) {
return /\*/g[Symbol.replace](value, '%2A');
}
var isQrPayStringOfLength = function (value, min, max) {
var sanitizedValue = sanitizePayString(value);
return sanitizedValue.length >= min
&& sanitizedValue.length <= max
&& !/[^\u0020-\u007e\u00a0-\u00ff]/g.test(value)
&& !/^ /.test(value)
&& !/ $/.test(value);
};
var isQrPayNumericStringOfLength = function (value, min, max) {
return isQrPayStringOfLength(value, min, max)
&& /^[0-9]*$/.test(value);
};