I am new to bitcoin, Blockchain.info API and javascript, however, i am trying to implement a code that tracks Live Payments notification on a particular bitcoin address. The idea here is... after the user scans the QR image <img src="http://chart.googleapis.com/chart?chs=200x200&cht=qr&chl=12fMma2J15qre9bZPsX3AerdgWd9Poe9ee">
, and makes payment to the BTC address, 12fMma2J15qre9bZPsX3AerdgWd9Poe9ee
, the Div with ID #websocket
will instantly display Live (without refreshing the webpage), the amount of Bitcoins Transaferred to the address, thus switching the initial content of the div from monitoring...
to the amount transferred Recieved: 0.003 BTC
.
I have written a piece of code ... but i'm not sure what i'm missing. Please Help. Thank you.
The code:
<div class="row">
<div class="col-md-4 ">
<img src="http://chart.googleapis.com/chart?chs=200x200&cht=qr&chl=12fMma2J15qre9bZPsX3AerdgWd9Poe9ee">
<div id="websocket">
Monitoring Transactions ...
</div>
<script>
var btcs = new WebSocket("12fMma2J15qre9bZPsX3AerdgWd9Poe9ee");
btcs.onopen = function() {
btcs.send(JSON.stringify({"op":"addr_sub", "addr":"12fMma2J15qre9bZPsX3AerdgWd9Poe9ee"}));
};
btcs.onmessage = function (onmsg) {
var response = JSON.parse(onmsg.data);
var getOutputs = response.x.out;
var countOuts = getOutputs.length;
for (i=0; i < countOuts; i++){
var outAdd = response.x.out[i].addr;
var address = "12fMma2J15qre9bZPsX3AerdgWd9Poe9ee";
if (outAdd == address){
var amount =response.x.out[i].value;
var calAmount = Amount / 100000000;
document.getElementById("websocket").innerHTML = "Recieved" + calAmount + "BTC";
}
}
};
</script>
</div>
<div class="col-md-8">
<!-- more html stuff goes here -->
</div>
</div>