-
-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathplaidIntegration.ts
268 lines (240 loc) Β· 11.3 KB
/
plaidIntegration.ts
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
import path from 'path'
import { parseISO, format, subMonths } from 'date-fns'
import plaid, { TransactionsResponse, CreateLinkTokenOptions } from 'plaid'
import { Config, updateConfig } from '../../common/config'
import { PlaidConfig, PlaidEnvironmentType } from '../../types/integrations/plaid'
import { IntegrationId } from '../../types/integrations'
import express from 'express'
import bodyParser from 'body-parser'
import { logInfo, logError, logWarn } from '../../common/logging'
import http from 'http'
import { AccountConfig, Account, PlaidAccountConfig } from '../../types/account'
import { Transaction } from '../../types/transaction'
const PLAID_USER_ID = 'LOCAL'
export class PlaidIntegration {
config: Config
plaidConfig: PlaidConfig
environment: string
client: plaid.Client
user: plaid.User
constructor(config: Config) {
this.config = config
this.plaidConfig = this.config.integrations[IntegrationId.Plaid] as PlaidConfig
this.environment =
this.plaidConfig.environment === PlaidEnvironmentType.Development
? plaid.environments.development
: plaid.environments.sandbox
this.client = new plaid.Client({
clientID: this.plaidConfig.credentials.clientId,
secret: this.plaidConfig.credentials.secret,
env: this.environment,
options: {
version: '2019-05-29'
}
})
// In production this is supposed to be a unique identifier but for Mintable we only have one user (you)
this.user = {
client_user_id: PLAID_USER_ID
}
}
public exchangeAccessToken = (accessToken: string): Promise<string> =>
// Exchange an expired API access_token for a new Link public_token
this.client.createPublicToken(accessToken).then(token => token.public_token)
public savePublicToken = (tokenResponse: plaid.TokenResponse): void => {
updateConfig(config => {
config.accounts[tokenResponse.item_id] = {
id: tokenResponse.item_id,
integration: IntegrationId.Plaid,
token: tokenResponse.access_token
}
this.config = config
return config
})
}
public accountSetup = (): Promise<void> => {
return new Promise((resolve, reject) => {
const client = this.client
const app = express()
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: true }))
.use(express.static(path.resolve(path.join(__dirname, '../../../docs'))))
let server: http.Server
app.post('/get_access_token', (req, res) => {
if (req.body.public_token !== undefined) {
client.exchangePublicToken(req.body.public_token, (error, tokenResponse) => {
if (error != null) {
reject(logError('Encountered error exchanging Plaid public token.', error))
}
this.savePublicToken(tokenResponse)
resolve(logInfo('Plaid access token saved.', req.body))
})
} else if (req.body.exit !== undefined) {
resolve(logInfo('Plaid authentication exited.', req.body))
} else {
if ((req.body.error['error-code'] = 'item-no-error')) {
resolve(logInfo('Account is OK, no further action is required.', req.body))
} else {
reject(logError('Encountered error during authentication.', req.body))
}
}
return res.json({})
})
app.post('/accounts', async (req, res) => {
let accounts: { name: string; token: string }[] = []
for (const accountId in this.config.accounts) {
const accountConfig: PlaidAccountConfig = this.config.accounts[accountId] as PlaidAccountConfig
if (accountConfig.integration === IntegrationId.Plaid) {
try {
await this.client.getAccounts(accountConfig.token).then(resp => {
accounts.push({
name: resp.accounts[0].name,
token: accountConfig.token
})
})
} catch {
accounts.push({
name: 'Error fetching account name',
token: accountConfig.token
})
}
}
}
return res.json(accounts)
})
app.post('/create_link_token', async (req, res) => {
const clientUserId = this.user.client_user_id
const options: CreateLinkTokenOptions = {
user: {
client_user_id: clientUserId
},
client_name: 'Mintable',
products: ['transactions'],
country_codes: ['US'], // TODO
language: 'en' // TODO
}
if (req.body.access_token) {
options.access_token = req.body.access_token
delete options.products
}
this.client.createLinkToken(options, (err, data) => {
if (err) {
logError('Error creating Plaid link token.', err)
}
logInfo('Successfully created Plaid link token.')
res.json({ link_token: data.link_token })
})
})
app.post('/remove', async (req, res) => {
try {
await updateConfig(config => {
Object.values(config.accounts).forEach(account => {
const accountConfig: PlaidAccountConfig = account as PlaidAccountConfig
if (accountConfig.hasOwnProperty('token') && accountConfig.token == req.body.token) {
delete config.accounts[accountConfig.id]
}
})
this.config = config
return config
})
logInfo('Successfully removed Plaid account.', req.body.token)
return res.json({})
} catch (error) {
logError('Error removing Plaid account.', error)
}
})
app.post('/done', (req, res) => {
res.json({})
return server.close()
})
app.get('/', (req, res) =>
res.sendFile(path.resolve(path.join(__dirname, '../../../src/integrations/plaid/account-setup.html')))
)
server = require('http')
.createServer(app)
.listen('8000')
})
}
public fetchPagedTransactions = async (
accountConfig: AccountConfig,
startDate: Date,
endDate: Date
): Promise<TransactionsResponse> => {
return new Promise(async (resolve, reject) => {
accountConfig = accountConfig as PlaidAccountConfig
try {
const dateFormat = 'yyyy-MM-dd'
const start = format(startDate, dateFormat)
const end = format(endDate, dateFormat)
let options: plaid.TransactionsRequestOptions = { count: 500, offset: 0 }
let accounts = await this.client.getTransactions(accountConfig.token, start, end, options)
while (accounts.transactions.length < accounts.total_transactions) {
options.offset += options.count
const next_page = await this.client.getTransactions(accountConfig.token, start, end, options)
accounts.transactions = accounts.transactions.concat(next_page.transactions)
}
return resolve(accounts)
} catch (e) {
return reject(e)
}
})
}
public fetchAccount = async (accountConfig: AccountConfig, startDate: Date, endDate: Date): Promise<Account[]> => {
if (startDate < subMonths(new Date(), 5)) {
logWarn('Transaction history older than 6 months may not be available for some institutions.', {})
}
return this.fetchPagedTransactions(accountConfig, startDate, endDate)
.then(data => {
let accounts: Account[] = data.accounts.map(account => ({
integration: IntegrationId.Plaid,
accountId: account.account_id,
mask: account.mask,
institution: account.name,
account: account.official_name,
type: account.subtype || account.type,
current: account.balances.current,
available: account.balances.available,
limit: account.balances.limit,
currency: account.balances.iso_currency_code || account.balances.unofficial_currency_code
}))
const transactions: Transaction[] = data.transactions.map(transaction => ({
integration: IntegrationId.Plaid,
name: transaction.name,
date: parseISO(transaction.date),
amount: transaction.amount,
currency: transaction.iso_currency_code || transaction.unofficial_currency_code,
type: transaction.transaction_type,
accountId: transaction.account_id,
transactionId: transaction.transaction_id,
pendingtransactionId: transaction.pending_transaction_id,
category: transaction.category.join(' - '),
address: transaction.location.address,
city: transaction.location.city,
state: transaction.location.region,
postal_code: transaction.location.postal_code,
country: transaction.location.country,
latitude: transaction.location.lat,
longitude: transaction.location.lon,
pending: transaction.pending
}))
accounts = accounts.map(account => ({
...account,
transactions: transactions
.filter(transaction => transaction.accountId === account.accountId)
.map(transaction => ({
...transaction,
institution: account.institution,
account: account.account
}))
}))
logInfo(
`Fetched ${data.accounts.length} sub-accounts and ${data.total_transactions} transactions.`,
accounts
)
return accounts
})
.catch(error => {
logError(`Error fetching account ${accountConfig.id}.`, error)
return []
})
}
}