Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve node.js sample code #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions nodejs/callback.js
Original file line number Diff line number Diff line change
@@ -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;
module.exports = app
54 changes: 27 additions & 27 deletions nodejs/currencies.js
Original file line number Diff line number Diff line change
@@ -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;
module.exports = app
161 changes: 80 additions & 81 deletions nodejs/paybear.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -107,4 +106,4 @@ module.exports = {
getCurrencies: getCurrencies,
getRate: getRate,
getRates: getRates
};
}
26 changes: 12 additions & 14 deletions nodejs/status.js
Original file line number Diff line number Diff line change
@@ -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