-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
163 lines (144 loc) · 3.44 KB
/
index.ts
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
interface QrisParse {
PayloadFormatIndicator: string
PointOfInitiationMethod: string
QRISIssuer: string
QRISGlobalMerchantID: string
QRISAcquirerMerchantID: string
MerchantBusinessType: string
MerchantCategoryCode: string
TransactionCurrency: string
CountryCode: string
MerchantName: string
MerchantCity: string
PostalCode: string
CRC: string
}
const qrisParse = (str: string): QrisParse => {
let tags = {
CountryCode: '',
CRC: '',
MerchantBusinessType: '',
MerchantCategoryCode: '',
MerchantCity: '',
MerchantName: '',
PayloadFormatIndicator: '',
PointOfInitiationMethod: '',
PostalCode: '',
QRISAcquirerMerchantID: '',
QRISGlobalMerchantID: '',
QRISIssuer: '',
TransactionCurrency: ''
}
const emvTags: { [key: string]: any } = {
'00': {
id: '00',
name: 'PayloadFormatIndicator',
format: 'N',
presence: 'M'
},
'01': {
id: '01',
name: 'PointOfInitiationMethod',
format: 'N',
presence: 'O'
},
'52': {
id: '52',
name: 'MerchantCategoryCode',
format: 'N',
presence: 'M'
},
'53': {
id: '53',
name: 'TransactionCurrency',
format: 'N',
presence: 'M'
},
'54': {
id: '54',
name: 'TransactionAmount',
format: 'ans',
presence: 'C'
},
'58': {
id: '58',
name: 'CountryCode',
format: 'ans',
presence: 'M'
},
'59': {
id: '59',
name: 'MerchantName',
format: 'ans',
presence: 'M'
},
'60': {
id: '60',
name: 'MerchantCity',
format: 'ans',
presence: 'M'
},
'61': {
id: '61',
name: 'PostalCode',
format: 'ans',
presence: 'O'
},
'63': {
id: '63',
name: 'CRC',
format: 'ans',
presence: 'M',
valid: true // check that it valid CRC
}
}
const _parseTag = (emvdata: string) => {
let index = 0
while (index < emvdata.length) {
const data = emvdata.substring(index)
const tagId = data.substr(0, 2)
const tagLength = parseInt(data.substr(2, 2))
const tagInformation = data.substr(4, tagLength)
if (tagId === '26') {
qris26Parse(tagInformation)
}
if (tagId === '51') {
// still nothing
}
if (tagId in emvTags) {
const key = emvTags[tagId].name
tags = {
...tags,
[key]: tagInformation
}
}
index += tagLength + 4
}
return tags
}
const qris26Parse = (qrisdata: string) => {
let index = 0
while (index < qrisdata.length) {
const data = qrisdata.substring(index)
const tagId = data.substr(0, 2)
const tagLength = parseInt(data.substr(2, 2))
const tagInformation = data.substr(4, tagLength)
if (tagId === '00') {
tags.QRISIssuer = tagInformation
}
if (tagId === '01') {
tags.QRISGlobalMerchantID = tagInformation
}
if (tagId === '02') {
tags.QRISAcquirerMerchantID = tagInformation
}
if (tagId === '03') {
tags.MerchantBusinessType = tagInformation
}
index += tagLength + 4
}
return tags
}
return _parseTag(str)
}
console.log(qrisParse('00020101021126640017ID.CO.BANKBSI.WWW0118936004510000080974021000009127860303UMI51440014ID.CO.QRIS.WWW0215ID20221485322620303UMI5204839853033605802ID5919BARKASMAL NUSANTARA6006SLEMAN61055528162070703A0163043C29'))