-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
326 lines (278 loc) · 10.5 KB
/
app.js
File metadata and controls
326 lines (278 loc) · 10.5 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* ================================================================
QR-Gen — Application Logic
================================================================ */
(function () {
'use strict';
// --- DOM refs ---
const $ = (s) => document.querySelector(s);
const tabBtns = document.querySelectorAll('.tab-btn');
const tabBar = $('.tab-bar');
// Link → QR tab
const urlInput = $('#url-input');
const urlLabelInput = $('#url-label');
const btnLinkGenerate = $('#btn-link-generate');
const qrOutputLink = $('#qr-output-link');
const qrCanvasLink = $('#qr-canvas-link');
const qrLinkLabel = $('#qr-link-label');
const qrLinkUrl = $('#qr-link-url');
const btnDownloadLink = $('#btn-download-link');
const btnCopyUrl = $('#btn-copy-url');
// UPI QR tab
const upiIdInput = $('#upi-id');
const payeeInput = $('#payee-name');
const amountInput = $('#amount');
const anyAmountToggle = $('#any-amount-toggle');
const expiryInput = $('#expiry-date');
const noExpiryToggle = $('#no-expiry-toggle');
const expiryHint = $('#expiry-hint');
const txnNoteInput = $('#txn-note');
const btnGenerate = $('#btn-generate');
const qrOutput = $('#qr-output');
const qrCanvas = $('#qr-canvas');
const qrPayeeLabel = $('#qr-payee-label');
const qrUpiLabel = $('#qr-upi-label');
const qrAmountLabel = $('#qr-amount-label');
const qrExpiryLabel = $('#qr-expiry-label');
const qrStatusBadge = $('#qr-status-badge');
const upiLinkDisplay = $('#upi-link-display');
const btnDownload = $('#btn-download');
const btnCopyLink = $('#btn-copy-link');
const toast = $('#toast');
const toastText = $('#toast-text');
let currentQR = null;
let currentLinkQR = null;
let currentUpiLink = '';
let currentUrl = '';
// --- Tabs ---
tabBtns.forEach(btn => {
btn.addEventListener('click', () => {
const tab = btn.dataset.tab;
tabBtns.forEach(b => { b.classList.remove('active'); b.setAttribute('aria-selected', 'false'); });
btn.classList.add('active');
btn.setAttribute('aria-selected', 'true');
tabBar.dataset.active = tab;
document.querySelectorAll('.tab-panel').forEach(p => p.classList.remove('active'));
$(`#panel-${tab}`).classList.add('active');
});
});
// --- Toggles ---
anyAmountToggle.addEventListener('change', () => {
amountInput.disabled = anyAmountToggle.checked;
if (anyAmountToggle.checked) {
amountInput.value = '';
clearError('amount');
}
});
noExpiryToggle.addEventListener('change', () => {
expiryInput.disabled = noExpiryToggle.checked;
if (noExpiryToggle.checked) {
expiryInput.value = '';
expiryHint.textContent = 'QR code will never expire';
} else {
const future = new Date();
future.setDate(future.getDate() + 30);
expiryInput.value = formatDate(future);
expiryInput.min = formatDate(new Date());
expiryHint.textContent = 'QR will be invalid after this date';
}
});
// --- Helpers ---
function formatDate(d) {
return d.toISOString().split('T')[0];
}
function showToast(msg) {
toastText.textContent = msg;
toast.classList.remove('hidden');
toast.classList.add('visible');
setTimeout(() => {
toast.classList.remove('visible');
}, 2400);
}
function setError(id, msg) {
const el = $(`#${id}-error`);
const input = $(`#${id}`);
if (el) el.textContent = msg;
if (input) input.classList.add('has-error');
}
function clearError(id) {
const el = $(`#${id}-error`);
const input = $(`#${id}`);
if (el) el.textContent = '';
if (input) input.classList.remove('has-error');
}
function clearAllErrors() {
document.querySelectorAll('.field-error').forEach(e => e.textContent = '');
document.querySelectorAll('.has-error').forEach(e => e.classList.remove('has-error'));
}
function buildUpiLink(pa, pn, am, tn) {
let link = `upi://pay?pa=${encodeURIComponent(pa)}&pn=${encodeURIComponent(pn)}&cu=INR`;
if (am) link += `&am=${am}`;
if (tn) link += `&tn=${encodeURIComponent(tn)}`;
return link;
}
function validateUpiId(val) {
return /^[\w.\-]+@[\w]+$/.test(val);
}
function isValidUrl(str) {
// Accept any non-empty string that looks like a URL or common shorthand
if (!str) return false;
// Add protocol if missing
if (!/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//.test(str)) {
str = 'https://' + str;
}
try {
new URL(str);
return true;
} catch {
return false;
}
}
function normalizeUrl(str) {
if (!/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//.test(str)) {
return 'https://' + str;
}
return str;
}
function renderQR(container, data) {
container.innerHTML = '';
const qr = new QRCode(container, {
text: data,
width: 200,
height: 200,
colorDark: '#111111',
colorLight: '#ffffff',
correctLevel: QRCode.CorrectLevel.H
});
return qr;
}
function downloadQR(frameSelector, filename) {
const frame = $(frameSelector);
const canvas = frame.querySelector('canvas');
if (!canvas) { showToast('No QR code to download'); return; }
const exportCanvas = document.createElement('canvas');
const pad = 40;
const size = canvas.width + pad * 2;
exportCanvas.width = size;
exportCanvas.height = size + 32;
const ctx = exportCanvas.getContext('2d');
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, exportCanvas.width, exportCanvas.height);
ctx.drawImage(canvas, pad, pad);
ctx.fillStyle = '#aaaaaa';
ctx.font = '11px Inter, sans-serif';
ctx.textAlign = 'center';
ctx.fillText('Generated by QR-Gen', exportCanvas.width / 2, exportCanvas.height - 10);
const link = document.createElement('a');
link.download = filename || 'payqr-code.png';
link.href = exportCanvas.toDataURL('image/png');
link.click();
showToast('QR code downloaded!');
}
function copyToClipboard(text, label) {
navigator.clipboard.writeText(text).then(() => {
showToast(`${label} copied to clipboard!`);
}).catch(() => {
const ta = document.createElement('textarea');
ta.value = text;
document.body.appendChild(ta);
ta.select();
document.execCommand('copy');
document.body.removeChild(ta);
showToast(`${label} copied!`);
});
}
function isExpired(dateStr) {
if (!dateStr) return false;
const expiry = new Date(dateStr);
const today = new Date();
today.setHours(0, 0, 0, 0);
return expiry < today;
}
// =============================================
// TAB 1 — Link → QR (any URL)
// =============================================
btnLinkGenerate.addEventListener('click', () => {
clearAllErrors();
const raw = urlInput.value.trim();
const label = urlLabelInput.value.trim();
if (!raw) {
setError('url-input', 'Please enter a URL');
return;
}
if (!isValidUrl(raw)) {
setError('url-input', 'Enter a valid URL (e.g. https://example.com)');
return;
}
currentUrl = normalizeUrl(raw);
currentLinkQR = renderQR(qrCanvasLink, currentUrl);
qrLinkLabel.textContent = label || 'QR Code';
qrLinkUrl.textContent = currentUrl;
qrOutputLink.classList.remove('hidden');
qrOutputLink.classList.add('visible');
qrOutputLink.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
});
btnDownloadLink.addEventListener('click', () => downloadQR('#qr-frame-link', 'payqr-link.png'));
btnCopyUrl.addEventListener('click', () => {
if (currentUrl) copyToClipboard(currentUrl, 'Link');
});
// =============================================
// TAB 2 — UPI QR
// =============================================
btnGenerate.addEventListener('click', () => {
clearAllErrors();
let valid = true;
const upiId = upiIdInput.value.trim();
const payee = payeeInput.value.trim();
const amount = anyAmountToggle.checked ? '' : amountInput.value.trim();
const note = txnNoteInput.value.trim();
const expiryDate = noExpiryToggle.checked ? '' : expiryInput.value;
if (!upiId) { setError('upi-id', 'UPI ID is required'); valid = false; }
else if (!validateUpiId(upiId)) { setError('upi-id', 'Enter a valid UPI ID (e.g. name@upi)'); valid = false; }
if (!payee) { setError('payee-name', 'Payee name is required'); valid = false; }
if (!anyAmountToggle.checked) {
if (!amount || parseFloat(amount) <= 0) {
amountInput.classList.add('has-error');
valid = false;
}
}
if (!valid) return;
const expired = isExpired(expiryDate);
const link = buildUpiLink(upiId, payee, amount, note);
currentUpiLink = link;
currentQR = renderQR(qrCanvas, link);
qrPayeeLabel.textContent = payee;
qrUpiLabel.textContent = upiId;
qrAmountLabel.textContent = amount ? `₹${parseFloat(amount).toFixed(2)}` : 'Any Amount';
if (expiryDate) {
const d = new Date(expiryDate);
qrExpiryLabel.textContent = expired
? `Expired on ${d.toLocaleDateString('en-IN', { day: 'numeric', month: 'short', year: 'numeric' })}`
: `Valid until ${d.toLocaleDateString('en-IN', { day: 'numeric', month: 'short', year: 'numeric' })}`;
} else {
qrExpiryLabel.textContent = 'No expiration';
}
qrStatusBadge.textContent = expired ? 'Expired' : 'Active';
qrStatusBadge.className = 'qr-badge' + (expired ? ' expired' : '');
upiLinkDisplay.textContent = link;
qrOutput.classList.remove('hidden');
qrOutput.classList.add('visible');
qrOutput.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
});
btnDownload.addEventListener('click', () => downloadQR('#qr-frame', 'payqr-upi.png'));
btnCopyLink.addEventListener('click', () => {
if (currentUpiLink) copyToClipboard(currentUpiLink, 'UPI link');
});
// --- Clear errors on typing ---
[upiIdInput, payeeInput, amountInput].forEach(input => {
input.addEventListener('input', () => {
input.classList.remove('has-error');
const errEl = input.parentElement.querySelector('.field-error');
if (errEl) errEl.textContent = '';
});
});
urlInput.addEventListener('input', () => {
urlInput.classList.remove('has-error');
const errEl = urlInput.parentElement.querySelector('.field-error');
if (errEl) errEl.textContent = '';
});
})();