-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreditor_reference.js
52 lines (41 loc) · 1.05 KB
/
creditor_reference.js
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
function generate_creditor_reference( ref ) {
if ( /^[A-Za-z0-9]{0,21}$/.test( ref ) ) {
ref = ref.toUpperCase();
let cr = '';
for ( let i = 0; i < ref.length; i++ ) {
let cc = ref.charCodeAt(i);
if ( 65 > cc ) {
cr += cc - 48;
} else {
cr += cc - 55;
}
}
cr += '271500';
while ( cr.length > 10 ) {
cr = ( cr.substring( 0, 10 ) % 97 ) + cr.substring( 10 );//concat
}
return 'RF' + ( 98 - cr % 97 + '' ).padStart( 2, '0' ) + ref;
}
}
function check_creditor_reference( ref ) {
if ( /^RF\d{2}[A-Za-z0-9]{0,21}$/.test( ref ) ) {
ref = ref.toUpperCase();
ref = ref.substring( 4 ) + ref.substring( 0, 4 );
let cr = '';
for ( let i = 0; i < ref.length; i++ ) {
let cc = ref.charCodeAt(i);
if ( 65 > cc ) {
cr += cc - 48;
} else {
cr += cc - 55;
}
}
while ( cr.length > 10 ) {
cr = ( cr.substring( 0, 10 ) % 97 ) + cr.substring( 10 );//concat
}
if ( 1 == cr % 97 ) {
return true;
}
}
return false;
}