aboutsummaryrefslogtreecommitdiffstats
path: root/public/js/calculator.coffee
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2012-11-07 17:46:49 -0800
committerAdam Soltys <[email protected]>2012-11-07 17:52:50 -0800
commit3e900e0bba211c2bbf2bb10c85a82dd9ce6943f0 (patch)
treebb3ce237413df5fb0896a1696f7bd6f41ac248f3 /public/js/calculator.coffee
parent5a4e3ff9bf1db2db4fc5ae8d3bd7dcefc8c58e25 (diff)
downloadcoinos-server-3e900e0bba211c2bbf2bb10c85a82dd9ce6943f0.tar.gz
coinos-server-3e900e0bba211c2bbf2bb10c85a82dd9ce6943f0.zip
Use coffeescript
Diffstat (limited to 'public/js/calculator.coffee')
-rw-r--r--public/js/calculator.coffee145
1 files changed, 145 insertions, 0 deletions
diff --git a/public/js/calculator.coffee b/public/js/calculator.coffee
new file mode 100644
index 00000000..ed199920
--- /dev/null
+++ b/public/js/calculator.coffee
@@ -0,0 +1,145 @@
+#= require jquery-1.8.2.min.js
+#= require moment.min.js
+#= require qr.js
+#= require bootstrap.min.js
+
+g = exports ? this
+
+$(->
+ g.websocket = null
+ g.client = $('#client').val()
+ g.title = get('title')
+ g.address = get('address')
+ g.commission = get('commission')
+ g.logo = get('logo')
+ g.exchange = 0
+
+ setupPage()
+ setupQR()
+ setupSocket()
+
+ if client? and client
+ $.getJSON('client/' + client, (data) ->
+ user = data[0]
+ g.title = user.title
+ g.address = user.address
+ g.commission = user.commission
+ g.logo = user.logo
+ setupPage()
+ )
+
+ $('#amount').keyup(updateTotal)
+ $('#amount').focus()
+ $('#amount').focus(->
+ $('#received').hide()
+ $('#payment').fadeIn('slow')
+ $(this).val('')
+ updateTotal()
+ )
+)
+
+updateTotal = ->
+ amount = parseFloat($('#amount').val())
+ total = amount / exchange
+ total = Math.ceil(total * 10000) / 10000
+
+ unless $.isNumeric(total)
+ total = ''
+
+ $('#total').html(total.toString())
+ displayQR('bitcoin:' + address + '?amount=' + total.toString())
+
+exchangeFail = ->
+ $('#error').show().html("Error fetching exchange rate")
+ $('#calculator').hide()
+
+fetchExchangeRate = ->
+ $.getJSON('ticker')
+ .success((data) ->
+ unless data?
+ exchangeFail()
+ return
+
+ exchange = 1000 / data.out
+ exchange = exchange - exchange * commission * 0.01
+ g.exchange = Math.ceil(exchange * 100) / 100
+ $('#exchange').val(exchange.toFixed(2))
+ updateTotal()
+
+ $('#error').hide()
+ $('#calculator').fadeIn('slow')
+ ).error(exchangeFail)
+ setTimeout(fetchExchangeRate, 900000)
+
+setupPage = ->
+ unless address
+ address = '1VAnbtCAnYccECnjaMCPnWwt81EHCVgNr'
+
+ unless commission
+ commission = 3
+
+ if logo
+ $('#logo').attr('src', logo).show()
+ else
+ $('#title').html(title)
+
+ $('#address').html(address)
+ $('#received').hide()
+
+ fetchExchangeRate()
+
+setupSocket = ->
+ setTimeout(setupSocket, 10000)
+
+ unless g.websocket and g.websocket.readyState is 1
+ g.websocket = new WebSocket("ws://api.blockchain.info:8335/inv")
+
+ g.websocket.onopen = ->
+ g.websocket.send('{"op":"addr_sub", "addr":"' + address + '"}')
+
+
+ g.websocket.onerror = g.websocket.onclose = ->
+ $('#calculator').hide()
+ $('#error').show().html("Error connecting to payment server")
+
+
+ g.websocket.onmessage = (e) ->
+ results = eval('(' + e.data + ')')
+ from_address = ''
+ total = 0
+ received = 0
+
+ $.each(results.x.out, (i, v) ->
+ if (v.addr == address)
+ received += v.value / 100000000
+ )
+
+ $.each(results.x.inputs, (i, v) ->
+ from_address = v.prev_out.addr
+ if (v.prev_out.addr == address)
+ input -= v.prev_out.value / 100000000
+ )
+
+ if (total <= received)
+ $('#amount').blur()
+ $('#payment').hide()
+ $('#received').fadeIn('slow')
+
+ $.get('record_transaction.php',
+ address: from_address,
+ date: moment().format("YYYY-MM-DD HH:mm:ss"),
+ received: received,
+ exchange: g.exchange
+ )
+
+get = (name) ->
+ name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]")
+ regexS = "[\\?&]" + name + "=([^&#]*)"
+ regex = new RegExp(regexS)
+ results = regex.exec(window.location.search)
+
+ if (results == null)
+ return ""
+ else
+ return decodeURIComponent(results[1].replace(/\+/g, " "))
+