node-paytmpg 3.0.5 → 5.1.0

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.
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+
3
+ var crypto = require('crypto');
4
+
5
+ class PaytmChecksum {
6
+
7
+ static encrypt(input, key) {
8
+ var cipher = crypto.createCipheriv('AES-128-CBC', key, PaytmChecksum.iv);
9
+ var encrypted = cipher.update(input, 'binary', 'base64');
10
+ encrypted += cipher.final('base64');
11
+ return encrypted;
12
+ }
13
+ static decrypt(encrypted, key) {
14
+ var decipher = crypto.createDecipheriv('AES-128-CBC', key, PaytmChecksum.iv);
15
+ var decrypted = decipher.update(encrypted, 'base64', 'binary');
16
+ try {
17
+ decrypted += decipher.final('binary');
18
+ }
19
+ catch (e) {
20
+ console.log(e);
21
+ }
22
+ return decrypted;
23
+ }
24
+ static generateSignature(params, key) {
25
+ if (typeof params !== "object" && typeof params !== "string") {
26
+ var error = "string or object expected, " + (typeof params) + " given.";
27
+ return Promise.reject(error);
28
+ }
29
+ if (typeof params !== "string"){
30
+ params = PaytmChecksum.getStringByParams(params);
31
+ }
32
+ return PaytmChecksum.generateSignatureByString(params, key);
33
+ }
34
+
35
+
36
+ static verifySignature(params, key, checksum) {
37
+ if (typeof params !== "object" && typeof params !== "string") {
38
+ var error = "string or object expected, " + (typeof params) + " given.";
39
+ return Promise.reject(error);
40
+ }
41
+ if(params.hasOwnProperty("CHECKSUMHASH")){
42
+ delete params.CHECKSUMHASH
43
+ }
44
+ if (typeof params !== "string"){
45
+ params = PaytmChecksum.getStringByParams(params);
46
+ }
47
+ return PaytmChecksum.verifySignatureByString(params, key, checksum);
48
+ }
49
+
50
+ static async generateSignatureByString(params, key) {
51
+ var salt = await PaytmChecksum.generateRandomString(4);
52
+ return PaytmChecksum.calculateChecksum(params, key, salt);
53
+ }
54
+
55
+ static verifySignatureByString(params, key, checksum) {
56
+ var paytm_hash = PaytmChecksum.decrypt(checksum, key);
57
+ var salt = paytm_hash.substr(paytm_hash.length - 4);
58
+ return (paytm_hash === PaytmChecksum.calculateHash(params, salt));
59
+ }
60
+
61
+ static generateRandomString(length) {
62
+ return new Promise(function (resolve, reject) {
63
+ crypto.randomBytes((length * 3.0) / 4.0, function (err, buf) {
64
+ if (!err) {
65
+ var salt = buf.toString("base64");
66
+ resolve(salt);
67
+ }
68
+ else {
69
+ console.log("error occurred in generateRandomString: " + err);
70
+ reject(err);
71
+ }
72
+ });
73
+ });
74
+ }
75
+
76
+ static getStringByParams(params) {
77
+ var data = {};
78
+ Object.keys(params).sort().forEach(function(key,value) {
79
+ data[key] = (params[key] !== null && params[key].toLowerCase() !== "null") ? params[key] : "";
80
+ });
81
+ return Object.values(data).join('|');
82
+ }
83
+
84
+ static calculateHash(params, salt) {
85
+ var finalString = params + "|" + salt;
86
+ return crypto.createHash('sha256').update(finalString).digest('hex') + salt;
87
+ }
88
+ static calculateChecksum(params, key, salt) {
89
+ var hashString = PaytmChecksum.calculateHash(params, salt);
90
+ return PaytmChecksum.encrypt(hashString,key);
91
+ }
92
+ }
93
+ PaytmChecksum.iv = '@@@@&&&&####$$$$';
94
+ module.exports = PaytmChecksum;
@@ -15,14 +15,17 @@ module.exports = function (app, callbacks) {
15
15
  var module = {};
16
16
  var config = (app.get('np_config'))
17
17
 
18
+ let usingMultiDbOrm = false;
18
19
  if (config.db_url) {
19
20
  User = require('../models/np_user.model.js');
21
+ usingMultiDbOrm = false;
20
22
  } else if (app.multidborm) {
21
23
  User = require('../models/np_multidbplugin.js')('npusers',app.multidborm);
22
24
  User.db=app.multidborm;
23
25
  User.modelname='npusers'
24
26
  User.idFieldName='id'
25
27
  app.NPUser = User;
28
+ usingMultiDbOrm = true;
26
29
  }
27
30
  module.create = (userData, cb) => {
28
31
 
@@ -55,7 +58,7 @@ module.exports = function (app, callbacks) {
55
58
 
56
59
  // console.log("User New : ",userData.name);
57
60
 
58
- userData.id = makeid(IDLEN);
61
+ userData.id = "user_"+makeid(IDLEN);
59
62
  var userTask = new User(userData);
60
63
  userTask.save()
61
64
  .then(user => {
@@ -69,7 +72,7 @@ module.exports = function (app, callbacks) {
69
72
 
70
73
  }
71
74
 
72
- },User);
75
+ },usingMultiDbOrm ? User : undefined);
73
76
 
74
77
  };
75
78
  return module;