bysquare 2.0.1 → 2.2.0
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/README.md +84 -55
- package/lib/cjs/cli.js +20 -39
- package/lib/cjs/generate.d.ts +19 -11
- package/lib/cjs/generate.js +122 -116
- package/lib/cjs/package.json +25 -26
- package/lib/cjs/parse.d.ts +13 -4
- package/lib/cjs/parse.js +201 -89
- package/lib/cjs/types.d.ts +18 -0
- package/lib/mjs/cli.js +20 -39
- package/lib/mjs/generate.d.ts +19 -11
- package/lib/mjs/generate.js +94 -111
- package/lib/mjs/package.json +25 -26
- package/lib/mjs/parse.d.ts +13 -4
- package/lib/mjs/parse.js +176 -85
- package/lib/mjs/types.d.ts +18 -0
- package/package.json +25 -26
package/README.md
CHANGED
|
@@ -2,18 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
![version][version] ![build][build]
|
|
4
4
|
|
|
5
|
-
Simple
|
|
5
|
+
Simple JavaScript library to generate and parse "PAY by square" string.
|
|
6
6
|
|
|
7
7
|
**What is `PAY by square`?**
|
|
8
8
|
|
|
9
|
-
It's a national standard for
|
|
10
|
-
Association in 2013. It is
|
|
11
|
-
other payment regulations.
|
|
9
|
+
It's a national standard for QR code payments that was adopted by the Slovak
|
|
10
|
+
Banking Association in 2013. It is incorporated into a variety of invoices,
|
|
11
|
+
reminders and other payment regulations.
|
|
12
12
|
|
|
13
13
|
**Can I generate an image?**
|
|
14
14
|
|
|
15
|
-
This library
|
|
16
|
-
|
|
15
|
+
This library doesn't have a specific opinion and how the QR code string is
|
|
16
|
+
transformed into images depends on how you implement it. See
|
|
17
|
+
[examples](examples).
|
|
17
18
|
|
|
18
19
|
## Install
|
|
19
20
|
|
|
@@ -31,6 +32,9 @@ npm install xseman/bysquare#master
|
|
|
31
32
|
|
|
32
33
|
# latest unreleased changes
|
|
33
34
|
npm install xseman/bysquare#develop
|
|
35
|
+
|
|
36
|
+
# specific tag version, e.g. v2.1.0
|
|
37
|
+
npm install xseman/bysquare#v2.1.0
|
|
34
38
|
```
|
|
35
39
|
|
|
36
40
|
**CLI**
|
|
@@ -39,67 +43,95 @@ npm install xseman/bysquare#develop
|
|
|
39
43
|
npm install --global bysquare
|
|
40
44
|
```
|
|
41
45
|
|
|
46
|
+
**Deno** `v1.28+`, just import `npm:bysquare` `v2.1.0+`
|
|
47
|
+
|
|
48
|
+
```
|
|
49
|
+
import { generate, parse } from "npm:bysquare@2.1.0"
|
|
50
|
+
```
|
|
51
|
+
|
|
42
52
|
## How it works
|
|
43
53
|
|
|
44
54
|
### Encoding sequence
|
|
45
55
|
|
|
46
|
-

|
|
47
57
|
|
|
48
58
|
## API
|
|
49
59
|
|
|
50
60
|
```ts
|
|
51
|
-
generate(model: DataModel, options?: Options):
|
|
52
|
-
parse(qr: string):
|
|
61
|
+
generate(model: DataModel, options?: Options): string
|
|
62
|
+
parse(qr: string): DataModel
|
|
53
63
|
detect(qr: string): Boolean
|
|
54
64
|
```
|
|
55
65
|
|
|
56
|
-
|
|
66
|
+
## Usage
|
|
67
|
+
|
|
68
|
+
Generate
|
|
57
69
|
|
|
58
70
|
```ts
|
|
59
|
-
import {
|
|
71
|
+
import { DataModel, generate, PaymentOptions } from "bysquare"
|
|
60
72
|
|
|
61
|
-
|
|
73
|
+
// long string ready to be encoded to QR
|
|
74
|
+
const qrString = generate({
|
|
62
75
|
invoiceId: "random-id",
|
|
63
76
|
payments: [
|
|
64
77
|
{
|
|
65
78
|
type: PaymentOptions.PaymentOrder,
|
|
66
79
|
amount: 100.0,
|
|
67
80
|
bankAccounts: [
|
|
68
|
-
{ iban: "SK9611000000002918599669" }
|
|
81
|
+
{ iban: "SK9611000000002918599669" }
|
|
69
82
|
],
|
|
70
83
|
currencyCode: "EUR",
|
|
71
|
-
variableSymbol: "123"
|
|
84
|
+
variableSymbol: "123"
|
|
72
85
|
}
|
|
73
86
|
]
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
generate(model).then((qr: string) => {
|
|
77
|
-
// your logic...
|
|
78
87
|
})
|
|
79
88
|
```
|
|
80
89
|
|
|
81
|
-
|
|
90
|
+
Parse
|
|
82
91
|
|
|
83
92
|
```ts
|
|
84
|
-
import { parse
|
|
85
|
-
|
|
86
|
-
const
|
|
87
|
-
parse(
|
|
88
|
-
|
|
89
|
-
|
|
93
|
+
import { parse } from "bysquare"
|
|
94
|
+
|
|
95
|
+
const model =
|
|
96
|
+
parse("0405QH8090IFU27IV0J6HGGLIOTIBVHNQQJQ6LAVGNBT363HR13JC6CB54HSI0KH9FCRASHNQBSKAQD2LJ4AU400UVKDNDPFRKLOBEVVVU0QJ000")
|
|
97
|
+
|
|
98
|
+
// {
|
|
99
|
+
// invoiceId: "random-id",
|
|
100
|
+
// payments: [
|
|
101
|
+
// {
|
|
102
|
+
// type: 1,
|
|
103
|
+
// amount: 100.0,
|
|
104
|
+
// bankAccounts: [
|
|
105
|
+
// { iban: "SK9611000000002918599669" },
|
|
106
|
+
// ],
|
|
107
|
+
// currencyCode: "EUR",
|
|
108
|
+
// variableSymbol: "123",
|
|
109
|
+
// }
|
|
110
|
+
// ]
|
|
111
|
+
// }
|
|
112
|
+
//
|
|
113
|
+
const qr =
|
|
114
|
+
"0004A00090IFU27IV0J6HGGLIOTIBVHNQQJQ6LAVGNBT363HR13JC6CB54HSI0KH9FCRASHNQBSKAQD2LJ4AU400UVKDNDPFRKLOBEVVVU0QJ000"
|
|
115
|
+
|
|
116
|
+
const model = parse(qr)
|
|
117
|
+
/**
|
|
118
|
+
* {
|
|
119
|
+
* invoiceId: "random-id",
|
|
120
|
+
* payments: [
|
|
121
|
+
* {
|
|
122
|
+
* type: PaymentOptions.PaymentOrder,
|
|
123
|
+
* amount: 100.0,
|
|
124
|
+
* bankAccounts: [
|
|
125
|
+
* { iban: "SK9611000000002918599669" },
|
|
126
|
+
* ],
|
|
127
|
+
* currencyCode: "EUR",
|
|
128
|
+
* variableSymbol: "123",
|
|
129
|
+
* }
|
|
130
|
+
* ]
|
|
131
|
+
* }
|
|
132
|
+
*/
|
|
90
133
|
```
|
|
91
134
|
|
|
92
|
-
**detect(qr: string): Boolean**
|
|
93
|
-
|
|
94
|
-
```ts
|
|
95
|
-
import { detect } from "bysquare"
|
|
96
|
-
|
|
97
|
-
const qr = "0004A00090IFU27IV0J6HGGLIOTIBVHNQQJQ6LAVGNBT363HR13JC6C75G19O246KTT5G8LTLM67HOIATP4OOG8F8FDLJ6T26KFCB1690NEVPQVSG0"
|
|
98
|
-
const isBysquare = detect(qr)
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
## CLI
|
|
102
|
-
|
|
103
135
|
You can use json file with valid model to generate qr-string.
|
|
104
136
|
|
|
105
137
|
```sh
|
|
@@ -110,9 +142,7 @@ You can use json file with valid model to generate qr-string.
|
|
|
110
142
|
# {
|
|
111
143
|
# "type": 1,
|
|
112
144
|
# "amount": 100.0,
|
|
113
|
-
# "bankAccounts": [
|
|
114
|
-
# { "iban": "SK9611000000002918599669" }
|
|
115
|
-
# ],
|
|
145
|
+
# "bankAccounts": [{ "iban": "SK9611000000002918599669" }],
|
|
116
146
|
# "currencyCode": "EUR",
|
|
117
147
|
# "variableSymbol": "123"
|
|
118
148
|
# }
|
|
@@ -120,27 +150,26 @@ You can use json file with valid model to generate qr-string.
|
|
|
120
150
|
# }
|
|
121
151
|
|
|
122
152
|
$ npx bysquare ./example.json
|
|
123
|
-
$
|
|
153
|
+
$ 0405QH8090IFU27IV0J6HGGLIOTIBVHNQQJQ6LAVGNBT363HR13JC6CB54HSI0KH9FCRASHNQBSKAQD2LJ4AU400UVKDNDPFRKLOBEVVVU0QJ000
|
|
124
154
|
```
|
|
125
155
|
|
|
126
156
|
You can also use stdin.
|
|
127
157
|
|
|
128
158
|
```sh
|
|
129
|
-
$ bysquare <<< '{
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
141
|
-
]
|
|
159
|
+
$ npx bysquare <<< '{
|
|
160
|
+
"invoiceId": "random-id",
|
|
161
|
+
"payments": [
|
|
162
|
+
{
|
|
163
|
+
"type": 1,
|
|
164
|
+
"amount": 100.0,
|
|
165
|
+
"bankAccounts": [{ "iban": "SK9611000000002918599669" }],
|
|
166
|
+
"currencyCode": "EUR",
|
|
167
|
+
"variableSymbol": "123"
|
|
168
|
+
}
|
|
169
|
+
]
|
|
142
170
|
}'
|
|
143
|
-
|
|
171
|
+
|
|
172
|
+
$ 0405QH8090IFU27IV0J6HGGLIOTIBVHNQQJQ6LAVGNBT363HR13JC6CB54HSI0KH9FCRASHNQBSKAQD2LJ4AU400UVKDNDPFRKLOBEVVVU0QJ000
|
|
144
173
|
```
|
|
145
174
|
|
|
146
175
|
## Related
|
|
@@ -162,9 +191,9 @@ https://github.com/dherges/npm-version-git-flow
|
|
|
162
191
|
- Run `npm test`
|
|
163
192
|
- Run `npm version <patch, minor, major>`
|
|
164
193
|
- Commit and push
|
|
165
|
-
- Run `npm version`
|
|
166
194
|
- Follow git-flow instructions
|
|
167
195
|
- Checkout to master
|
|
196
|
+
- Build artefacts
|
|
168
197
|
- Push commits and tag, git push && git push --tags
|
|
169
198
|
- Validate with `npm publish --dry-run`
|
|
170
199
|
- Publish to npm, `npm publish`
|
package/lib/cjs/cli.js
CHANGED
|
@@ -8,52 +8,34 @@ const node_fs_1 = require("node:fs");
|
|
|
8
8
|
const node_path_1 = __importDefault(require("node:path"));
|
|
9
9
|
const node_readline_1 = require("node:readline");
|
|
10
10
|
const generate_js_1 = require("./generate.js");
|
|
11
|
-
if (process.stdin.isTTY) {
|
|
12
|
-
// bysquare "file"
|
|
11
|
+
if /** bysquare "file" */ (process.stdin.isTTY) {
|
|
13
12
|
handleInput(process.argv[2]);
|
|
14
|
-
}
|
|
13
|
+
} /** bysquare <<< "data" */
|
|
15
14
|
else {
|
|
16
|
-
// echo "data" | bysquare
|
|
17
15
|
;
|
|
18
16
|
(async () => {
|
|
19
17
|
const stdin = await handleStdin();
|
|
20
|
-
|
|
21
|
-
console.error(e);
|
|
22
|
-
process.exit(1);
|
|
23
|
-
});
|
|
24
|
-
console.log(qr);
|
|
18
|
+
console.log(fromJsonString(stdin));
|
|
25
19
|
process.exit(0);
|
|
26
20
|
})();
|
|
27
21
|
}
|
|
28
|
-
|
|
22
|
+
function handleInput(input) {
|
|
29
23
|
if (input === undefined || input === "-h" || input === "--help") {
|
|
30
24
|
console.log(help());
|
|
31
25
|
process.exit(0);
|
|
32
26
|
}
|
|
33
27
|
if ((0, node_fs_1.existsSync)(process.argv[2])) {
|
|
34
28
|
const file = (0, node_fs_1.readFileSync)(process.argv[2], "utf8");
|
|
35
|
-
|
|
36
|
-
console.error(e);
|
|
37
|
-
process.exit(1);
|
|
38
|
-
});
|
|
39
|
-
console.log(qr);
|
|
29
|
+
console.log(fromJsonString(file));
|
|
40
30
|
}
|
|
41
31
|
else {
|
|
42
32
|
console.error(`File ${process.argv[2]} doesn't exists`);
|
|
43
33
|
process.exit(1);
|
|
44
34
|
}
|
|
45
35
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const data = JSON.parse(stdin);
|
|
50
|
-
const qrString = (0, generate_js_1.generate)(data);
|
|
51
|
-
resolve(qrString);
|
|
52
|
-
}
|
|
53
|
-
catch (e) {
|
|
54
|
-
reject(e);
|
|
55
|
-
}
|
|
56
|
-
});
|
|
36
|
+
function fromJsonString(stdin) {
|
|
37
|
+
const data = JSON.parse(stdin);
|
|
38
|
+
return (0, generate_js_1.generate)(data);
|
|
57
39
|
}
|
|
58
40
|
async function handleStdin() {
|
|
59
41
|
const readline = (0, node_readline_1.createInterface)({
|
|
@@ -90,18 +72,17 @@ function help() {
|
|
|
90
72
|
"If <file> is omitted, reads from stdin.",
|
|
91
73
|
"",
|
|
92
74
|
"Examples:",
|
|
93
|
-
"
|
|
94
|
-
"",
|
|
95
|
-
"
|
|
96
|
-
"
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
"
|
|
105
|
-
" | bysquare"
|
|
75
|
+
" bysquare <<< \"{",
|
|
76
|
+
" \"invoiceId\": \"random-id\",",
|
|
77
|
+
" \"payments\": [",
|
|
78
|
+
" {",
|
|
79
|
+
" \"type\": 1,",
|
|
80
|
+
" \"amount\": 100.0,",
|
|
81
|
+
" \"bankAccounts\": [{ \"iban\": \"SK9611000000002918599669\" }],",
|
|
82
|
+
" \"currencyCode\": \"EUR\",",
|
|
83
|
+
" \"variableSymbol\": \"123\"",
|
|
84
|
+
" }",
|
|
85
|
+
" ]",
|
|
86
|
+
" }\""
|
|
106
87
|
].join("\n");
|
|
107
88
|
}
|
package/lib/cjs/generate.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
import { DataModel } from "./types.js";
|
|
3
2
|
/**
|
|
4
3
|
* Returns a 2 byte buffer that represents the header of the bysquare
|
|
@@ -13,36 +12,45 @@ import { DataModel } from "./types.js";
|
|
|
13
12
|
* | Reserved | 4 | 0-15 | bits reserved for future needs
|
|
14
13
|
* ```
|
|
15
14
|
*
|
|
16
|
-
* @see 3.5.
|
|
15
|
+
* @see 3.5.
|
|
17
16
|
*/
|
|
18
|
-
export declare function
|
|
17
|
+
export declare function headerBysquare(
|
|
18
|
+
/** dprint-ignore */
|
|
19
|
+
header?: [
|
|
19
20
|
bySquareType: number,
|
|
20
21
|
version: number,
|
|
21
22
|
documentType: number,
|
|
22
23
|
reserved: number
|
|
23
|
-
]):
|
|
24
|
+
]): Uint8Array;
|
|
24
25
|
/**
|
|
25
|
-
*
|
|
26
|
+
* Creates a one-byte array that represents the length of compressed data in
|
|
27
|
+
* combination with CRC32 in bytes.
|
|
26
28
|
*/
|
|
27
|
-
export declare function
|
|
29
|
+
export declare function headerDataLength(length: number): Uint8Array;
|
|
28
30
|
/**
|
|
29
31
|
* Transfer object to a tabbed string and append a CRC32 checksum
|
|
30
32
|
*
|
|
31
|
-
* @see 3.10.
|
|
33
|
+
* @see 3.10.
|
|
32
34
|
*/
|
|
33
|
-
export declare function
|
|
35
|
+
export declare function addChecksum(serialized: string): Uint8Array;
|
|
34
36
|
/**
|
|
35
37
|
* Transform data to ordered tab-separated intermediate representation ready for
|
|
36
38
|
* encoding
|
|
37
39
|
*
|
|
38
|
-
* @see Table 15
|
|
40
|
+
* @see Table 15.
|
|
39
41
|
*/
|
|
40
|
-
export declare function
|
|
42
|
+
export declare function serialize(data: DataModel): string;
|
|
41
43
|
type Options = {
|
|
44
|
+
/**
|
|
45
|
+
* Many banking apps do not support diacritics, which results in errors when
|
|
46
|
+
* serializing data from QR codes.
|
|
47
|
+
*
|
|
48
|
+
* @default true
|
|
49
|
+
*/
|
|
42
50
|
deburr: boolean;
|
|
43
51
|
};
|
|
44
52
|
/**
|
|
45
53
|
* Generate QR string ready for encoding into text QR code
|
|
46
54
|
*/
|
|
47
|
-
export declare function generate(model: DataModel, options?: Options):
|
|
55
|
+
export declare function generate(model: DataModel, options?: Options): string;
|
|
48
56
|
export {};
|