Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Merge pull request #93 from dylanlott/updates
Browse files Browse the repository at this point in the history
add realtime storj coin price
  • Loading branch information
bryanchriswhite committed Nov 14, 2017
2 parents 13f867b + 0a93cce commit 4db19d5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 19 deletions.
3 changes: 2 additions & 1 deletion lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,6 @@ module.exports = {
'storj',
'btc',
'eth'
]
],
STORJ_FALLBACK_PRICE: 0.511
};
68 changes: 50 additions & 18 deletions lib/server/routes/coinpayments.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const middleware = require('storj-service-middleware');
const bodyParser = require('body-parser');
const errors = require('storj-service-error-types');
const log = require('../../logger');
const axios = require('axios');
const Promise = require('bluebird');
const Coinpayments = require('coinpayments');
const coinpayments = require('../vendor/coinpayments');
Expand All @@ -27,6 +28,18 @@ function CoinpaymentsRouter(options) {

inherits(CoinpaymentsRouter, Router);

CoinpaymentsRouter.prototype._currentPrice = function () {
return axios.get('https://min-api.cryptocompare.com/data/price?fsym=STORJ&tsyms=USD')
.then((res) => {
const currentPrice = res.data.USD;
return currentPrice;
})
.catch((err) => {
log.error('error getting STORJ price', err)
return constants.STORJ_FALLBACK_PRICE
});
}

CoinpaymentsRouter.prototype.handleIPN = function (req, res) {
log.info('ipn received', req.body);

Expand All @@ -35,6 +48,13 @@ CoinpaymentsRouter.prototype.handleIPN = function (req, res) {

let credit;

const formatted = {
amount: parseFloat(req.body.amount).toFixed(8),
amounti: parseInt(req.body.amounti),
fee: parseFloat(req.body.fee).toFixed(8),
feei: parseInt(req.body.feei)
}

if (req.body.currency === 'STORJ') {
PaymentProcessor.findOne({
'rawData.address': req.body.address
Expand All @@ -45,19 +65,31 @@ CoinpaymentsRouter.prototype.handleIPN = function (req, res) {
return res.status(500).send('no user found');
}

if (req.body.status < 100) {
return this._currentPrice().then((price) => {
const data = {
proc,
current_price: price
}
return data;
});
})
.then((data) => {
const USD = (formatted.amount * data.current_price) * 100;
req.body.usd = USD;

if (req.body.status !== '100') {
return Credit.findOne({
'data.txn_id': req.body.txn_id,
'user': proc.user
'user': data.proc.user
})
.then((foundCredit) => {
if (!foundCredit) {
credit = new Credit({
paid: false,
invoiced_amount: req.body.amount,
invoiced_amount: USD,
paid_amount: 0,
type: constants.CREDIT_TYPES.MANUAL,
user: proc.user,
type: constants.CREDIT_TYPES.AUTO,
user: data.proc.user,
payment_processor: constants.PAYMENT_PROCESSORS.COINPAYMENTS,
data: req.body
});
Expand All @@ -68,32 +100,32 @@ CoinpaymentsRouter.prototype.handleIPN = function (req, res) {
return res.sendStatus(200)
});
} else {
foundCredit.user = proc.user;
foundCredit.user = data.proc.user;
foundCredit.paid = false;
foundCredit.paid_amount = 0;
foundCredit.invoiced_amount = req.body.amount;
foundCredit.invoiced_amount = USD;

return foundCredit.save()
.then(() => res.status(204).end());
}
})
.catch((err) => {
log.error('err status < 100: ', err);
return res.status(500).send(err)
})
})
.catch((err) => {
log.error('err status < 100: ', err);
return res.status(500).send(err)
})
}

if (req.body.status === 100) {
if (req.body.status === '100') {
return Credit.findOne({
'data.txn_id': req.body.txn_id,
'user': proc.user
'user': data.proc.user
})
.then((credit) => {
if (!credit) {
const credit = new Credit({
paid_amount: req.body.amount,
invoiced_amount: req.body.amount,
user: proc.user,
paid_amount: USD,
invoiced_amount: USD,
user: data.proc.user,
paid: true,
payment_processor: constants.PAYMENT_PROCESSORS.COINPAYMENTS,
type: constants.CREDIT_TYPES.AUTO,
Expand All @@ -105,7 +137,7 @@ CoinpaymentsRouter.prototype.handleIPN = function (req, res) {
}

credit.data = req.body;
credit.paid_amount = req.body.amount;
credit.paid_amount = USD;
credit.paid = true;

return credit.save()
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
},
"dependencies": {
"async": "^1.5.2",
"axios": "^0.17.1",
"coinpayments": "^1.1.2",
"cors": "^2.7.1",
"cron": "^1.1.1",
Expand Down

0 comments on commit 4db19d5

Please sign in to comment.