-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwoocommerce-lld-gateway
More file actions
87 lines (78 loc) · 3.29 KB
/
woocommerce-lld-gateway
File metadata and controls
87 lines (78 loc) · 3.29 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
// assets/woocommerce-lld-gateway.js
(function () {
function renderFromData() {
try {
var container = document.getElementById('lld-payment-instructions') || document.getElementById('lld-qrcode');
if (!container) return;
// support both data-gateway (new) and data-url (older)
var gateway = container.getAttribute('data-gateway') || container.getAttribute('data-url') || container.dataset.gateway || container.dataset.url;
if (!gateway) {
container.innerText = (window.lld_payment_vars && lld_payment_vars.error_text) ? lld_payment_vars.error_text : 'Payment link not available.';
return;
}
// Clear
container.innerHTML = '';
// Info label
var label = document.createElement('p');
label.innerText = (window.lld_payment_vars && lld_payment_vars.scan_text) ? lld_payment_vars.scan_text : 'Scan to pay';
container.appendChild(label);
// QR container
var qrDiv = document.createElement('div');
container.appendChild(qrDiv);
// Generate QR
if (typeof QRCode === 'function') {
try {
new QRCode(qrDiv, {
text: gateway,
width: 180,
height: 180,
colorDark: '#000000',
colorLight: '#ffffff',
correctLevel: QRCode.CorrectLevel.H
});
} catch (e) {
console.error('LLD QR error', e);
container.appendChild(document.createTextNode((window.lld_payment_vars && lld_payment_vars.error_text) ? lld_payment_vars.error_text : 'QR generation failed.'));
}
} else {
// attempt to load library then render
var script = document.createElement('script');
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js';
script.onload = function () {
try {
new QRCode(qrDiv, { text: gateway, width: 180, height: 180 });
} catch (e) { console.error(e); }
};
document.body.appendChild(script);
}
// Gateway link
var link = document.createElement('a');
link.href = gateway;
link.target = '_blank';
link.innerText = (window.lld_payment_vars && lld_payment_vars.link_text) ? lld_payment_vars.link_text : 'Open gateway';
link.style.display = 'block';
link.style.marginTop = '10px';
container.appendChild(link);
// Copyable fallback (try to parse address & price)
try {
var u = new URL(gateway);
var p = new URLSearchParams(u.search);
var toId = p.get('toId') || p.get('address');
var price = p.get('price') || p.get('amount');
if (toId) {
var fallback = document.createElement('div');
fallback.style.marginTop = '8px';
fallback.innerHTML = '<strong>Address:</strong> <code>' + toId + '</code><br><strong>Price (LLD):</strong> ' + (price || '') ;
container.appendChild(fallback);
}
} catch (err) {
// ignore parse errors
}
} catch (err) {
console.error('LLD render error', err);
}
}
document.addEventListener('DOMContentLoaded', renderFromData);
document.addEventListener('updated_checkout', renderFromData);
setTimeout(renderFromData, 600);
})();