mirror of
https://github.com/HackPlan/RootPanel.git
synced 2026-03-26 07:44:10 +08:00
import bitcoin plugin
This commit is contained in:
@@ -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'
|
||||
|
||||
|
||||
@@ -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
|
||||
, ->
|
||||
|
||||
@@ -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) ->
|
||||
|
||||
@@ -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: []
|
||||
|
||||
@@ -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', ->
|
||||
|
||||
@@ -10,6 +10,7 @@ describe 'router/panel', ->
|
||||
.end done
|
||||
|
||||
it 'GET pay', (done) ->
|
||||
@timeout 15000
|
||||
agent.get '/panel/pay'
|
||||
.expect 200
|
||||
.end done
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
, ->
|
||||
|
||||
@@ -14,7 +14,7 @@ module.exports =
|
||||
default_timezone: 'Asia/Shanghai'
|
||||
|
||||
plugin:
|
||||
available_extensions: []
|
||||
available_extensions: ['bitcoin']
|
||||
available_services: []
|
||||
|
||||
billing:
|
||||
|
||||
Reference in New Issue
Block a user