aboutsummaryrefslogtreecommitdiffstats
path: root/index.html
blob: 724cc489890bf7f6d5301e5ec04c9ea8012aa071 (plain) (blame)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Bitcoin Payment Calculator</title>
    <script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.8.1.min.js"></script>

    <script language="javascript" type="text/javascript">
      $(function() {
        var websocket = new WebSocket("ws://api.blockchain.info:8335/inv");
        var exchange = 0;
        var total = 0;
        var received = 0;
        var address = getParameterByName('address');
        var qr_url = 'https://chart.googleapis.com/chart?cht=qr&chl=bitcoin:' + address + '&chs=177x177&chld=H'

        if (address == "")
          window.location = 'index.html?address=1SoLtySab29J2tAdF2x3PosjnwWF37wEY'

        $('#qr').attr('src', qr_url);
        $('#address').html(address);

        websocket.onopen = function() { 
          websocket.send('{"op":"addr_sub", "addr":"' + address + '"}');
        }

        websocket.onmessage = function(e) { 
          var results = eval('(' + e.data + ')');
          
          $.each(results.x.out, function(i, v) {
            if (v.addr == address) {
              received += v.value / 100000000;
            }
          });

          $.each(results.x.inputs, function(i, v) {
            if (v.prev_out.addr == address) {
              input -= v.prev_out.value / 100000000;
            }
          });

          $('#status').html('Received: ' + received.toString());
          if (total <= received) {
            $('#status').css('color', 'green');
          }
        }

        $.getJSON('ticker', function (data) {
          exchange = data.return.last.value;  
          $('#exchange').html(exchange);
          updateTotal();
        });

        $('#owing').keyup(updateTotal);
        $('#owing').focus();

        function updateTotal() {
          var owing = parseFloat($('#owing').val());
          total = owing / exchange;
          total = Math.ceil(total * 10000) / 10000;
          if (!$.isNumeric(total)) total = '';
          $('#total').html(total.toString());
        }
      });

      function getParameterByName(name) {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regexS = "[\\?&]" + name + "=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec(window.location.search);
        if(results == null)
          return "";
        else
          return decodeURIComponent(results[1].replace(/\+/g, " "));
      }
    </script>

    <style>
      body, input { font-family: 'Arial'; font-size: 20px; }
      body { width: 100% }
      #container { width: 50%; margin: 0px auto; }
      #total { font-size: 32px; }
      #status { color: red }
    </style>
  </head>
  <body>
    <div id="container">
      <div>
        $<input type="text" id="owing" value="0" size="4" /> CAD @
        <span id="exchange"></span>
        BTC/CAD
      </div>

      <br />

      <div>
        <span>Please send</span>
        <strong id="total">0.0</strong>
        bitcoins to:

        <br /><br />

        <span id="address"></span>

        <br />

        <img id="qr" src="" width="300" height="300" />
        
        <br />
        
        <span id="status">Waiting for Payment</span>
      </div>
    </div>
  </body>
</html>