2019-01-01 16:26:51 +00:00
|
|
|
var request = require('request-promise');
|
2019-09-01 17:01:38 +00:00
|
|
|
var common = require('../../common');
|
|
|
|
var logger = require('../logger');
|
2019-03-02 17:55:19 +00:00
|
|
|
var options = {};
|
2018-10-10 00:08:05 +00:00
|
|
|
|
|
|
|
exports.decodePayment = (req, res, next) => {
|
2019-04-07 00:20:40 +00:00
|
|
|
options = common.getOptions();
|
2019-09-02 04:11:37 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/payreq/' + req.params.payRequest;
|
2019-01-01 16:26:51 +00:00
|
|
|
request(options).then((body) => {
|
2020-01-29 23:36:28 +00:00
|
|
|
const body_str = (!body) ? '' : JSON.stringify(body);
|
|
|
|
const search_idx = (!body) ? -1 : body_str.search('Not Found');
|
2020-01-02 23:23:47 +00:00
|
|
|
logger.info({fileName: 'PayReq', msg: 'Payment Decode Received: ' + body_str});
|
2020-01-29 23:36:28 +00:00
|
|
|
if(!body || search_idx > -1 || body.error) {
|
2018-10-10 00:08:05 +00:00
|
|
|
res.status(500).json({
|
|
|
|
message: "Payment Request Decode Failed!",
|
2020-01-29 23:36:28 +00:00
|
|
|
error: (!body || search_idx > -1) ? 'Error From Server!' : body.error
|
2018-10-10 00:08:05 +00:00
|
|
|
});
|
|
|
|
} else {
|
2020-01-29 23:36:28 +00:00
|
|
|
body.btc_num_satoshis = (!body.num_satoshis) ? 0 : common.convertToBTC(body.num_satoshis);
|
|
|
|
body.timestamp_str = (!body.timestamp) ? '' : common.convertTimestampToDate(body.timestamp);
|
2018-10-10 00:08:05 +00:00
|
|
|
res.status(200).json(body);
|
|
|
|
}
|
2019-01-01 16:26:51 +00:00
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
return res.status(500).json({
|
|
|
|
message: "Payment Request Decode Failed!",
|
|
|
|
error: err.error
|
|
|
|
});
|
2018-10-10 00:08:05 +00:00
|
|
|
});
|
|
|
|
};
|