|
1
|
|
|
require('dotenv').config(); |
|
2
|
|
|
var https = require('https'); |
|
3
|
|
|
var express = require('express'); |
|
4
|
|
|
var session = require('express-session'); |
|
5
|
|
|
var request = require('request'); |
|
6
|
|
|
var app = express(); |
|
7
|
|
|
var config = require('./config.json'); |
|
8
|
|
|
var path = require('path'); |
|
9
|
|
|
var crypto = require('crypto'); |
|
10
|
|
|
var QuickBooks = require('node-quickbooks'); |
|
11
|
|
|
var queryString = require('query-string'); |
|
12
|
|
|
var fs = require('fs'); |
|
13
|
|
|
var json2csv = require('json2csv'); |
|
14
|
|
|
var Tokens = require('csrf'); |
|
15
|
|
|
var csrf = new Tokens(); |
|
16
|
|
|
var atob = require('atob'); |
|
17
|
|
|
|
|
18
|
|
|
// Configure View and Handlebars |
|
19
|
|
|
app.use(express.static(path.join(__dirname, ''))) |
|
20
|
|
|
app.set('views', path.join(__dirname, 'views')) |
|
21
|
|
|
var exphbs = require('express-handlebars'); |
|
22
|
|
|
var hbs = exphbs.create({}); |
|
23
|
|
|
app.engine('handlebars', hbs.engine); |
|
24
|
|
|
app.set('view engine', 'handlebars'); |
|
25
|
|
|
app.use(session({secret: 'secret', resave: 'false', saveUninitialized: 'false'})) |
|
26
|
|
|
|
|
27
|
|
|
/* |
|
28
|
|
|
Create body parsers for application/json and application/x-www-form-urlencoded |
|
29
|
|
|
*/ |
|
30
|
|
|
var bodyParser = require('body-parser') |
|
31
|
|
|
app.use(bodyParser.json()) |
|
32
|
|
|
var urlencodedParser = bodyParser.urlencoded({ extended: false }) |
|
33
|
|
|
|
|
34
|
|
|
/* |
|
35
|
|
|
App Variables |
|
36
|
|
|
*/ |
|
37
|
|
|
var oauth2_token_json=null, |
|
38
|
|
|
realmId = '', |
|
39
|
|
|
accessToken = '', |
|
40
|
|
|
payload = ''; |
|
41
|
|
|
scope=''; |
|
|
|
|
|
|
42
|
|
|
var fields = ['realmId', 'name', 'id', 'operation', 'lastUpdated']; |
|
43
|
|
|
var newLine= "\r\n"; |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
app.use(express.static('views')); |
|
47
|
|
|
|
|
48
|
|
|
app.get('/', function(req, res) { |
|
49
|
|
|
|
|
50
|
|
|
// Render home page with params |
|
51
|
|
|
res.render('index', { |
|
52
|
|
|
redirect_uri: config.redirectUri, |
|
53
|
|
|
oauth2_token_json: oauth2_token_json |
|
54
|
|
|
}); |
|
55
|
|
|
}); |
|
56
|
|
|
|
|
57
|
|
|
app.get('/authUri', function(req,res) { |
|
58
|
|
|
|
|
59
|
|
|
// Generate csrf Anti Forgery |
|
60
|
|
|
req.session.secret = csrf.secretSync(); |
|
61
|
|
|
var state = csrf.create(req.session.secret); |
|
62
|
|
|
|
|
63
|
|
|
// Generate the AuthUrl |
|
64
|
|
|
var redirecturl = config.authorization_endpoint + '?' + queryString.stringify({ |
|
65
|
|
|
'client_id': config.clientId, |
|
66
|
|
|
'redirect_uri': config.redirectUri, //Make sure this path matches entry in application dashboard |
|
67
|
|
|
'scope': config.scopes.connect_to_quickbooks[0]+' '+config.scopes.connect_to_quickbooks[1]+' '+config.scopes.sign_in_with_intuit[0]+' '+config.scopes.sign_in_with_intuit[1]+' '+config.scopes.sign_in_with_intuit[2]+' '+config.scopes.sign_in_with_intuit[3]+' '+config.scopes.sign_in_with_intuit[4], |
|
68
|
|
|
'response_type': 'code', |
|
69
|
|
|
'state': state |
|
70
|
|
|
}); |
|
71
|
|
|
res.send(redirecturl); |
|
72
|
|
|
}); |
|
73
|
|
|
|
|
74
|
|
|
app.get('/callback', function(req, res) { |
|
75
|
|
|
|
|
76
|
|
|
var parsedUri = queryString.parse(req.originalUrl); |
|
77
|
|
|
realmId = parsedUri.realmId; |
|
78
|
|
|
|
|
79
|
|
|
var auth = (new Buffer(config.clientId + ':' + config.clientSecret).toString('base64')); |
|
|
|
|
|
|
80
|
|
|
var postBody = { |
|
81
|
|
|
url: config.token_endpoint, |
|
82
|
|
|
headers: { |
|
83
|
|
|
Accept: 'application/json', |
|
84
|
|
|
'Content-Type': 'application/x-www-form-urlencoded', |
|
85
|
|
|
Authorization: 'Basic ' + auth, |
|
86
|
|
|
}, |
|
87
|
|
|
form: { |
|
88
|
|
|
grant_type: 'authorization_code', |
|
89
|
|
|
code: req.query.code, |
|
90
|
|
|
redirect_uri: config.redirectUri |
|
91
|
|
|
} |
|
92
|
|
|
}; |
|
93
|
|
|
|
|
94
|
|
|
request.post(postBody, function (err, res, data) { |
|
|
|
|
|
|
95
|
|
|
accessToken = JSON.parse(res.body); |
|
96
|
|
|
oauth2_token_json = JSON.stringify(accessToken, null,2); |
|
97
|
|
|
console.log('The access tokeb is :'+oauth2_token_json); |
|
|
|
|
|
|
98
|
|
|
}); |
|
99
|
|
|
res.send(''); |
|
100
|
|
|
}); |
|
101
|
|
|
|
|
102
|
|
|
app.get('/refreshAccessToken', function(req,res){ |
|
103
|
|
|
|
|
104
|
|
|
// save the access token somewhere on behalf of the logged in user |
|
105
|
|
|
var qbo = new QuickBooks(config.clientId, |
|
106
|
|
|
config.clientSecret, |
|
107
|
|
|
accessToken.access_token, /* oAuth access token */ |
|
108
|
|
|
false, /* no token secret for oAuth 2.0 */ |
|
109
|
|
|
realmId, |
|
110
|
|
|
config.useSandbox, /* use a sandbox account */ |
|
111
|
|
|
true, /* turn debugging on */ |
|
112
|
|
|
4, /* minor version */ |
|
113
|
|
|
'2.0', /* oauth version */ |
|
114
|
|
|
accessToken.refresh_token /* refresh token */); |
|
115
|
|
|
|
|
116
|
|
|
qbo.refreshAccessToken(function(err, refreshToken) { |
|
117
|
|
|
if (err) { |
|
118
|
|
|
console.log(err); |
|
|
|
|
|
|
119
|
|
|
res.send(err); |
|
120
|
|
|
} |
|
121
|
|
|
else { |
|
122
|
|
|
console.log("The response refresh is :" + JSON.stringify(refreshToken,null,2)); |
|
123
|
|
|
res.send(refreshToken); |
|
124
|
|
|
} |
|
125
|
|
|
}); |
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
}); |
|
129
|
|
|
|
|
130
|
|
|
app.get('/getCompanyInfo', function(req,res){ |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
// save the access token somewhere on behalf of the logged in user |
|
134
|
|
|
var qbo = new QuickBooks(config.clientId, |
|
135
|
|
|
config.clientSecret, |
|
136
|
|
|
accessToken.access_token, /* oAuth access token */ |
|
137
|
|
|
false, /* no token secret for oAuth 2.0 */ |
|
138
|
|
|
realmId, |
|
139
|
|
|
config.useSandbox, /* use a sandbox account */ |
|
140
|
|
|
true, /* turn debugging on */ |
|
141
|
|
|
4, /* minor version */ |
|
142
|
|
|
'2.0', /* oauth version */ |
|
143
|
|
|
accessToken.refresh_token /* refresh token */); |
|
144
|
|
|
|
|
145
|
|
|
qbo.getCompanyInfo(realmId, function(err, companyInfo) { |
|
146
|
|
|
if (err) { |
|
147
|
|
|
console.log(err); |
|
|
|
|
|
|
148
|
|
|
res.send(err); |
|
149
|
|
|
} |
|
150
|
|
|
else { |
|
151
|
|
|
console.log("The response is :" + JSON.stringify(companyInfo,null,2)); |
|
152
|
|
|
res.send(companyInfo); |
|
153
|
|
|
} |
|
154
|
|
|
}); |
|
155
|
|
|
}); |
|
156
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
// Start server on HTTP (will use ngrok for HTTPS forwarding) |
|
159
|
|
|
app.listen(3000, function () { |
|
160
|
|
|
console.log('Example app listening on port 3000!') |
|
|
|
|
|
|
161
|
|
|
}); |