import bitcoin plugin

This commit is contained in:
jysperm
2014-11-01 18:50:00 +08:00
parent fffcb94373
commit 5c0e8a01b1
9 changed files with 34 additions and 29 deletions

View File

@@ -14,6 +14,7 @@ app.libs =
express: require 'express'
fs: require 'fs'
harp: require 'harp'
jade: require 'jade'
markdown: require('markdown').markdown
middlewareInjector: require 'middleware-injector'
moment: require 'moment-timezone'
@@ -23,6 +24,7 @@ app.libs =
path: require 'path'
redis: require 'redis'
redisStore: require 'connect-redis'
request: require 'request'
expressSession: require 'express-session'
mongooseUniqueValidator: require 'mongoose-unique-validator'

View File

@@ -138,6 +138,8 @@ Account.statics.register = (account, callback) ->
language: 'auto'
timezone: config.i18n.default_timezone
pluggable: {}
async.each pluggable.selectHook(account, 'account.before_register'), (hook, callback) ->
hook.filter account, callback
, ->

View File

@@ -144,14 +144,14 @@ exports.initializePlugins = ->
throw new Error "#{plugin_name} is Dependent on #{dependence} but not load"
for plugin_name in all_plugins
plugin_path = path.join __dirname, "../plugin/#{name}"
plugin_path = path.join __dirname, "../plugin/#{plugin_name}"
plugin = require plugin_path
if fs.existsSync path.join(plugin_path, 'locale')
i18n.loadForPlugin plugin
if fs.existsSync path.join(plugin_path, 'static')
app.use harp.mount("/plugin/#{name}", path.join(plugin_path, 'static'))
app.use harp.mount("/plugin/#{plugin_name}", path.join(plugin_path, 'static'))
exports.createHelpers = (plugin) ->
plugin.registerHook = (hook_name, payload) ->

View File

@@ -1,6 +1,7 @@
describe 'billing', ->
billing = null
config = null
utils = null
Account = null
Financials = null
@@ -8,12 +9,12 @@ describe 'billing', ->
account = null
before ->
{billing, config} = app
{billing, utils, config} = app
{Account, Financials} = app.models
account = new Account
username: 'billing'
email: 'billing@gmail.com'
username: "test#{utils.randomString(10).toLowerCase()}"
email: "#{utils.randomString 20}@gmail.com"
billing:
services: []
plans: []

View File

@@ -22,8 +22,7 @@ describe 'cache', ->
redis.get 'RP:test_key', (err, value) ->
value.should.be.equal 'test_key_value'
done()
done err
it 'should success when cache exist', (done) ->
cache.try 'test_key', ->

View File

@@ -10,6 +10,7 @@ describe 'router/panel', ->
.end done
it 'GET pay', (done) ->
@timeout 15000
agent.get '/panel/pay'
.expect 200
.end done

View File

@@ -1,9 +1,12 @@
request = require 'request'
{config, cache} = app
{request} = app.libs
{config, cache, logger} = app
# @param callback(address)
exports.genAddress = (bitcoin_secret, callback) ->
if config.plugins.bitcoin.coinbase_api_key == 'coinbase-simple-api-key'
app.deprecate 'Invalid coinbase-simple-api-key'
return callback()
request 'https://coinbase.com/api/v1/account/generate_receive_address',
method: 'POST'
json:
@@ -11,23 +14,22 @@ exports.genAddress = (bitcoin_secret, callback) ->
address:
callback_url: "#{config.web.url}/bitcoin/coinbase_callback?secret=#{bitcoin_secret}"
, (err, res, body) ->
throw err if err
logger.error 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) ->
cache.try
key: 'bitcoin.getExchangeRate'
currency: currency
, (SETEX) ->
request 'https://blockchain.info/ticker', (err, res, body) ->
throw err if err
logger.error if err
body = JSON.parse body
rate = 1 / parseFloat(body[currency]['15m'])
callback parseFloat rate
, (rate) ->
callback rate
SETEX rate, 60
, callback

View File

@@ -1,11 +1,9 @@
jade = require 'jade'
path = require 'path'
{jade, path} = app.libs
{Account} = app.models
{pluggable, config, utils} = app
bitcoin = require './bitcoin'
{mAccount} = app.models
{pluggable, config, utils} = app
module.exports = pluggable.createHelpers exports =
name: 'bitcoin'
type: 'extension'
@@ -35,8 +33,8 @@ exports.registerHook 'view.pay.display_payment_details',
order_id: deposit_log.payload.order_id
short_order_id: deposit_log.payload.order_id[0 .. 40]
app.post '/bitcoin/coinbase_callback', (req, res) ->
mAccount.findOne
app.express.post '/bitcoin/coinbase_callback', (req, res) ->
Account.findOne
'pluggable.bitcoin.bitcoin_deposit_address': req.body.address
, (err, account) ->
unless account
@@ -48,7 +46,7 @@ app.post '/bitcoin/coinbase_callback', (req, res) ->
bitcoin.getExchangeRate config.billing.currency, (rate) ->
amount = req.body.amount / rate
mAccount.incBalance account, 'deposit', amount,
Account.incBalance amount, 'deposit',
type: 'bitcoin'
order_id: req.body.transaction.hash
, ->

View File

@@ -14,7 +14,7 @@ module.exports =
default_timezone: 'Asia/Shanghai'
plugin:
available_extensions: []
available_extensions: ['bitcoin']
available_services: []
billing: