-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
396 lines (337 loc) · 14.6 KB
/
main.js
File metadata and controls
396 lines (337 loc) · 14.6 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
//please use main.js only for app.x so that it is cleaner
/**
* Split the modules into parts, for node sided JS please put in
* nodemodjs and use module.exports to #include functions here
*
* For client sided JS please put in js folder
*
* Remember, this is an API so calls should not rely too much on html
*/
var ejs = require('ejs'); //ejs is not express, but is a extension to express
var path = require("path"); //pathing system
var bodyParser = require('body-parser'); //parse POST data
var session = require('express-session'); //temporary to store sensitive data, see if theres better way
var authenticator = require("./nodemodjs/authenticator.js");
var queue1Function = require("./nodemodjs/azureStorageHash.js");
var jeDatabase = require("./nodemodjs/databaseFunctions.js")
var hyperWallet = require("./nodemodjs/hyperwallet.js")
const cvars = require("./nodemodjs/commonvariables.js");
const express = require('express'); //express is good
const app = express();
const customer = require('./nodemodjs/customer.js');
//const http = require('http'); //http stuff, not needed yet
//const fs = require('fs'); //filesystem, not needed yet
const port = 5000;
/**
* uses ejs engine to eval HTML files (to use <%= %> variables)
*/
app.engine('html', require('ejs').renderFile);
app.use(session({
secret: 'whatsecretshallweuse kitten',//session secret to sign sessions
resave: true, //force save
saveUninitialized: true,
/*cookie: { secure: true }*/
})); //secure needs HTTPS, cookies will not be stored if running from HTTP with this option
app.use(bodyParser.json()); // supporting POST data
app.use(bodyParser.urlencoded({ extended: true })); // supportting POST data
/**
* evals js/css/img folders for JS/CSS/image files
*/
app.use(express.static(path.join(__dirname, '/js')));
app.use(express.static(path.join(__dirname, '/css')));
app.use(express.static(path.join(__dirname, '/img')));
app.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
/**
* listens to dynamic port if online, and local testing uses 5000
*/
app.listen(process.env.PORT || port);
app.get('/', function (req, res) { //base page
res.render(path.join(__dirname + '/Home.html'));
});
app.get('/walletPresentation', function (req, res) { //base page
res.render(path.join(__dirname + '/walletPresentation.html'));
});
// app.post('/walletPresentation', function (req, res) { //base page
// if (!req.body.clientID) {
// res.send("<p>Please provide a valid client ID</p>");
// return;
// }
// if (req.body.clientID){
// var databaseFunctions = require('./nodemodjs/databaseFunctions.js')
// var promiseSearchClient = databaseFunctions.retrieveUserByID(req.body.clientId)
// promiseSearchClient.then((value)=>{
// if(value.statusCode >= 200 && value.statusCode <= 299){
// res.render(path.join(__dirname + '/walletPresentation/Wallet.html'), clientID);
// }else{
// res.render("<script>alert('"+value+"')</script>", clientID, path.join(__dirname + '/walletPresentation.html'))
// }
// })
// }
// });
// app.get('/walletPresentation/Wallet', function (req, res) { //base page
// if (!req.query.clientID) {
// res.send("<p>Please provide a valid client ID</p>");
// return;
// }
// res.render(path.join(__dirname + '/walletPresentation/Wallet.html'));
// });
/**
* API Description:
* Authentication for 6 digit pins and users (not connected to database yet)
*
* user: the clientid to authenticate
* pin: the pin to check against the clientid
* now connected to real database
*/
app.post('/authenticate', function (req, res) { //base page
if (!req.body.user || !req.body.pin || req.body.pin.length != 6) {
res.send("Please input a userid and a 6 digits pin");
return;
}
if (authenticator.checkAuthorized(req.session)) {
res.send("Already Authorized. At " + req.session.authorized);
}
else {
var promisething = authenticator.authRequest(req.session, req.body.user, req.body.pin);
promisething.then((value) => {
if (value) {
res.send("Authorized. At " + req.session.authorized);
}
else {
res.send("Invalid user and pin combination. try again!");
}
})
}
});
/**
* on start at localhost:3000/pay?amount=10.00 generate the token
* Requires Bot to send the query via POST
*
* API Description:
* This is the credit card request function
*
* amount: the amount to pay, 1 = $1.00
* customer: the token from the customer
* To use: send a request to localhost:3000/pay?amount=(amount)&customer=(token)&merchantid=(merchant)
* Example Request: /pay?amount=300.00&customer=663573599&merchantid=123
*/
app.get('/pay', function (req, res) { //change to app.post once debug finish
var sess = req.session;
//if (!authenticator.checkAuthorized(sess)) {
// res.render(path.join(__dirname + '/Home.html'));
// return;
//}//check auth later
if (!req.query.amount || req.query.amount < 0.01 || !req.query.customer || !req.query.merchantid || !req.query.branchid|| !req.query.savedaddress) { //change to req.body if POST
res.send("<p>Please provide amount, customer, merchantid, branchid and savedaddress</p>");
return;
}
var page = path.join(__dirname + '/index.html');
var randHash = authenticator.genRandomizedLink(req.query.amount, req.query.customer, req.query.merchantid, req.query.savedaddress);
res.send(randHash);
queue1Function.sendBotTransactionDetailsToTable(randHash,req.query.savedaddress,req.query.amount,req.query.merchantid,req.query.customer,req.query.branchid);
//hash is the primary key*
//var cpromise = BTDatabaseFunction.findBTtoken(req.query.customer);
//cpromise.then(function(value) {
// customer.openCustomerPay(sess, req.query.amount, value, req.query.merchantid, res, page, req.query.savedAddress); //find customer, if customer not found overwrite but this should not happen
// });
});
app.get('/payhash', function (req, res) { //TEST FUNCTION FOR HASH
var sess = req.session;
//if (!authenticator.checkAuthorized(sess)) {
// res.render(path.join(__dirname + '/Home.html'));
// return;
//}//check auth later
if (!req.query.hash) { //change to req.body if POST
res.send("<p>Please provide a valid hash</p>");
return;
}
var hash = encodeURIComponent(req.query.hash);
var page = path.join(__dirname + '/index.html');
queue1Function.searchQueue1Storage(hash, res,sess,page);
// customer.openCustomerPay(sess, amount, customertoken, merchant, res, page, savedAddress); //find customer, if customer not found overwrite but this should not happen
});
app.post('/pay', function (req, res) {
var sess = req.session;
//if (!authenticator.checkAuthorized(sess)) {
// res.render(path.join(__dirname + '/Home.html'));
// return;
//}//check auth later
if (!req.body.amount || req.body.amount < 0.01 || !req.body.customer || !req.body.merchantid || !req.body.savedaddress|| !req.body.branchid) { //change to req.body if POST
res.send("<p>Please provide amount, customer and merchantid to pay to</p>");
return;
}
var page = path.join(__dirname + '/index.html');
var randHash = authenticator.genRandomizedLink(req.body.amount + "", req.body.customer + "", req.body.merchantid + "", req.body.savedaddress + "");
res.send(randHash);
queue1Function.sendBotTransactionDetailsToTable(randHash,req.body.savedaddress,req.body.amount,req.body.merchantid,req.body.customer,req,body.branchid);
//customer.openCustomerPay(sess, req.body.amount, value, req.body.merchantid, res, page, req.body.savedAddress); //find customer, if customer not found overwrite but this should not happen
});
/**
* processpayment handler, customer.chargeCard for details
*/
app.post('/processpayment', function (req, res) {
if (!req.body.user || !req.body.amount || !req.body.nonce || !req.session.customer || !req.body.merchantid || !req.body.branchid) {
res.send("<p>Please provide transactionid, amount, nonce, customer token and merchantid</p>");
return;
}
var storageAddress = req.session.storageAddress;
console.log("storeaddress is " + storageAddress);
customer.chargeCard(req.body.amount, req.body.nonce, req.body.merchantid, res, storageAddress, req.session,req.body.user,req.body.branchid);
});
app.post('/autopayment', function (req, res) {
if (!req.body.amount || !req.body.customer || !req.body.merchantid) {
res.send("<p>Please provide amount, customer token and merchantid</p>");
return;
}
//swap customer for clientid
var storageAddress = req.session.storageAddress;
customer.autoChargeCard(req.body.amount, req.body.customer, req.body.merchantid, res, storageAddress);
});
/**
* create customer handler, customer.createCustomer for details
*/
app.get("/create/customer", function (req, res) {
if (!req.query.clientid) {
res.send("<p>Please provide clientid</p>");
return;
}
customer.createBrainTreeCustomer(res, req.query.clientid);
hyperWallet.insertNewClientWallet(req.query.clientid,req.query.clientid,0)
});
app.get("/create/merchantWallet", function (req, res) {
if (!req.query.merchantid) {
res.send("<p>Please provide merchantid</p>");
return;
}
hyperWallet.insertNewMerchantWallet(req.query.merchantid,req.query.merchantid,0)
});
app.get("/find/customer", function (req, res) {
if (!req.query.clientid) {
res.send("<p>Please provide clientid</p>");
return;
}
var cpromise = jeDatabase.retrieveBrainTreeToken(req.query.clientid);
cpromise.then(function(value) {
console.log("test" + value);
customer.retrieveCustomerCardDetails(value,res);
});
});
app.get("/testcustomer", function (req, res) {
var page = path.join(__dirname + '/index.html');
customer.openCustomerPay(req.session, 100,"12345",res,page,"123","123",3)
});
app.post("/walletAdd", function (req, res) {
if (!req.body.clientID||!req.body.amount||!req.body.nonce) {
res.send("<p>Please provide clientid, amount</p>");
return;
}
// console.log("DASDAS"+req.body.clientID)
customer.chargeByNonceClient(req.body.amount, req.body.nonce,req.body.clientID,res);
//customer.openCustomerPay(req.session, res.query.amount,-1,res,page,"savedAddress",-1,res.query.clientid)
// Adds a transaction record along the process, unless the other methods
});
// Wallet Pages // left 2FA
app.get("/walletTopUp", function (req, res) {
if (!req.query.clientid,!req.query.amount) {
res.send("<p>Please provide clientid, amount</p>");
return;
}
var page = path.join(__dirname + '/index.html');
customer.openCustomerPay(req.session, req.query.amount,-1,res,page,"savedAddress",-1,req.query.clientid)
// Adds a transaction record along the process, unless the other methods
});
app.get("/walletPay", function (req, res) {
if (!req.query.clientid,!req.query.merchantid,!req.query.branchid,!req.query.amount) {
res.send("<p>Please provide clientid, merchantid, amount</p>");
return;
}
var errorCheck = hyperWallet.processTransaction(req.query.clientid,req.query.merchantid,req.query.amount)
errorCheck.then((err)=>{
if(err='error'){
res.send("<p>Error occured during processing of transaction please try again</p>");
}
else{
jeDatabase.createTransactionWalletPayment(req.query.clientid,req.query.merchantid,req.query.branchid,req.query.amount)
}
})
});
app.post("/walletPay", function (req, res) {
if (!req.body.clientid,!req.body.merchantid,!req.body.branchid,!req.body.amount) {
res.send("<p>Please provide clientid, merchantid, amount</p>");
return;
}
var errorCheck = hyperWallet.processTransaction(req.body.clientid,req.body.merchantid,req.body.amount)
errorCheck.then((err)=>{
if(err=='error'){
res.send("<p>Error occured during processing of transaction please try again</p>");
}
else{
jeDatabase.createTransactionWalletPayment(req.body.clientid,req.body.merchantid,req.body.branchid,req.body.amount)
res.send("Your payment to Merchant " + merchantid + ": branch " + branchid + " of $" + amount + " is successful.")
}
})
});
app.post("/getwalletamount", function (req, res) {
if (!req.body.clientid) {
res.send("<p>Please provide clientid</p>");
return;
}
hyperWallet.getClientWalletByClientID(req.body.clientid).then((value) => {
res.send(JSON.stringify(value))
})
});
app.get("/walletRefund", function (req, res) {
if (!req.query.clientid,!req.query.merchantid,!req.query.branchid,!req.query.amount) {
res.send("<p>Please provide clientid merchantid, amount</p>");
return;
}
var errorCheck = hyperWallet.createTransactionRefund(req.query.clientid,req.query.merchantid,req.query.amount)
errorCheck.then((err)=>{
if(err='error'){
res.send("<p>Error occured during processing of transaction refund please try again</p>");
}
else{
jeDatabase.createTransactionWalletRefund(req.query.clientid,req.query.merchantid,req.query.branchid,req.query.amount)
}
})
});
app.post("/retrieveuserid", function (req, res) {
if (!req.body.clientid) {
res.send("<p>Please provide clientid</p>");
return;
}
jeDatabase.retrieveUserByID(req.body.clientid).then((value) => {
res.send(value)
})
});
// Houliang here
app.get("/createSettlement", function (req, res) {
if (!req.query.clientid,!req.query.amount) {
res.send("<p>Please provide clientid, amount</p>");
return;
}
customer.openCustomerPay(req.session, res.query.amount,-1,res,page,"savedAddress",-1,res.query.clientid)
// Adds a transaction record along the process, unless the other methods
});
app.post("/customerhistory", function (req, res) {
if (!req.body.clientID) {
res.send("<p>Please provide clientid, amount</p>");
return;
}
hyperWallet.retrieveTransactionByID(req.body.clientID).then((value) => {
res.send(value)
})
});
/**
* handles 404 errors here
*
* note that this has to be the last app.x function
*/
app.use(function (req, res, next) {
res.status(404).send("You may not view this page. Please use localhost:3000/pay")
});