bitcoin plugin & cache

This commit is contained in:
jysperm
2014-09-29 04:44:16 +08:00
parent 80add60f32
commit bb606c18a4
5 changed files with 90 additions and 41 deletions

View File

@@ -44,6 +44,7 @@ exports.run = ->
app.i18n = require './core/i18n'
app.utils = require './core/utils'
app.cache = require './cache'
app.config = require './config'
app.package = require './package.json'
app.billing = require './core/billing'

View File

@@ -0,0 +1,43 @@
stringify = require 'json-stable-stringify'
{redis, config} = app
exports.hashKey = (key, param) ->
return "#{config.redis.prefix}:#{key}" + stringify(param)
# @param [options] {param, command, is_json}
# @param setter(callback(value), param)
# @param callback(value)
exports.try = (key, options, setter, callback) ->
unless callback
callback = setter
setter = options
options = {}
options.param ?= {}
options.command ?= exports.SET()
key = exports.hashKey key, options.param
redis.get key, (err, value) ->
if value != undefined
if options.is_json
value = JSON.prase value
callback value
else
setter options.param, (value) ->
if options.is_json
value = JSON.stringify value
options.command key, value, ->
callback value
exports.SET = ->
return (key, value, callback) ->
redis.set key, value, callback
exports.SETEX = (seconds) ->
return (key, value, callback) ->
redis.setex key, seconds, value, callback

View File

@@ -41,6 +41,7 @@
"redis": "^0.12.1",
"request": "^2.40.0",
"tmp": "^0.0.24",
"underscore": "^1.6.0"
"underscore": "^1.6.0",
"json-stable-stringify": "^1.0.0"
}
}

View File

@@ -0,0 +1,33 @@
request = require 'request'
{config, cache} = app
# @param callback(address)
exports.genAddress = (bitcoin_secret, callback) ->
request 'https://coinbase.com/api/v1/account/generate_receive_address',
method: 'POST'
json:
api_key: config.plugins.bitcoin.coinbase_api_key
address:
callback_url: "#{config.web.url}/bitcoin/coinbase_callback?secret=#{bitcoin_secret}"
, (err, res, body) ->
throw err if err
callback body.address
# @param currency: CNY, USD, JPY etc.
# @param callback(rate)
exports.getExchangeRate = (currency, callback) ->
cache.try 'bitcoin.getExchangeRate',
param: currency: currency
command: cache.SETEX 60
, (callback) ->
request 'https://blockchain.info/ticker', (err, res, body) ->
throw err if err
body = JSON.parse body
rate = 1 / parseFloat(body[currency]['15m'])
callback parseFloat rate
, (rate) ->
callback rate

View File

@@ -1,51 +1,22 @@
request = require 'request'
bitcoin = require './bitcoin'
{mAccount} = app.models
{pluggable, redis, config} = app
{pluggable, config} = app
module.exports =
name: 'bitcoin'
type: 'extension'
# @param callback(address)
genAddress = (bitcoin_secret, callback) ->
request 'https://coinbase.com/api/v1/account/generate_receive_address',
method: 'POST'
json:
api_key: config.bitcoin.coinbase_api_key
address:
callback_url: "#{config.web.url}/bitcoin/coinbase_callback?secret=#{bitcoin_secret}"
, (err, res, body) ->
throw err if err
callback body.address
pluggable.registerHook 'account.before_register', module.exports,
filter: (account, callback) ->
bitcoin_secret = exports.randomSalt()
# @param currency: CNY, USD, JPY etc.
# @param callback(rate)
getExchangeRate = (currency, callback) ->
REDIS_KEY = "#{config.redis.prefix}:[bitcoin.getExchangeRate]:#{currency}"
bitcoin.genAddress bitcoin_secret, (address) ->
account.pluggable.bitcoin =
bitcoin_deposit_address: address
bitcoin_secret: bitcoin_secret
redis.get REDIS_KEY, (err, rate) ->
if rate
callback rate
else
request 'https://blockchain.info/ticker', (err, res, body) ->
throw err if err
body = JSON.parse body
rate = 1 / parseFloat(body[currency]['15m'])
app.redis.setex REDIS_KEY, 60, rate, ->
callback parseFloat rate
pluggable.hooks.account.before_register.psuh (account, callback) ->
bitcoin_secret = exports.randomSalt()
genAddress bitcoin_secret, (address) ->
account.pluggable.bitcoin =
bitcoin_deposit_address: address
bitcoin_secret: bitcoin_secret
callback()
callback()
app.post '/bitcoin/coinbase_callback', (req, res) ->
mAccount.findOne
@@ -57,7 +28,7 @@ app.post '/bitcoin/coinbase_callback', (req, res) ->
unless req.query.secret == account.pluggable.bitcoin.bitcoin_secret
return res.send 400, 'Invalid Secret'
getExchangeRate config.billing.currency, (rate) ->
bitcoin.getExchangeRate config.billing.currency, (rate) ->
amount = req.body.amount / rate
mAccount.incBalance account, 'deposit', amount,