Created
March 24, 2015 09:44
-
-
Save lukem512/a4b7c752d3377a3105ea to your computer and use it in GitHub Desktop.
Insight Payment Watcher for StartCOIN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>StartCOIN Payment Watcher</title> | |
<script src="https://cdn.socket.io/socket.io-1.3.4.js"></script> | |
<script> | |
// Create an XML request | |
var createRequest = function() { | |
var result = null; | |
if (window.XMLHttpRequest) { | |
// FireFox, Safari, etc. | |
result = new XMLHttpRequest(); | |
if (typeof result.overrideMimeType != 'undefined') { | |
result.overrideMimeType('text/xml'); // Or anything else | |
} | |
} | |
else if (window.ActiveXObject) { | |
// MSIE | |
result = new ActiveXObject("Microsoft.XMLHTTP"); | |
} | |
else { | |
// No can do | |
console.error("Cannot create XML HTTP request. Please use a newer browser."); | |
} | |
return result; | |
} | |
// Handle incoming data by passing it, as an object, to a callback | |
var createAndHandleRequest = function(url, callback) { | |
var req = createRequest(); | |
req.onreadystatechange = function() { | |
// switch on state | |
if (req.readyState != 4) { | |
// waiting... | |
return; | |
} | |
if (req.status != 200) { | |
console.error('Error code 200'); | |
return; | |
} | |
// success parse results | |
eval("var resp = ("+req.responseText+")"); | |
callback(resp); | |
}; | |
req.open("GET", url, true); | |
req.send(); | |
} | |
// Retrieve transaction information, given a TXID | |
var getTx = function(txid, callback) { | |
createAndHandleRequest("http://explorer.startcoin.org/api/tx/"+txid, callback); | |
} | |
// Retrieve block information, given a hash | |
var getBlock = function(hash, callback) { | |
createAndHandleRequest("http://explorer.startcoin.org/api/block/"+hash, callback); | |
} | |
// Number of confirmations to wait for | |
var SAFE_CONFIRMATIONS = 1; | |
// Invoice details, hard-coded for demo | |
// The address is treated as a blank slate - any existing balance is ignored | |
var INVOICE_ADDRESS = "sSHKfyUUMzNtAF5kBFNSYHUC2xg3RG7Dgv"; | |
var INVOICE_AMOUNT = 0.5; | |
/* | |
Array of objects in the form: | |
{ | |
txid: "", | |
amount: 0, | |
confirmations: 0 | |
} | |
*/ | |
var transactions = []; | |
// Amount of invoice paid | |
var amountPaid = 0; | |
// Open socket to StartCOIN Explorer | |
var socket = io("http://direct.insight2.startcoin.org/"); | |
// Join the room once it's connected | |
socket.on('connect', function() { | |
// Join the room. | |
socket.emit('subscribe', 'inv'); | |
}); | |
// Watch for new transactions | |
socket.on('tx', function(data) { | |
// New transaction received | |
// Wait a few seconds for Insight to catch up | |
window.setTimeout(function(){ | |
getTx(data.txid, function(response) { | |
var i = 0; | |
for (i = 0; i < response.vout.length; i++) { | |
if (response.vout[i].scriptPubKey.addresses[0] == INVOICE_ADDRESS) { | |
// Transaction to invoice address | |
console.log('Payment transaction received in ' + response.txid + ' for ' + response.vout[i].value); | |
var transaction = { | |
txid: response.txid, | |
value: parseFloat(response.vout[i].value), | |
confirmations: 0 | |
}; | |
transactions.push(transaction); | |
} | |
} | |
}, 5000); | |
}); | |
}); | |
// Watch for new blocks | |
socket.on('block', function(hash) { | |
// New block received | |
// Wait a few seconds for Insight to catch up | |
window.setTimeout(function(){ | |
getBlock(hash, function(response) { | |
var i = 0, j = 0; | |
for (i = 0; i < response.tx.length; i++) { | |
for (j = 0; j < transactions.length; j++) { | |
if (response.tx[i] == transactions[j].txid) { | |
console.log('Incrementing confirmations for transaction ' + transactions[j].txid); | |
transactions[j].confirmations++; | |
if (transactions[j].confirmations == SAFE_CONFIRMATIONS) { | |
amountPaid += transactions[j].value; | |
console.log('Confirming payment of ' + transactions[j].value + '. Total paid is now ' + amountPaid); | |
} | |
} | |
} | |
} | |
if (amountPaid >= INVOICE_AMOUNT) { | |
console.log('Payment complete!'); | |
// Unsubscribe from socket io to save resources | |
socket.removeAllListeners('tx'); | |
socket.removeAllListeners('block'); | |
} | |
}); | |
}, 5000); | |
}); | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment