2019-01-01 11:26:51 -05:00
|
|
|
var request = require('request-promise');
|
2018-11-22 20:36:28 -05:00
|
|
|
var options = require("../connect");
|
|
|
|
var common = require('../common');
|
|
|
|
|
2019-01-08 22:04:34 -05:00
|
|
|
exports.getInvoice = (req, res, next) => {
|
|
|
|
options.url = common.lnd_server_url + '/invoice/' + req.params.rHashStr;
|
|
|
|
request(options).then((body) => {
|
|
|
|
console.log(`Invoice Information Received: ${JSON.stringify(body)}`);
|
|
|
|
if(undefined === body || body.error) {
|
|
|
|
res.status(500).json({
|
|
|
|
message: "Fetching Invoice Info Failed!",
|
|
|
|
error: (undefined === body) ? 'Error From Server!' : body.error
|
|
|
|
});
|
|
|
|
}
|
|
|
|
res.status(200).json(body);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
return res.status(500).json({
|
|
|
|
message: "Fetching Invoice Info Failed!",
|
|
|
|
error: err.error
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-11-22 20:36:28 -05:00
|
|
|
exports.listInvoices = (req, res, next) => {
|
|
|
|
options.url = common.lnd_server_url + '/invoices';
|
2019-01-01 11:26:51 -05:00
|
|
|
request(options).then((body) => {
|
2018-11-22 20:36:28 -05:00
|
|
|
const body_str = (undefined === body) ? '' : JSON.stringify(body);
|
|
|
|
const search_idx = (undefined === body) ? -1 : body_str.search('Not Found');
|
|
|
|
console.log('Information Received: ' + body_str);
|
|
|
|
if(undefined === body || search_idx > -1 || body.error) {
|
|
|
|
res.status(500).json({
|
|
|
|
message: "Fetching Invoice Info failed!",
|
|
|
|
error: (undefined === body || search_idx > -1) ? 'Error From Server!' : body.error
|
|
|
|
});
|
|
|
|
} else {
|
2018-11-25 09:25:22 -05:00
|
|
|
if (undefined !== body.invoices) {
|
2018-11-25 08:56:00 -05:00
|
|
|
body.invoices.forEach(invoice => {
|
|
|
|
invoice.creation_date_str = (undefined === invoice.creation_date) ? '' : common.convertTimestampToDate(invoice.creation_date);
|
|
|
|
invoice.settle_date_str = (undefined === invoice.settle_date) ? '' : common.convertTimestampToDate(invoice.settle_date);
|
|
|
|
invoice.btc_value = (undefined === invoice.value) ? 0 : common.convertToBTC(invoice.value);
|
|
|
|
invoice.btc_amt_paid_sat = (undefined === invoice.amt_paid_sat) ? 0 : common.convertToBTC(invoice.amt_paid_sat);
|
|
|
|
});
|
|
|
|
}
|
2018-11-25 00:07:59 -05:00
|
|
|
console.log('\nInvoices Received: ' + JSON.stringify(body));
|
2018-11-22 20:36:28 -05:00
|
|
|
res.status(200).json(body);
|
|
|
|
}
|
2019-01-01 11:26:51 -05:00
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
return res.status(500).json({
|
|
|
|
message: "Fetching Invoice Info failed!",
|
|
|
|
error: err.error
|
|
|
|
});
|
2018-11-22 20:36:28 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.addInvoice = (req, res, next) => {
|
|
|
|
options.url = common.lnd_server_url + '/invoices';
|
|
|
|
options.form = JSON.stringify({
|
|
|
|
memo: req.body.memo,
|
|
|
|
value: req.body.amount
|
|
|
|
});
|
|
|
|
console.log('Add Invoice Options Form:' + options.form);
|
2019-01-01 11:26:51 -05:00
|
|
|
request.post(options).then((body) => {
|
2018-11-22 20:36:28 -05:00
|
|
|
console.log('Add Invoice Response: ');
|
|
|
|
console.log(body);
|
|
|
|
if(undefined === body || body.error) {
|
|
|
|
res.status(500).json({
|
|
|
|
message: "Add Invoice Failed!",
|
|
|
|
error: (undefined === body) ? 'Error From Server!' : body.error
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.status(201).json(body);
|
|
|
|
}
|
2019-01-01 11:26:51 -05:00
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
return res.status(500).json({
|
|
|
|
message: "Add Invoice Failed!",
|
|
|
|
error: err.error
|
|
|
|
});
|
2018-11-22 20:36:28 -05:00
|
|
|
});
|
|
|
|
};
|