-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
188 lines (164 loc) · 5.55 KB
/
script.js
File metadata and controls
188 lines (164 loc) · 5.55 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
188
document.getElementById('fileInput').addEventListener('change', handleFile, false);
const dropArea = document.getElementById('drop-area');
// Prevent default drag behaviors
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, preventDefaults, false);
});
// Highlight drop area when item is dragged over it
['dragenter', 'dragover'].forEach(eventName => {
dropArea.addEventListener(eventName, highlight, false);
});
['dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, unhighlight, false);
});
dropArea.addEventListener('drop', handleDrop, false);
function preventDefaults(e) {
e.preventDefault();
e.stopPropagation();
}
function highlight() {
dropArea.classList.add('highlight');
}
function unhighlight() {
dropArea.classList.remove('highlight');
}
function handleDrop(e) {
const dt = e.dataTransfer;
const files = dt.files;
handleFile({ target: { files: files } });
}
function handleFile(event) {
const file = event.target.files[0];
if (!file) {
return;
}
const reader = new FileReader();
reader.onload = function (e) {
const img = new Image();
img.onload = function () {
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
context.drawImage(img, 0, 0);
const imageData = context.getImageData(0, 0, canvas.width, canvas.height);
const code = jsQR(imageData.data, imageData.width, imageData.height);
if (code) {
const decodedText = code.data;
document.getElementById('qrText').innerText = decodedText;
const ppInfo = extractPPInfo(decodedText);
displayPPInfo(ppInfo);
} else {
alert('No QR code found.');
}
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
}
function _decode(ppText) {
const ppObj = {};
let fieldNo = '';
let fieldSize = '';
for (let i = 0; i < ppText.length; i++) {
if (fieldNo === '') {
fieldNo = ppText.substring(i, i + 2);
i += 1;
} else if (fieldSize === '') {
fieldSize = parseInt(ppText.substring(i, i + 2)).toString();
i += 1;
} else {
ppObj[fieldNo] = ppText.substring(i, i + parseInt(fieldSize));
i += parseInt(fieldSize) - 1;
fieldNo = '';
fieldSize = '';
}
}
return ppObj;
}
function decode(ppText) {
const obj = _decode(ppText);
const merchantInfo = obj['29'];
if (merchantInfo) {
const merchantObj = _decode(merchantInfo);
if (merchantObj['00'] !== 'A000000677010111') {
return null;
}
Object.entries(merchantObj).forEach(([k, v]) => {
if (k !== '00') {
obj['29_acc_type'] = k;
obj['29_acc_no'] = v;
}
});
}
const merchantInfo30 = obj['30'];
if (merchantInfo30) {
const merchantObj = _decode(merchantInfo30);
if (merchantObj['00'] === 'A000000677010112') {
Object.entries(merchantObj).forEach(([k, v]) => {
if (k === '01') {
obj['30_biller_id'] = v;
} else if (k === '02') {
obj['30_merchant_id'] = v;
} else if (k === '03') {
obj['30_txn_id'] = v;
}
});
}
}
const merchantInfo31 = obj['31'];
if (merchantInfo31) {
const merchantObj = _decode(merchantInfo31);
Object.entries(merchantObj).forEach(([k, v]) => {
if (k === '02') {
obj['31_merchant_id'] = v;
} else if (k === '04') {
obj['31_txn_id'] = v;
}
});
}
const merchantInfo62 = obj['62'];
if (merchantInfo62) {
const merchantObj = _decode(merchantInfo62);
Object.entries(merchantObj).forEach(([k, v]) => {
if (k === '07') {
obj['62_ref_3'] = v;
}
});
}
return obj;
}
function extractPPInfo(content) {
const obj = decode(content);
if (obj) {
let merchantId = obj['30_biller_id'] || obj['29_acc_no'] || '';
let accountType = 'UNKNOWN';
if (merchantId) {
if (merchantId.length === 11 || (merchantId.length === 13 && merchantId.startsWith('0066'))) {
accountType = 'MSISDN';
merchantId = merchantId.replace('0066', '0');
} else if (merchantId.length === 13) {
accountType = 'NATID';
} else if (merchantId.length === 15) {
accountType = 'EWALLET';
}
}
let merchantName = obj['59'] || merchantId;
let amount = obj['54'] || '0.00';
accountType = obj['30_biller_id'] ? 'BILLERID' : accountType;
return {
merchantName: merchantName,
merchantId: merchantId,
accountType: accountType,
amount: amount
};
} else {
throw new Error(`Invalid format:\n\n${content}`);
}
}
function displayPPInfo(ppInfo) {
document.getElementById('merchantName').innerText = ppInfo.merchantName;
document.getElementById('accountType').innerText = ppInfo.accountType;
document.getElementById('merchantId').innerText = ppInfo.merchantId;
document.getElementById('amount').innerText = ppInfo.amount;
}