remove js
This commit is contained in:
parent
57bf994926
commit
164b7f65f8
|
|
@ -184,94 +184,5 @@
|
|||
<% } %>
|
||||
</main>
|
||||
|
||||
<script>
|
||||
const PAY_API = '<%= payApi %>';
|
||||
const SOCK_API = 'https://sock-v1.freedom.st';
|
||||
|
||||
<% if (invoice && (invoice.status === 'pending' || invoice.status === 'underpaid' || invoice.status === 'confirming')) { %>
|
||||
// ── JS enhancements for live updates (progressive, page works without this) ──
|
||||
|
||||
let paymentId = '<%~ invoice.id %>';
|
||||
let paymentData = <%~ JSON.stringify(invoice) %>;
|
||||
let watchedTxids = [<% (invoice.txs || []).forEach(function(tx) { %>'<%= tx.txid %>',<% }) %>];
|
||||
let localReceived = <%= parseFloat(invoice.amount_received || '0') %>;
|
||||
let pollInterval = null;
|
||||
let countdownInterval = null;
|
||||
let eventSource = null;
|
||||
let watchedAddress = '<%= invoice.address %>';
|
||||
|
||||
// Stop the meta refresh since JS is handling it
|
||||
document.querySelector('meta[http-equiv="refresh"]')?.remove();
|
||||
|
||||
// Start SSE
|
||||
eventSource = new EventSource(`${SOCK_API}/sse`);
|
||||
eventSource.onmessage = (e) => {
|
||||
try {
|
||||
const event = JSON.parse(e.data);
|
||||
if (event.type === 'tx') onTx(event);
|
||||
else if (event.type === 'block') onBlock(event);
|
||||
} catch {}
|
||||
};
|
||||
|
||||
function onTx(event) {
|
||||
const outputs = event.data?.out ?? [];
|
||||
const txHash = event.data?.tx?.hash;
|
||||
if (!txHash || watchedTxids.includes(txHash)) return;
|
||||
|
||||
let txValue = 0;
|
||||
for (const out of outputs) {
|
||||
if (out?.script?.address === watchedAddress) txValue += Number(out.value ?? 0);
|
||||
}
|
||||
if (txValue === 0) return;
|
||||
|
||||
watchedTxids.push(txHash);
|
||||
localReceived += txValue;
|
||||
console.log('SSE: tx', txHash, '+' + txValue, 'total:', localReceived);
|
||||
|
||||
const expected = parseFloat(paymentData.amount_crypto);
|
||||
const remaining = Math.max(0, expected - localReceived);
|
||||
|
||||
// Update UI inline
|
||||
const statusEl = document.getElementById('pay-status');
|
||||
if (statusEl) {
|
||||
if (remaining <= expected * 0.005) {
|
||||
statusEl.innerHTML = '<span class="w-2 h-2 rounded-full bg-blue-500 animate-pulse"></span><span class="text-blue-400">Transaction received, waiting for 1 confirmation...</span>';
|
||||
} else {
|
||||
statusEl.innerHTML = '<span class="w-2 h-2 rounded-full bg-yellow-500 animate-pulse"></span><span class="text-yellow-400">Underpaid — please send the remaining amount</span>';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function onBlock(event) {
|
||||
if (watchedTxids.length === 0) return;
|
||||
const blockTxs = event.data?.tx ?? [];
|
||||
if (watchedTxids.some(t => blockTxs.includes(t))) {
|
||||
console.log('SSE: block confirmed');
|
||||
const statusEl = document.getElementById('pay-status');
|
||||
if (statusEl) statusEl.innerHTML = '<span class="text-green-400 font-medium">Payment confirmed!</span>';
|
||||
if (eventSource) eventSource.close();
|
||||
setTimeout(() => { window.location.href = '/dashboard/settings'; }, 2000);
|
||||
}
|
||||
}
|
||||
|
||||
// Poll as fallback
|
||||
pollInterval = setInterval(async () => {
|
||||
try {
|
||||
const res = await fetch(`${PAY_API}/checkout/${paymentId}`, { credentials: 'include' });
|
||||
const data = await res.json();
|
||||
if (data.status === 'paid' || data.status === 'expired') {
|
||||
window.location.reload();
|
||||
}
|
||||
} catch {}
|
||||
}, 10000);
|
||||
|
||||
<% } %>
|
||||
|
||||
// Clipboard copy (progressive)
|
||||
function copyAddress() {
|
||||
const addr = document.getElementById('pay-address');
|
||||
if (addr) navigator.clipboard.writeText(addr.textContent);
|
||||
}
|
||||
</script>
|
||||
|
||||
<%~ include('./partials/foot') %>
|
||||
|
|
|
|||
Loading…
Reference in New Issue