-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpayment.js
232 lines (194 loc) · 6.6 KB
/
payment.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
var crypto = require("crypto"),
events = require('events'),
_ = require('underscore')._,
express = require('express'),
converter = require('./referenceConverter');
var config = require('./config.json');
var parameters = require('./parameters');
var basePath = "/epayments",
cancelPath = basePath + "/cancel",
okPath = basePath + "/ok",
rejectPath = basePath + "/reject";
var buttonTemplate = _.template(
'<form id="<%= id %>-payment-form" method="POST" action="<%= paymentUrl %>" class="payment-button">'+
'<div id="<%= id %>-payment" style="cursor: pointer">' +
' <div class="bank-payment-image"><img src="<%= imgPath %>" alt="<%= name %>"></div>' +
' <div class="bank-payment-name"><a href="#"><%= name %></a></div>' +
'</div>' +
'<% _.each(_.pairs(bankParams), function (pair) { %><input name="<%= pair[0] %>" value="<%= pair[1] %>" type="hidden"><% }) %>'+
'<script>' +
'var bankPayment = document.getElementById("<%= id %>-payment");' +
'var clickHandler = function() {' +
'document.getElementById("<%= id %>-payment-form").submit();' +
'};' +
'if(bankPayment.addEventListener) {' +
'bankPayment.addEventListener("click", clickHandler, false);' +
'}'+
'else if (bankPayment.attachEvent) {' +
'bankPayment.attachEvent("onclick", clickHandler);' +
'}' +
'</script>' +
'</form>'
);
exports.create = function (globalOptions, bankOptions) {
initLogger(globalOptions);
parameters.requireParams(globalOptions, ['appHandler', 'hostUrl']);
var paymentGen = Object.create(events.EventEmitter.prototype);
var hostOptions = _.clone(globalOptions);
var banks = setupBanks(bankOptions, globalOptions.hostUrl);
paymentGen.buildRequestParams = function (bankId, options) {
var bank = findBank(bankId, banks);
return requestParams(bank, options);
};
paymentGen.paymentButton = function (bankId, options) {
var bank = findBank(bankId, banks);
return createButton(bank, options);
};
paymentGen.referenceNumbers = converter;
paymentGen.banks = _.pluck(banks, 'id');
bindReturnUrlsToHandler(paymentGen, hostOptions.appHandler, banks);
hostOptions.appHandler.use(express.static(__dirname + '/public'));
return paymentGen;
};
exports.referenceNumbers = converter;
function initLogger(globalOptions) {
var logger = require('./logger');
if(globalOptions.logger){
logger.setLogger(globalOptions.logger);
}
}
function requestParams (bank, options) {
var provider = bank.provider;
if (provider && bank) {
var formParams = provider.mapParams(_.extend({}, bank, options));
var nonEmptyParams = removeIf(formParams, function (k, v) {
return !v;
});
nonEmptyParams[provider.macFormName] =
macForRequest(provider, nonEmptyParams, bank);
return nonEmptyParams;
} else {
throw new Error("No provider or configuration found for id '" + bank.id + "'.");
}
}
function createButton (bank, options) {
var bankParams = requestParams(bank, options);
var params = _.extend(commonParams(bank), {
bankParams: bankParams
});
return buttonTemplate(params);
}
function setupBanks(bankOptions, hostUrl) {
var banksWithoutUrls = extendDefaults(bankOptions);
var bankIds = _.pluck(banksWithoutUrls, 'id');
var returnUrls = returnUrlConfig(hostUrl, bankIds);
return addProviders(addReturnUrls(banksWithoutUrls, returnUrls));
}
function addProviders (banks) {
return _.map(banks, function (bank) {
return _.extend({}, bank, { provider: require('./providers/'+bank.id) });
});
}
function macForRequest (provider, providerParams, bankConfig) {
var paramsForMac = provider.requestMacParams(bankConfig, providerParams);
return generateMac(paramsForMac, provider.algorithmType(bankConfig), bankConfig.macSeparator);
}
function generateMac(params, algorithmType, separator) {
var joinedParams = params.join(separator) + separator;
return crypto.createHash(algorithmType.toLowerCase()).update(joinedParams, 'binary').digest('hex').toUpperCase();
}
function removeIf (params, condition) {
var cloned = _.clone(params);
_.each(_.keys(cloned), function (key) {
if (condition(key, cloned[key])) {
delete cloned[key];
}
});
return cloned;
}
function commonParams(bank) {
return {
id : bank.id,
name : bank.name,
paymentUrl : bank.paymentUrl,
imgPath: bank.imgPath
};
}
function findBank (bankId, bankConfigs) {
return _.find(bankConfigs, function (bank) {
return bank.id == bankId;
});
}
function bindReturnUrlsToHandler (eventEmitter, handler, banks) {
bindOkUrls(handler, banks, function (req, res, bank) {
var provider = bank.provider;
var queryData = provider.renameQueryParams(req.query);
if (_.isEmpty(req.query) || checkMac(bank, req.query, queryData.mac)) {
eventEmitter.emit('success', req, res, queryData);
} else {
eventEmitter.emit('mac-check-failed', req, res, queryData);
}
});
handler.get(cancelPath, function (req, res) {
eventEmitter.emit('cancel', req, res);
});
handler.get(rejectPath, function (req, res) {
eventEmitter.emit('reject', req, res);
});
}
function bindOkUrls (handler, banks, okCallback) {
_.each(banks, function (bank) {
var callback = function (req, res) {
okCallback(req, res, bank);
};
var okPath = bank.returnUrls.ok.path;
handler.post(okPath, callback);
handler.get(okPath, callback);
});
}
function checkMac (bank, query, expected) {
var macParams = _.filter(bank.provider.returnMacParams(bank, query), function(param){
return typeof param !== "undefined";
});
var mac = generateMac(macParams, bank.provider.algorithmType(bank), bank.macSeparator);
return mac === expected;
}
function returnUrlConfig (hostUrl, bankIds) {
var okUrls = {};
_.each(bankIds, function (bankId) {
var bankPath = okPath + '/' + bankId;
okUrls[bankId] = {
url: hostUrl + bankPath,
path: bankPath
};
});
return {
ok: okUrls,
cancel: hostUrl + cancelPath,
reject: hostUrl + rejectPath
};
}
function extendDefaults (bankOpts) {
return _.map(config.banks, function (bank) {
var bankWithDefaults = _.extend({}, config.defaultBank, bank);
var vendorOpts = _.find(bankOpts, function (bankConf) {
return bankConf.id == bankWithDefaults.id;
});
if (vendorOpts) {
return _.extend({}, bankWithDefaults, vendorOpts);
} else {
return bankWithDefaults;
}
});
}
function addReturnUrls (bankConfigs, returnUrls) {
return _.map(bankConfigs, function (bank) {
return _.extend({}, bank, {
returnUrls: {
reject: returnUrls.reject,
cancel: returnUrls.cancel,
ok: returnUrls.ok[bank.id]
}
});
});
}