Skip to content

Instantly share code, notes, and snippets.

@dhjw
Last active September 23, 2019 02:42
Show Gist options
  • Save dhjw/bb700aec7da2c521ac0f303bb52375d0 to your computer and use it in GitHub Desktop.
Save dhjw/bb700aec7da2c521ac0f303bb52375d0 to your computer and use it in GitHub Desktop.
Accept BCH donations
<?php
// address used throughout page and script. could also just be hardcoded without PHP.
$addr = 'qqvppp5pe4sce8qyvyncdrhekgkxcyw2hczvervmp8';
?>
<html>
<head>
<title>Donate BCH</title>
<style>
body { font:16pt arial; }
#address { font:18pt arial; }
#address a, #address a:visited { color:black; text-decoration:none; }
#address a:hover { text-decoration:underline; }
#receivedText { font:18pt arial; color:green; }
</style>
</head>
<body>
<center>
Send your donation to:<br>
<br>
<div id="address"><a target="_blank" href="https://blockchair.com/bitcoin-cash/address/<?=$addr?>"><?=$addr?></a></div>
<br>
<img src="https://chart.googleapis.com/chart?cht=qr&chs=240x240&chld=L|0&chl=<?=$addr?>"><br>
<br>
<div id="receivedText"></div>
<script>
var addr = '<?=$addr?>', startBalance = 0, looptime = 12000;
// run immediately and get initial balance
checkPayment(1);
// run every looptime (1000 = 1s)
setInterval(checkPayment, looptime);
// check payment
function checkPayment(start){
if(!start && !document.hasFocus()) return; // avoid api calls when tab not visible
// ajax
var xhr = new XMLHttpRequest();
xhr.timeout = looptime;
xhr.open('GET','https://rest.bitcoin.com/v2/address/details/' + addr);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) { // request done
if (xhr.status === 200) { // success
try { var r = JSON.parse(xhr.responseText); } catch(e){}
// console.log('api response=', r);
if (r) {
var total = r.balance + r.unconfirmedBalance;
if (start || total < startBalance) {
startBalance = total;
return;
}
if (total > startBalance) {
// console.log('payment received');
document.querySelector('#receivedText').innerHTML = 'Thank you for your donation!';
setTimeout(()=>{ document.querySelector('#receivedText').innerHTML = ''; }, looptime - 2000);
startBalance = total;
}
}
} else console.log('ajax error: ' + xhr.status);
}
}
xhr.send();
}
</script>
</body>
</html>
@dhjw
Copy link
Author

dhjw commented Sep 20, 2019

Test at http://irc.dw1.xyz/donate/

License: Do whatever you want with it. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment