bysquare 1.3.0 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/generate.d.ts CHANGED
@@ -41,4 +41,7 @@ export declare function makeTabbed(model: Model): string;
41
41
  * @see 3.13. Alphanumeric conversion using Base32hex
42
42
  */
43
43
  export declare function alphanumericConversion(data: Buffer): string;
44
+ /**
45
+ * Generate QR string ready for encoding into basic QR code
46
+ */
44
47
  export declare function generate(model: Model): Promise<string>;
package/lib/generate.js CHANGED
@@ -24,6 +24,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.generate = exports.alphanumericConversion = exports.makeTabbed = exports.prepareForCompression = exports.makeChecksum = exports.makeHeaderBysquare = void 0;
27
+ const lodash_es_1 = require("lodash-es");
27
28
  const lzma = __importStar(require("lzma-native"));
28
29
  const index_1 = require("./index");
29
30
  // echo "Hello" | xz --format=raw --lzma1=lc=3,lp=0,pb=2,dict=32KiB --stdout | hexdump -C
@@ -108,6 +109,11 @@ exports.prepareForCompression = prepareForCompression;
108
109
  function makeTabbed(model) {
109
110
  const tabbed = Object.keys(model).reduce((acc, key) => {
110
111
  const index = index_1.SequenceOrder[key];
112
+ /** Diacritical marks are not allowed */
113
+ if (key === "PaymentNote") {
114
+ acc[index] = (0, lodash_es_1.deburr)(model[key]);
115
+ return acc;
116
+ }
111
117
  acc[index] = String(model[key]);
112
118
  return acc;
113
119
  }, Array(33).fill(undefined));
@@ -159,6 +165,9 @@ function alphanumericConversion(data) {
159
165
  return encoded;
160
166
  }
161
167
  exports.alphanumericConversion = alphanumericConversion;
168
+ /**
169
+ * Generate QR string ready for encoding into basic QR code
170
+ */
162
171
  function generate(model) {
163
172
  const data = prepareForCompression(model);
164
173
  const compressedData = [];
package/lib/parse.js CHANGED
@@ -26,9 +26,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.detect = exports.inverseAlphanumericConversion = exports.parse = exports.assemble = void 0;
27
27
  const lzma = __importStar(require("lzma-native"));
28
28
  const index_1 = require("./index");
29
- const FIELDS_INVOICE = 0;
30
- const FIELDS_NUMBER_OF_PAYMENTS = 1;
31
- const FIELDS_PAYMENT_OPTIONS = 2;
29
+ const FIELD_INVOICE = 0;
30
+ const FIELD_PAYMENT_COUNT = 1;
31
+ const FIELD_PAYMENT_OPTIONS = 2;
32
+ const FIELD_BANK_ACCOUNT_COUNT = 11;
32
33
  /**
33
34
  * @see 3.14. Generating by square Code
34
35
  */
@@ -37,15 +38,19 @@ function assemble(tabbed) {
37
38
  .split("\t")
38
39
  /** The end of the qr-string might contain a NULL-terminated string */
39
40
  .map((entry) => entry.replace("\x00", ""));
40
- const invoiceId = fields[FIELDS_INVOICE];
41
+ const invoiceId = fields[FIELD_INVOICE];
41
42
  const output = {
42
- invoiceId: !!invoiceId.length ? invoiceId : undefined,
43
+ invoiceId: invoiceId?.length ? invoiceId : undefined,
43
44
  payments: []
44
45
  };
45
- const paymentsCount = Number(fields[FIELDS_NUMBER_OF_PAYMENTS]);
46
- const paymentOptions = Number(fields[FIELDS_PAYMENT_OPTIONS]);
46
+ const paymentsCount = Number(fields[FIELD_PAYMENT_COUNT]);
47
+ const paymentOptions = Number(fields[FIELD_PAYMENT_OPTIONS]);
48
+ const bankAccountsCount = Number(fields[FIELD_BANK_ACCOUNT_COUNT]);
47
49
  for (let i = 0; i < paymentsCount; i++) {
48
- const payment = fields.slice(3 + i, 11 + i);
50
+ const paymentFieldCount = 8;
51
+ const paymentFieldStart = 3 + (i * paymentFieldCount);
52
+ const paymentFieldEnd = 11 + (i * paymentFieldCount);
53
+ const payment = fields.slice(paymentFieldStart, paymentFieldEnd);
49
54
  const [ammount, currencyCode, paymentDueDate, variableSymbol, constantSymbol, specificSymbol, originatorsReferenceInformation, paymentNote] = payment;
50
55
  output.payments.push({
51
56
  amount: ammount.length ? Number(ammount) : undefined,
@@ -56,17 +61,17 @@ function assemble(tabbed) {
56
61
  specificSymbol: specificSymbol?.length ? specificSymbol : undefined,
57
62
  originatorsReferenceInformation: originatorsReferenceInformation?.length ? originatorsReferenceInformation : undefined,
58
63
  paymentNote: paymentNote?.length ? paymentNote : undefined,
59
- bankAccounts: []
64
+ bankAccounts: [],
60
65
  });
61
- const bankAccounts = fields.slice(11 + i, 15 + i);
62
- const [bankAccountsCount, iban, bic] = bankAccounts;
63
- if (bankAccountsCount?.length === 0) {
64
- throw new Error("Missing bank accounts count");
65
- }
66
- if (iban?.length === 0) {
67
- throw new Error("Missing IBAN");
68
- }
69
66
  for (let j = 0; j < Number(bankAccountsCount); j++) {
67
+ const bankAccountFieldCount = 2;
68
+ const bankAccountFieldStart = 12 + (j * bankAccountFieldCount);
69
+ const bankAccountFieldEnd = 14 + (j * bankAccountFieldCount);
70
+ const bankAccounts = fields.slice(bankAccountFieldStart, bankAccountFieldEnd);
71
+ const [iban, bic] = bankAccounts;
72
+ if (iban?.length === 0) {
73
+ throw new Error("Missing IBAN");
74
+ }
70
75
  output.payments[i].bankAccounts.push({
71
76
  iban: iban,
72
77
  bic: bic?.length ? bic : undefined
@@ -76,7 +81,10 @@ function assemble(tabbed) {
76
81
  case index_1.PaymentOptions.PaymentOrder:
77
82
  break;
78
83
  case index_1.PaymentOptions.StandingOrder:
79
- const standingOrder = fields.slice(15 + i, 20 + i);
84
+ const standingOrderFieldCount = 4;
85
+ const standingOrderFieldStart = 15 + (i * standingOrderFieldCount);
86
+ const standingOrderFieldEnd = 20 + (i * standingOrderFieldCount);
87
+ const standingOrder = fields.slice(standingOrderFieldStart, standingOrderFieldEnd);
80
88
  const [day, month, periodicity, lastDate] = standingOrder;
81
89
  output.payments[i].standingOrder = {
82
90
  day: day?.length ? Number(day) : undefined,
@@ -86,7 +94,10 @@ function assemble(tabbed) {
86
94
  };
87
95
  break;
88
96
  case index_1.PaymentOptions.DirectDebit:
89
- const directDebit = fields.slice(16 + i, 26 + i);
97
+ const directDebitFieldCount = 10;
98
+ const directDebitFieldStart = 16 + (i * directDebitFieldCount);
99
+ const directDebitFieldEnd = 27 + (i * directDebitFieldStart);
100
+ const directDebit = fields.slice(directDebitFieldStart, directDebitFieldEnd);
90
101
  const [directDebitScheme, directDebitType, variableSymbol, specificSymbol, originatorsReferenceInformation, mandateId, creditorId, contractId, maxAmount, validTillDate,] = directDebit;
91
102
  output.payments[i].directDebit = {
92
103
  directDebitScheme: directDebitScheme.length ? Number(directDebitScheme) : undefined,
@@ -107,15 +118,16 @@ function assemble(tabbed) {
107
118
  /** Beneficiary list bysquare v1.1 */
108
119
  for (let i = 0; i < paymentsCount; i++) {
109
120
  let beneficiary = [];
121
+ const offset = (i || 1) * (2 * bankAccountsCount) - 2;
110
122
  switch (paymentOptions) {
111
123
  case index_1.PaymentOptions.PaymentOrder:
112
- beneficiary = fields.slice(16 + i, 20 + i);
124
+ beneficiary = fields.slice(16 + offset, 20 + offset);
113
125
  break;
114
126
  case index_1.PaymentOptions.StandingOrder:
115
- beneficiary = fields.slice(20 + i, 24 + i);
127
+ beneficiary = fields.slice(20 + offset, 24 + offset);
116
128
  break;
117
129
  case index_1.PaymentOptions.DirectDebit:
118
- beneficiary = fields.slice(25 + i, 29 + i);
130
+ beneficiary = fields.slice(25 + offset, 29 + offset);
119
131
  break;
120
132
  }
121
133
  if (beneficiary.length === 0) {
package/lib/types.d.ts CHANGED
@@ -2,13 +2,13 @@
2
2
  * The bit sequence is split into 5 bit chunks which are mapped onto the
3
3
  * characters
4
4
  *
5
- * @see {spec 3.13. Table 9 – Encoding table}
5
+ * @see 3.13. Table 9 – Encoding table
6
6
  */
7
7
  export declare const SUBST = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
8
8
  export declare enum PaymentOptions {
9
9
  PaymentOrder = 1,
10
10
  StandingOrder = 2,
11
- DirectDebit = 3
11
+ DirectDebit = 4
12
12
  }
13
13
  export declare enum MonthClassifier {
14
14
  January = 1,
package/lib/types.js CHANGED
@@ -5,7 +5,7 @@ exports.CurrencyCode = exports.SequenceOrder = exports.DirectDebitScheme = expor
5
5
  * The bit sequence is split into 5 bit chunks which are mapped onto the
6
6
  * characters
7
7
  *
8
- * @see {spec 3.13. Table 9 – Encoding table}
8
+ * @see 3.13. Table 9 – Encoding table
9
9
  */
10
10
  exports.SUBST = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
11
11
  /**
@@ -31,7 +31,7 @@ var PaymentOptions;
31
31
  (function (PaymentOptions) {
32
32
  PaymentOptions[PaymentOptions["PaymentOrder"] = 1] = "PaymentOrder";
33
33
  PaymentOptions[PaymentOptions["StandingOrder"] = 2] = "StandingOrder";
34
- PaymentOptions[PaymentOptions["DirectDebit"] = 3] = "DirectDebit";
34
+ PaymentOptions[PaymentOptions["DirectDebit"] = 4] = "DirectDebit";
35
35
  })(PaymentOptions = exports.PaymentOptions || (exports.PaymentOptions = {}));
36
36
  var MonthClassifier;
37
37
  (function (MonthClassifier) {
@@ -46,7 +46,7 @@ var MonthClassifier;
46
46
  MonthClassifier[MonthClassifier["September"] = 256] = "September";
47
47
  MonthClassifier[MonthClassifier["October"] = 512] = "October";
48
48
  MonthClassifier[MonthClassifier["November"] = 1024] = "November";
49
- MonthClassifier[MonthClassifier["December"] = 2048] = "December";
49
+ MonthClassifier[MonthClassifier["December"] = 2048] = "December"; // 2^11
50
50
  })(MonthClassifier = exports.MonthClassifier || (exports.MonthClassifier = {}));
51
51
  var PeriodicityClassifier;
52
52
  (function (PeriodicityClassifier) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bysquare",
3
3
  "description": "It's a national standard for payment QR codes adopted by Slovak Banking Association (SBA)",
4
- "version": "1.3.0",
4
+ "version": "1.3.2",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Filip Seman <filip.seman@protonmail.com>",
7
7
  "keywords": [
@@ -34,9 +34,11 @@
34
34
  "postversion": ""
35
35
  },
36
36
  "dependencies": {
37
+ "lodash-es": "~4.17.0",
37
38
  "lzma-native": "8.0.6"
38
39
  },
39
40
  "devDependencies": {
41
+ "@types/lodash-es": "~4.17.0",
40
42
  "@types/lzma-native": "^4.0.1",
41
43
  "@types/node": ">=14",
42
44
  "typescript": "~4.7.0",