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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
#= require jquery-1.8.2.min.js
#= require moment.min.js
#= require qr.js
#= require bootstrap.min.js
#= require 2.5.3-crypto-sha256.js
#= require jsbn.js
#= require jsbn2.js
#= require check_address.js
EXCHANGE_FAIL = "Error fetching exchange rate"
SOCKET_FAIL = "Error connecting to payment server"
ADDRESS_FAIL = "Invalid address"
g = exports ? this
$(->
g.user = $('#user').val()
g.name = get('name')
g.address = get('address')
g.symbol = get('symbol')
g.commission = parseFloat(get('commission'))
g.logo = get('logo')
g.errors = []
if g.user
$.ajax(
url: g.user + '.json',
cache: false,
dataType: 'json',
success: (data) ->
if data?
g.name = data.name
g.address = data.address
g.symbol = data.symbol
g.commission = data.commission
g.logo = data.logo
setup()
)
else
setup()
$('#amount').keyup(updateTotal)
$('#amount').focus(->
$('#received').hide()
$('#payment').fadeIn('slow')
$(this).val('')
updateTotal()
)
)
setup = ->
g.address or= '1VAnbtCAnYccECnjaMCPnWwt81EHCVgNr'
g.commission or= 0
g.symbol or= 'virtexCAD'
if g.logo
$('#logo').attr('src', g.logo).show()
else if not g.name
$('#logo').attr('src', 'img/bitcoin.png').show()
else
$('#name').html(g.name).show()
address = g.address
if g.user? and g.user
address = "<a href='/#{g.user}/report'>#{address}</a>"
if check_address(g.address)
$('#address').html(address)
else
fail(ADDRESS_FAIL)
symbol = g.symbol
symbol += " - #{parseFloat(g.commission).toFixed(0)}%" if g.commission > 0
$('#symbol').html(symbol)
$('#currency').html(g.symbol.slice(-3))
$('#received').hide()
setupQR()
setupSocket()
fetchExchangeRate()
fetchExchangeRate = ->
$.ajax(
url: "ticker?symbol=#{g.symbol}&type=ask&amount=1000",
cache: false,
success: (exchange) ->
if exchange?
clear(EXCHANGE_FAIL)
else
fail(EXCHANGE_FAIL)
return
unless g.setupComplete
finalize()
g.exchange = exchange - exchange * g.commission * 0.01
$('#exchange').val(g.exchange.toFixed(2))
updateTotal()
error: -> fail(EXCHANGE_FAIL)
)
setTimeout(fetchExchangeRate, 900000)
finalize = ->
$('#amount').focus()
g.setupComplete = true
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":"' + g.address + '"}')
g.websocket.onerror = g.websocket.onclose = ->
fail(SOCKET_FAIL)
g.websocket.onmessage = (e) ->
results = eval('(' + e.data + ')')
from_address = ''
total = 0
received = 0
$.each(results.x.out, (i, v) ->
if (v.addr == g.address)
received += v.value / 100000000
)
$.each(results.x.inputs, (i, v) ->
from_address = v.prev_out.addr
if (v.prev_out.addr == g.address)
input -= v.prev_out.value / 100000000
)
if (total <= received)
$('#amount').blur()
$('#payment').hide()
$('#received').fadeIn('slow')
if g.user
$.post("/#{g.user}/transactions",
address: from_address,
date: moment().format("YYYY-MM-DD HH:mm:ss"),
received: received,
exchange: g.exchange
)
updateTotal = ->
amount = parseFloat($('#amount').val())
total = amount / g.exchange
total = Math.ceil(total * 10000) / 10000
unless $.isNumeric(total)
total = ''
$('#total').html(total.toString())
displayQR('bitcoin:' + g.address + '?amount=' + total.toString())
fail = (msg) ->
g.errors.push(msg)
g.errors = g.errors.uniq()
$('#calculator').hide()
$('#error').show().html(g.errors.toString())
clear = (msg) ->
i = g.errors.indexOf(msg)
g.errors.splice(i, 1) if i >= 0
if (g.errors.length > 0)
$('#error').show().html(g.errors.toString())
else
$('#error').hide()
$('#calculator').fadeIn('slow')
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, " "))
Array::uniq = ->
output = {}
output[@[key]] = @[key] for key in [0...@length]
value for key, value of output
|