forked from oelna/javascript-girocode-bezahlcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
187 lines (155 loc) · 5.27 KB
/
script.js
File metadata and controls
187 lines (155 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
'use strict';
const output = {
'girocode': document.querySelector('#girocode'),
'bezahlcode': document.querySelector('#bezahlcode')
};
const fields = {
'bic': document.querySelector('#bic'),
'recipient': document.querySelector('#recipient'),
'iban': document.querySelector('#iban'),
'currency': document.querySelector('#currency'),
'amount': document.querySelector('#amount'),
'reason': document.querySelector('#reason')
};
const settings = {
'barcodeSize': 384,
'darkColor': '#000000',
'lightColor': '#ffffff'
};
const replaceSpecialCharacters = function(input) {
let value = input.replace(/Ö/g, 'OE');
value = value.replace(/Ü/g, 'UE');
value = value.replace(/Ä/g, 'AE');
value = value.replace(/ä/g, 'ae');
value = value.replace(/ö/g, 'oe');
value = value.replace(/ü/g, 'ue');
value = value.replace(/ß/g, 'ss');
return value;
};
const giroCodeString = function (params) {
const sep = "\n";
const data = {
'service': 'BCD',
'version': params.bic.trim() ? '001' : '002',
'encoding': '1', // 1 = UTF-8, 2 = ISO 8859-1
'transfer': 'SCT',
'bic': params.bic.trim(),
'name': replaceSpecialCharacters(params.name).trim().substring(0,70), // max of 70 characters
'iban': params.iban.replace(' ', '').trim(),
'currency': params.currency.trim().toUpperCase(),
'amount': parseFloat(params.amount.trim()),
'char': '', // 4 digit key - like SALA, ...
'ref': '', // reference - alternative to reason, max of 25 characters
'reason': replaceSpecialCharacters(params.reason).trim().replace(sep, ' ').substring(0, 140), // max of 140 characters
'hint': '' // max of 70 characters
};
let epcString = data.service;
epcString += sep + data.version;
epcString += sep + data.encoding;
epcString += sep + data.transfer;
epcString += sep + data.bic;
epcString += sep + data.name;
epcString += sep + data.iban;
epcString += sep + data.currency + data.amount;
epcString += sep + data.char;
epcString += sep + data.ref;
epcString += sep + data.reason;
epcString += sep + data.hint;
return epcString;
}
const makeGiroCode = function () {
const str = giroCodeString({
'bic': fields.bic.value,
'name': fields.recipient.value,
'iban': fields.iban.value,
'currency': fields.currency.value,
'amount': fields.amount.value,
'reason': fields.reason.value
});
makeQR(output.girocode, str);
}
const bezahlCodeString = function (params) {
const data = {};
data.bic = params.bic.toUpperCase().trim();
data.name = params.name.toUpperCase().trim();
data.iban = params.iban.replace(' ', '').toUpperCase().trim();
data.amount = parseFloat(params.amount.trim().replace(',', '.')).toString().replace('.', ',');
data.reason = params.reason.trim().replace("\n", ' ').substring(0, 140);
if (params.currency.trim().toUpperCase() != 'EUR') {
alert('Bezahlcode unterstützt nur Beträge in Euro!');
return '';
}
const parameters = new URLSearchParams(data).toString();
const bezahlCodeString = 'bank://singlepaymentsepa?' + parameters.replace('+', '%20');
return bezahlCodeString;
}
const makeBezahlCode = function () {
const str = bezahlCodeString({
'bic': fields.bic.value,
'name': fields.recipient.value,
'iban': fields.iban.value,
'currency': fields.currency.value,
'amount': fields.amount.value,
'reason': fields.reason.value
});
makeQR(output.bezahlcode, str);
}
const makeQR = function (ele, str) {
if (str.length > 0) {
const bezahlCode = new QRCode(ele, {
width : settings.barcodeSize,
height : settings.barcodeSize,
colorDark : settings.darkColor,
colorLight : settings.lightColor,
correctLevel : QRCode.CorrectLevel.M,
useSVG: true
});
bezahlCode.makeCode(str);
}
}
const updateHash = function() {
const oSearchParams = new URLSearchParams({
'bic': fields.bic.value,
'name': fields.recipient.value,
'iban': fields.iban.value,
'currency': fields.currency.value,
'amount': fields.amount.value,
'reason': fields.reason.value
});
location.hash = '#' + oSearchParams.toString();
}
const updateFromhash = function() {
const oSearchParams = new URLSearchParams(location.hash.substring(1));
fields.bic.value = oSearchParams.get('bic');
fields.recipient.value = oSearchParams.get('name');
fields.iban.value = oSearchParams.get('iban');
fields.currency.value = oSearchParams.get('currency');
fields.amount.value = oSearchParams.get('amount');
fields.reason.value = oSearchParams.get('reason');
}
const updateCode = function (e) {
e.preventDefault();
// todo: do some sanity value checking here
makeGiroCode();
makeBezahlCode();
updateHash();
}
document.querySelector('#generate').addEventListener('click', updateCode);
document.querySelectorAll('a[download]').forEach(function (ele, i) {
ele.addEventListener('click', function (e) {
const svg = e.target.parentNode.querySelector('.code svg');
if (!svg) { return false; }
svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
ele.setAttribute('href', 'data:image/svg+xml;utf8,' + svg.outerHTML);
});
});
updateFromhash();
addEventListener("hashchange", updateFromhash)
/*
// fill in test data
document.querySelector('#bic').value = 'SOLADES1PFD';
document.querySelector('#recipient').value = 'Girosolution GmbH';
document.querySelector('#iban').value = 'DE19690516200000581900';
document.querySelector('#amount').value = '22.30';
document.querySelector('#reason').value = 'testzweck';
*/