diff --git a/nodejs/callback.js b/nodejs/callback.js index 97d5548..e2ec05e 100644 --- a/nodejs/callback.js +++ b/nodejs/callback.js @@ -1,26 +1,26 @@ -const app = new (require('express').Router)(); +const app = new (require('express').Router)() app.post('/paybear/callback/:order', (req, res) => { - if(req.body && req.params.order) { - var orderId = req.params.order; - var data = req.body; - var invoice = data.invoice; + if (req.body && req.params.order) { + // const orderId = req.params.order + const data = req.body + const invoice = data.invoice - //save data.confirmations - number of confirmations to DB + // save data.confirmations - number of confirmations to DB - if(data.confirmations >= data.maxConfirmations) { - var amountPaid = data.inTransaction.amount / Math.pow(10, data.inTransaction.exp); - //compare $amountPaid with order total - //compare $invoice with one saved in the database to ensure callback is legitimate - //mark the order as paid - res.send(invoice); //stop further callbacks + if (data.confirmations >= data.maxConfirmations) { + // const amountPaid = data.inTransaction.amount / Math.pow(10, data.inTransaction.exp) + + // compare $amountPaid with order total + // compare $invoice with one saved in the database to ensure callback is legitimate + // mark the order as paid + res.send(invoice) // stop further callbacks + } else { + res.send('waiting for confirmations') + } } else { - res.send('waiting for confirmations'); + res.send('error') } -} else { - res.send('error'); -} - -}); +}) -module.exports = app; \ No newline at end of file +module.exports = app diff --git a/nodejs/currencies.js b/nodejs/currencies.js index 0c51f68..236d780 100644 --- a/nodejs/currencies.js +++ b/nodejs/currencies.js @@ -1,37 +1,37 @@ -var getCurrency = require('./paybear').getCurrency; -var getCurrencies = require('./paybear').getCurrencies; -const app = new (require('express').Router)(); +const getCurrency = require('./paybear').getCurrency +const getCurrencies = require('./paybear').getCurrencies +const app = new (require('express').Router)() app.get('/paybear/currencies', (req, res) => { - if(req.query.order) { - var orderId = +req.query.order; - var fiatTotal = 19.99; //get from order + if (req.query.order) { + const orderId = +req.query.order + // const fiatTotal = 19.99 // get from order - var token = req.query.token; + const token = req.query.token - if(token) { - getCurrency(token, orderId, true, function (curr) { - res.json(curr); //return this data to PayBear form - }); + if (token) { + getCurrency(token, orderId, true, (curr) => { + res.json(curr) // return this data to PayBear form + }) } else { - var returnCurrs = []; - getCurrencies(function(currs) { - var collectCurrencies = function (currCodes) { - if(currCodes.length === 0) { - res.json(returnCurrs); - return; + const returnCurrs = [] + getCurrencies((currs) => { + const collectCurrencies = (currCodes) => { + if (currCodes.length === 0) { + res.json(returnCurrs) + return } - getCurrency(currCodes.pop(), orderId, true, function(currency) { - returnCurrs.push(currency); - collectCurrencies(currCodes); - }); - }; - collectCurrencies(Object.keys(currs)); - }); + getCurrency(currCodes.pop(), orderId, true, (currency) => { + returnCurrs.push(currency) + collectCurrencies(currCodes) + }) + } + collectCurrencies(Object.keys(currs)) + }) } } else { - res.json({error: 'send the order number'}); + res.json({error: 'send the order number'}) } -}); +}) -module.exports = app; \ No newline at end of file +module.exports = app diff --git a/nodejs/paybear.js b/nodejs/paybear.js index 14fd5d1..6bf3230 100644 --- a/nodejs/paybear.js +++ b/nodejs/paybear.js @@ -1,104 +1,103 @@ -var https = require('https'); +const https = require('https') -var PAYBEAR_SECRET = 'secAPIKEY'; +const PAYBEAR_SECRET = 'secAPIKEY' -function getAddress(orderId, token, callback) { - var callbackUrl = 'http://CHANGEME.com/payBear/callback/' + orderId; - var url = 'https://api.paybear.io/v2/' + token.toLowerCase() + '/payment/' + '?token=' + PAYBEAR_SECRET; +function getAddress (orderId, token, callback) { + // const callbackUrl = `http://CHANGEME.com/payBear/callback/${orderId}` + const url = `https://api.paybear.io/v2/${token.toLowerCase()}/payment/?token=${PAYBEAR_SECRET}` - https.get(url, function (res) { - var rawData = ''; - res.on('data', function (chunk) { rawData += chunk; }); + https.get(url, (res) => { + let rawData = '' + res.on('data', (chunk) => { rawData += chunk }) - res.on('end', function () { - var response = JSON.parse(rawData); + res.on('end', () => { + const response = JSON.parse(rawData) if (response.success) { - callback(response.data.address); + callback(response.data.address) } - }); - }). - on('error', function (e) { - console.error(e); - callback(null); - }); + }) + }) + .on('error', function (e) { + console.error(e) + callback(null) + }) } -function getCurrencies(callback) -{ - var currencies = null; //TODO: add cache here? - - var url = 'https://api.paybear.io/v2/currencies?token=' + PAYBEAR_SECRET; - - https.get(url, function (res) { - var rawData = ''; - res.on('data', function (chunk) { rawData += chunk; }); - - res.on('end', function () { - var response = JSON.parse(rawData); - if (response.success) { - callback(response.data); - } - }); - }). - on('error', function (e) { - console.error(e); - callback(null); - }); +function getCurrencies (callback) { + // const currencies = null // TODO: add cache here? + + const url = `https://api.paybear.io/v2/currencies?token=${PAYBEAR_SECRET}` + + https.get(url, (res) => { + let rawData = '' + res.on('data', (chunk) => { rawData += chunk }) + + res.on('end', () => { + const response = JSON.parse(rawData) + if (response.success) { + callback(response.data) + } + }) + }) + .on('error', (e) => { + console.error(e) + callback(null) + }) } -function getCurrency(token, orderId, getAddr, callback) { - getRate(token, function(rate) { +function getCurrency (token, orderId, getAddr, callback) { + getRate(token, (rate) => { if (rate) { - var fiatValue = 19.99; //get from $orderId - var coinsValue = +(fiatValue / rate).toFixed(8); - var currency = null; - - getCurrencies(function(currencies) { - token = token.toLowerCase(); - currency = currencies[token]; - currency['coinsValue'] = coinsValue; - - if (getAddr) { - getAddress(orderId, token, function(address) { - currency['address'] = address; - currency['blockExplorer'] = currency['blockExplorer'] + address; - callback(currency); - }); - } else { - currency['currencyUrl'] = '/paybear/currencies?order=' + orderId + '&token=' + token; - callback(currency); - } - }); - + const fiatValue = 19.99 // get from $orderId + const coinsValue = +(fiatValue / rate).toFixed(8) + let currency = null + + getCurrencies((currencies) => { + token = token.toLowerCase() + currency = currencies[token] + currency['coinsValue'] = coinsValue + + if (getAddr) { + getAddress(orderId, token, (address) => { + currency['address'] = address + currency['blockExplorer'] = currency['blockExplorer'] + address + callback(currency) + }) + } else { + currency['currencyUrl'] = `/paybear/currencies?order=${orderId}&token=${token}` + callback(currency) + } + }) } - }); + }) } -function getRate(curCode, callback) { - curCode = curCode.toLowerCase(); +function getRate (curCode, callback) { + curCode = curCode.toLowerCase() getRates(function (rates) { - rates[curCode] ? callback(rates[curCode].mid) : callback(false); - }); + if (rates[curCode]) callback(rates[curCode].mid) + else callback(false) + }) } -function getRates(callback) { - var url = 'https://api.paybear.io/v2/exchange/usd/rate'; +function getRates (callback) { + const url = 'https://api.paybear.io/v2/exchange/usd/rate' - https.get(url, function (res) { - var rawData = ''; - res.on('data', function (chunk) { rawData += chunk; }); + https.get(url, (res) => { + let rawData = '' + res.on('data', (chunk) => { rawData += chunk }) - res.on('end', function () { - var response = JSON.parse(rawData); + res.on('end', () => { + const response = JSON.parse(rawData) if (response.success) { - callback(response.data); + callback(response.data) } - }); - }). - on('error', function (e) { - console.error(e); - callback(null); - }); + }) + }) + .on('error', (e) => { + console.error(e) + callback(null) + }) } module.exports = { @@ -107,4 +106,4 @@ module.exports = { getCurrencies: getCurrencies, getRate: getRate, getRates: getRates -}; +} diff --git a/nodejs/status.js b/nodejs/status.js index b5a6b82..2dd3d6d 100644 --- a/nodejs/status.js +++ b/nodejs/status.js @@ -1,16 +1,14 @@ -const app = new (require('express').Router)(); +const app = new (require('express').Router)() app.get('/paybear/status/:order', (req, res) => { - var orderId = req.params.order; -var confirmations = null; - -confirmations = 0; //get from DB, see callback.php -maxConfirmations = 3; //get from DB, see callback.php -var resp = { - success: confirmations >= maxConfirmations -}; -if(confirmations !== null) - resp.confirmations = confirmations; -res.json(resp); //return this data to PayBear form -}); -module.exports = app; + // const orderId = req.params.order + const confirmations = 0 // get from DB, see callback.php + const maxConfirmations = 3 // get from DB, see callback.php + const resp = { + success: confirmations >= maxConfirmations + } + if (confirmations !== null) { resp.confirmations = confirmations } + res.json(resp) // return this data to PayBear form +}) + +module.exports = app