-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgateway.js
212 lines (186 loc) · 5.68 KB
/
gateway.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
'use strict';
var debug = require('debug')('payzippy');
var crypto = require('crypto');
var request = require('request');
var assets = require('./assets');
var constants = assets.constants;
var messages = assets.messages;
var config = require('./config');
///////////////////// Private Functions /////////////////////////
function paramsToString(params) {
var data = '';
var tempKeys = Object.keys(params).sort();
tempKeys.forEach(function (key) {
if (typeof params[key] !=='undefined' && key !== 'hash'){
data += (params[key] + '|');
}
});
return data;
}
function generateChecksumHash(params, key) {
var data = paramsToString(params) + key;
debug(data);
var hash = crypto.createHash(params.hash_method).update(data).digest('hex');
params.hash = hash;
return params;
}
function validateData(data, type, cb) {
var key, param;
if (!data){
return new Error("No params provided");
}
for(key in constants[type].params) {
param = constants[type].params[key];
if (param.mandatory && !data[key]) {
return cb(new Error("Missing mandatory Parameter: " + key));
}
if (data[key] && param.type && typeof data[key] !== param.type){
return cb(new Error("Parameter " + key + " must be a " + param.type));
}
if (data[key] && param.maxLength && data[key].maxLength > param.maxLength){
return cb(new Error("Parameter " + key + " length must be less than " + param.maxLength));
}
if (data[key] && param.values && param.values.indexOf(data[key]) === -1) {
return cb(new Error ("Parameter " + key + " value must be one of " + param.values));
}
if (data[key] && param.lessThan && param.lessThan < data[key]) {
return cb(new Error ("Parameter " + key + " value must be less than " + param.lessThan));
}
if (data[key] && param.moreThan && param.moreThan > data[key]) {
return cb(new Error ("Parameter " + key + " value must be more than " + param.moreThan));
}
}
return cb(null);
}
function getRequestParams(data, type) {
var param, params = {};
Object.keys(constants[type].params).forEach(function(p){
param = config[p] || data[p];
if (param) {
params[p] = param;
}
});
return generateChecksumHash(params, config.secret_key);
}
function verifychecksum(params, key) {
if (!params) {
debug('Params Missing for checksum verification');
return false;
}
var data = paramsToString(params) + key;
debug(data);
if (params.hash && params.hash_method) {
var hash = crypto.createHash(params.hash_method).update(data).digest('hex');
debug(hash, params.hash);
if (hash === params.hash) {
return true;
} else {
debug('Checksum is wrong');
return false;
}
} else {
debug('Checksum not found');
return false;
}
}
///////////////////// Private Functions /////////////////////////
///////////////////// Public Functions //////////////////////////
var gateway = {
name: 'payzippy',
enabled: true,
assets: assets,
configure: function(configuration) {
if (!configuration || !Object.keys(configuration).length) {
throw new Error('Missing Configuration error');
}
Object.keys(configuration).forEach(function(c){
config[c] = configuration[c];
});
for(var key in config) {
if (config[key] === '') {
throw new Error('Configuration Error: Missing mandatory Parameters: ' + key);
}
}
return this;
},
charge: function (data, cb) {
validateData(data, 'request', function(err){
if (err) {
return cb(err);
}
var url;
var params = getRequestParams(data, 'request');
url = config.endpoints.charging + '?';
for (var key in params) {
url += key + '=' + encodeURIComponent(params[key]) + '&';
}
url = url.substr(0,url.length-1);
return cb({
url: url
});
});
},
response: function (req, cb) {
if (!req.query || !req.body) {
return cb(new Error("req is not a HTTP request Object"));
}
var err;
var res = Object.keys(req.query).length? req.query : req.body;
debug('Received Payment Response:', res);
if (!verifychecksum(res, config.secret_key)) {
debug('Checksum Failed');
err = new Error('Error: checksum failed');
err.response = res;
return cb(err);
}
return cb({
response: res
});
},
refund: function (data, cb) {
validateData(data, 'refund', function(err){
if (err) {
return cb(err);
}
var params = getRequestParams(data, 'refund');
var options = {
url: config.endpoints.refund,
qs: params,
timeout: config.timeout,
strictSSL: config.strictSSL
};
debug('[Refund] Making call to pg', options);
request.post(options, function (err, resp, body) {
debug('[Refund] PG resposne:', err || body);
cb(err, body);
});
});
},
status: function (data, cb) {
validateData(data, 'status', function(err){
if (err) {
return cb(err);
}
var params = getRequestParams(data, 'status');
var options = {
url: config.endpoints.status,
qs: params,
timeout: config.timeout,
strictSSL: config.strictSSL
};
debug('[Status] Making call to pg', options);
request.post(options, function (err, resp, body) {
debug('[Status] PG resposne:', err || body);
cb(err, body);
});
});
},
};
module.exports = gateway;
///////////////////// Public Functions /////////////////////////
///////////////////// TEST CODE ////////////////////////////////
(function () {
if (require.main === module) {
}
}());
///////////////////// TEST CODE ////////////////////////////////