-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.js
103 lines (82 loc) · 2.75 KB
/
app.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
'use strict';
require('dotenv').config();
const express = require('express');
const request = require('request');
const app = express();
const path = require('path');
const bodyParser = require('body-parser');
const uuidv1 = require('uuid/v1');
const ngrok = (process.env.NGROK_ENABLED==="true") ? require('ngrok'):null;
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(path.join(__dirname, '/public')));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(bodyParser.json())
var urlencodedParser = bodyParser.urlencoded({ extended: false });
var chargeID='';
app.use(express.static('views'));
app.get('/', function(req, res) {
// Render home page with params
res.render('index');
});
app.post('/authorizePurchase', urlencodedParser, function(req,res) {
var postBody = {
url: process.env.CHARGE_URL,
headers: {
'Content-Type': 'application/json',
'Request-Id' : uuidv1(),
'Authorization' : 'Bearer ' + process.env.ACCESS_TOKEN
},
body: req.body.json,
json: true
};
request.post(postBody, function (err, response) {
chargeID = response.body.id;
res.send(response.body);
});
});
app.post('/captureCharge', urlencodedParser, function(req,res) {
var postBody = {
url: process.env.CHARGE_URL +'/'+ chargeID +'/capture',
headers: {
'Content-Type': 'application/json',
'Request-Id' : uuidv1(),
'Authorization' : 'Bearer ' + process.env.ACCESS_TOKEN
},
body: req.body.json,
json: true
};
request.post(postBody, function (err, response, data) {
res.send(response.body);
});
});
// Start server on HTTP
const server = app.listen(process.env.PORT || 8001, () => {
console.log(`💻 Server listening on port ${server.address().port}`);
if(!ngrok){
console.log(`💳 See the Sample App in your browser: ` + 'http://localhost:' + `${server.address().port}`);
}
});
// Turn on the ngrok tunnel in development, which provides both the mandatory HTTPS support for all card payments
if (ngrok) {
console.log("NGROK Enabled");
ngrok.connect(
{
addr: process.env.PORT || 8000,
subdomain: '',
authtoken: '',
},
(err, url) => {
if (err) {
if (err.code === 'ECONNREFUSED') {
console.log(`Connection refused at ${err.address}:${err.port}`);
} else {
console.log(`⚠️ ${err}`);
}
process.exit(1);
} else {
console.log(`💳 See the Sample App in your browser: ${url}`);
}
}
);
}