-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
59 lines (53 loc) · 1.54 KB
/
server.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
'use strict';
const config = require('./config')
const form = require('./lib/validate');
const payment = require('./lib/payment');
const db = require('./lib/db');
const bodyParser = require('body-parser');
const express = require('express');
const app = express();
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({extended:true}))
//validate request
app.use('/checkout', form.checkValid);
app.post('/checkout', (req, res) => {
let opts = {
ccName: req.body.cc_name,
ccNumber: req.body.cc_number,
expirationMonth: req.body.cc_expiry_month,
expirationYear: req.body.cc_expiry_year,
cvv: req.body.cc_cvv,
currency: req.body.currency,
amount: req.body.price
}
try{
//configure payment
var pay = payment(opts);
}catch(e){
return res.status(401).send(e);
}
//process payment
pay.send((err, result)=>{
let paymentStatus = err ? 'Failed' : 'Success';
let data = {
price: req.body.price,
currency: req.body.currency,
fullName: req.body.full_name,
status: paymentStatus,
response: err || result
};
//save data + response
db.save(data, (error, info)=>{
res.send('Payment ' + paymentStatus + '. <a href="/status/' + info.insertedId + '"> View details </a>');
})
})
})
//view transaction detail
app.get('/status/:id', (req, res)=>{
db.findById(req.params.id, (err, data)=>{
res.send(data);
})
})
app.listen(config.port, () => {
console.log('Server started at port ', config.port)
})