-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
154 lines (127 loc) · 4.36 KB
/
index.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
'use strict';
var data1 = require('./lib/plaidSandbox2.json');
var purchase = require('./purchase.js');
var fetchAlts = require('./fetchAlts.js');
var envvar = require('envvar');
var express = require('express');
var bodyParser = require('body-parser');
var moment = require('moment');
var plaid = require('plaid');
var transactions;
var APP_PORT = envvar.number('APP_PORT', 8000);
var debug = true;
var PLAID_CLIENT_ID = envvar.string('PLAID_CLIENT_ID', debug ? '5a21a2ac4e95b836d37e3672' : '5616c9f51abbf9e13f581fb2' );
var PLAID_SECRET = envvar.string('PLAID_SECRET', debug ? 'c8dbac9121245f93510d139d270519' : '488760dbd0560fbb10d5845bf03a24');
var PLAID_PUBLIC_KEY = envvar.string('PLAID_PUBLIC_KEY', debug ? 'b8ef71cf0f8b7bdfe6d8da3761a04a' : '505704614c705498646afea6bffe29');
var PLAID_ENV = envvar.string('PLAID_ENV', debug ? 'sandbox' : 'production' );
// We store the access_token in memory - in production, store it in a secure
// persistent data store
var ACCESS_TOKEN = null;
var PUBLIC_TOKEN = null;
var ITEM_ID = null;
var client = new plaid.Client(
PLAID_CLIENT_ID,
PLAID_SECRET,
PLAID_PUBLIC_KEY,
plaid.environments[PLAID_ENV]
);
var app = express();
app.use(express.static('public'));
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
app.get('/', function(request, response, next) {
response.render('index.ejs', {
PLAID_PUBLIC_KEY: PLAID_PUBLIC_KEY,
PLAID_ENV: PLAID_ENV,
});
});
app.post('/get_access_token', function(request, response, next) {
console.log(request.body);
PUBLIC_TOKEN = request.body.public_token;
console.log(PUBLIC_TOKEN);
client.exchangePublicToken(PUBLIC_TOKEN, function(error, tokenResponse) {
if (error != null) {
var msg = 'Could not exchange public_token!';
console.log(msg + '\n' + JSON.stringify(error));
return response.json({
error: msg
});
}
ACCESS_TOKEN = tokenResponse.access_token;
ITEM_ID = tokenResponse.item_id;
console.log('Access Token: ' + ACCESS_TOKEN);
console.log('Item ID: ' + ITEM_ID);
response.json({
'error': false
});
});
});
app.get('/accounts', function(request, response, next) {
client.getAuth(ACCESS_TOKEN, function(error, authResponse) {
if (error != null) {
var msg = 'Unable to pull accounts from the Plaid API.';
console.log(msg + '\n' + error);
return response.json({
error: msg
});
}
console.log(authResponse.accounts);
response.json({
error: false,
accounts: authResponse.accounts,
numbers: authResponse.numbers,
});
});
});
app.post('/item', function(request, response, next) {
client.getItem(ACCESS_TOKEN, function(error, itemResponse) {
if (error != null) {
console.log(JSON.stringify(error));
return response.json({
error: error
});
}
client.getInstitutionById(itemResponse.item.institution_id, function(err, instRes) {
if (err != null) {
var msg = 'Unable to pull institution information from the Plaid API.';
console.log(msg + '\n' + error);
return response.json({
error: msg
});
} else {
response.json({
item: itemResponse.item,
institution: instRes.institution,
});
}
});
});
});
app.get('/transactions', function(request, response, next) {
//start and end dates for transaction
var startDate = moment().subtract(160, 'days').format('YYYY-MM-DD');
var endDate = moment().format('YYYY-MM-DD');
client.getTransactions(ACCESS_TOKEN, startDate, endDate, {
count: 250,
offset: 0,
}, function(error, transactionsResponse) {
if (error != null) {
console.log(JSON.stringify(error));
return response.json({
error: error
});
}
transactions = debug ? transactionsResponse.transactions.concat(data1.transactions) : transactionsResponse.transactions;
transactions = transactions.map(entry => purchase.purchase(entry.product_name==null ? entry.name : entry.product_name, entry.amount, entry.date, entry.category_id));
console.dir(transactions, {depth: null});
transactions = fetchAlts(transactions).then((output) => {
response.json(output.filter(n => !!n));
});
});
});
var server = app.listen(APP_PORT, function() {
console.log('plaid-walkthrough server listening on port ' + APP_PORT);
});