node-paytmpg 6.4.6 → 6.4.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app/controllers/payment_controller.js +91 -0
- package/index.js +13 -9
- package/package.json +1 -1
|
@@ -919,6 +919,20 @@ module.exports = function (app, callbacks) {
|
|
|
919
919
|
|
|
920
920
|
module.createTxn = (req, res) => {
|
|
921
921
|
|
|
922
|
+
// mandayory field
|
|
923
|
+
const requiredFields = ['NAME', 'EMAIL', 'MOBILE_NO', 'TXN_AMOUNT', 'PRODUCT_NAME'];
|
|
924
|
+
const checkedFields = [];
|
|
925
|
+
let gotAllParams = true;
|
|
926
|
+
requiredFields.forEach(field => {
|
|
927
|
+
if (!req.body[field]) {
|
|
928
|
+
gotAllParams = false;
|
|
929
|
+
checkedFields.push(field);
|
|
930
|
+
}
|
|
931
|
+
})
|
|
932
|
+
if (!gotAllParams) {
|
|
933
|
+
res.status(400).send({ message: "Missing required fields", missing: checkedFields });
|
|
934
|
+
return;
|
|
935
|
+
}
|
|
922
936
|
|
|
923
937
|
useController.create({ name: req.body.NAME, email: req.body.EMAIL, phone: req.body.MOBILE_NO },
|
|
924
938
|
async function (user) {
|
|
@@ -1050,12 +1064,85 @@ module.exports = function (app, callbacks) {
|
|
|
1050
1064
|
|
|
1051
1065
|
};
|
|
1052
1066
|
|
|
1067
|
+
// optional user
|
|
1068
|
+
module.getTransactions = async (req, res) => {
|
|
1069
|
+
// parameters can be from query or body
|
|
1070
|
+
// MID, MOBILE_NO, PRODUCT_NAME, EMAIL, NAME, limit, offset
|
|
1071
|
+
const params = { ...(req.query || {}), ...(req.body || {}) };
|
|
1072
|
+
|
|
1073
|
+
// Basic authz guard if caller supplies MID and it mismatches current config
|
|
1074
|
+
if (params.MID && config.MID && params.MID !== config.MID) {
|
|
1075
|
+
return res.status(403).send({ message: 'MID mismatch' });
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
// Build query map from incoming fields to db columns
|
|
1079
|
+
const query = {};
|
|
1080
|
+
const fieldMap = {
|
|
1081
|
+
MOBILE_NO: 'phone',
|
|
1082
|
+
PRODUCT_NAME: 'pname',
|
|
1083
|
+
EMAIL: 'email',
|
|
1084
|
+
NAME: 'name',
|
|
1085
|
+
ORDER_ID: 'orderId',
|
|
1086
|
+
ORDERID: 'orderId',
|
|
1087
|
+
STATUS: 'status'
|
|
1088
|
+
};
|
|
1089
|
+
|
|
1090
|
+
Object.keys(fieldMap).forEach((key) => {
|
|
1091
|
+
if (params[key]) {
|
|
1092
|
+
query[fieldMap[key]] = params[key];
|
|
1093
|
+
}
|
|
1094
|
+
});
|
|
1095
|
+
|
|
1096
|
+
// Pagination
|
|
1097
|
+
const limit = Math.min(parseInt(params.limit, 10) || 20, 100);
|
|
1098
|
+
const offset = Math.max(parseInt(params.offset, 10) || 0, 0);
|
|
1099
|
+
|
|
1100
|
+
try {
|
|
1101
|
+
let transactions = [];
|
|
1102
|
+
|
|
1103
|
+
if (usingMultiDbOrm) {
|
|
1104
|
+
const all = await Transaction.db.get(Transaction.modelname, query, {
|
|
1105
|
+
sort: [{ field: 'time', order: 'DESC' }]
|
|
1106
|
+
});
|
|
1107
|
+
const safeAll = Array.isArray(all) ? all : [];
|
|
1108
|
+
transactions = safeAll.slice(offset, offset + limit);
|
|
1109
|
+
}
|
|
1110
|
+
else {
|
|
1111
|
+
transactions = await Transaction.find(query)
|
|
1112
|
+
.sort({ time: -1 })
|
|
1113
|
+
.skip(offset)
|
|
1114
|
+
.limit(limit)
|
|
1115
|
+
.lean();
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1118
|
+
return res.send({
|
|
1119
|
+
limit,
|
|
1120
|
+
offset,
|
|
1121
|
+
count: transactions.length,
|
|
1122
|
+
transactions
|
|
1123
|
+
});
|
|
1124
|
+
}
|
|
1125
|
+
catch (err) {
|
|
1126
|
+
console.log('getTransactions error', err);
|
|
1127
|
+
return res.status(500).send({ message: 'Failed to fetch transactions', error: err.message });
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1053
1131
|
|
|
1054
1132
|
module.status = (req, res) => {
|
|
1055
1133
|
|
|
1134
|
+
if (!req.body.ORDERID && req.query.ORDERID) {
|
|
1135
|
+
req.body.ORDERID = req.query.ORDERID
|
|
1136
|
+
}
|
|
1056
1137
|
if (!req.body.ORDER_ID && req.query.ORDER_ID) {
|
|
1057
1138
|
req.body.ORDER_ID = req.query.ORDER_ID
|
|
1058
1139
|
}
|
|
1140
|
+
if(!req.body.ORDER_ID && req.body.ORDERID){
|
|
1141
|
+
req.body.ORDER_ID = req.body.ORDERID
|
|
1142
|
+
}
|
|
1143
|
+
if(!req.body.ORDER_ID){
|
|
1144
|
+
return res.status(400).send({ message: "Missing ORDER_ID" })
|
|
1145
|
+
}
|
|
1059
1146
|
var myquery = { orderId: req.body.ORDER_ID };
|
|
1060
1147
|
Transaction.findOne(myquery, async function (err, orderData) {
|
|
1061
1148
|
|
|
@@ -1064,6 +1151,10 @@ module.exports = function (app, callbacks) {
|
|
|
1064
1151
|
res.send(err)
|
|
1065
1152
|
return
|
|
1066
1153
|
}
|
|
1154
|
+
else if (!orderData) {
|
|
1155
|
+
res.send({ message: "Order Not Found or not initiated yet!", ORDER_ID: req.body.ORDER_ID })
|
|
1156
|
+
return
|
|
1157
|
+
}
|
|
1067
1158
|
if (orderData.status === "INITIATED") {
|
|
1068
1159
|
|
|
1069
1160
|
var params = {}
|
package/index.js
CHANGED
|
@@ -14,9 +14,12 @@ const buildConfig = require('./lib/config/buildConfig');
|
|
|
14
14
|
* @param {object} db - optional multi-db-orm instance; if omitted and db_url is provided, MongoDB is used
|
|
15
15
|
* @returns {import('express').Application} configured sub-application ready to mount
|
|
16
16
|
*/
|
|
17
|
-
function createPaymentMiddleware(userConfig = {}, db) {
|
|
17
|
+
function createPaymentMiddleware(userConfig = {}, db, authenticationMiddleware) {
|
|
18
18
|
const config = buildConfig(userConfig);
|
|
19
19
|
const subApp = express();
|
|
20
|
+
if(!authenticationMiddleware) {
|
|
21
|
+
authenticationMiddleware = (req, res, next) => next();
|
|
22
|
+
}
|
|
20
23
|
|
|
21
24
|
// expose config + optional db handle for downstream controllers
|
|
22
25
|
subApp.set('np_config', config);
|
|
@@ -68,15 +71,16 @@ function createPaymentMiddleware(userConfig = {}, db) {
|
|
|
68
71
|
console.log('Received request at', req.originalUrl);
|
|
69
72
|
next();
|
|
70
73
|
});
|
|
71
|
-
subApp.all('/init', pc.init);
|
|
72
|
-
subApp.all('/callback', pc.callback);
|
|
73
|
-
subApp.all('/api/webhook', pc.webhook);
|
|
74
|
-
subApp.all('/api/status', pc.status);
|
|
75
|
-
subApp.all('/api/
|
|
76
|
-
subApp.all('/api/createTxn', pc.
|
|
77
|
-
subApp.all('/', pc.
|
|
74
|
+
subApp.all('/init',authenticationMiddleware, pc.init);
|
|
75
|
+
subApp.all('/callback',authenticationMiddleware, pc.callback);
|
|
76
|
+
subApp.all('/api/webhook',authenticationMiddleware, pc.webhook);
|
|
77
|
+
subApp.all('/api/status',authenticationMiddleware, pc.status);
|
|
78
|
+
subApp.all('/api/transactions',authenticationMiddleware, pc.getTransactions);
|
|
79
|
+
subApp.all('/api/createTxn/token',authenticationMiddleware, pc.createTxnToken);
|
|
80
|
+
subApp.all('/api/createTxn',authenticationMiddleware, pc.createTxn);
|
|
81
|
+
subApp.all('/', authenticationMiddleware, pc.init);
|
|
78
82
|
|
|
79
|
-
subApp.use(express.static(path.join(__dirname, 'public')), pc.init);
|
|
83
|
+
subApp.use(express.static(path.join(__dirname, 'public')), authenticationMiddleware, pc.init);
|
|
80
84
|
|
|
81
85
|
return subApp;
|
|
82
86
|
}
|