-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (76 loc) · 3.09 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
require('dotenv').config();
const fetch = require('node-fetch');
const crypto = require('crypto');
const fs = require('fs');
const {
TRANSFERWISE_API_TOKEN,
TRANSFERWISE_PROFILE_ID,
TRANSFERWISE_BORDERLESS_ACCOUNT_ID,
FREEAGENT_CLIENT_ID,
FREEAGENT_CLIENT_SECRET,
FREEAGENT_REFRESH_TOKEN,
FREEAGENT_BANK_ACCOUNT_ID,
} = process.env;
const dateString = process.argv[2];
if (!dateString || !/^20\d{6}$/.test(dateString)) {
console.error("Need a start date e.g. 20190101");
process.exit(1);
}
go(`${dateString.substr(0, 4)}-${dateString.substr(4, 2)}-${dateString.substr(6, 2)}T00:00:00.000Z`)
.catch(error => {
console.error(error);
});
async function go(start) {
let signature;
let ott;
let res1;
for (let i = 0; i <= 1; i++) {
const headers = {
"Authorization": `Bearer ${TRANSFERWISE_API_TOKEN}`,
};
if (signature && ott) {
headers["X-Signature"] = signature;
headers["X-2FA-Approval"] = ott;
}
res1 = await fetch(`https://api.transferwise.com/v3/profiles/${TRANSFERWISE_PROFILE_ID}/borderless-accounts/${TRANSFERWISE_BORDERLESS_ACCOUNT_ID}/statement.json?currency=USD&intervalStart=${start}&intervalEnd=${new Date().toISOString()}`, {
headers,
});
if (res1.status === 403 && res1.headers.get("X-2FA-Approval-Result") === "REJECTED") {
const sign = crypto.createSign("RSA-SHA256");
sign.update(res1.headers.get("X-2FA-Approval"));
signature = sign.sign(fs.readFileSync("private.pem")).toString("base64");
ott = res1.headers.get("X-2FA-Approval");
}
}
if (!res1.ok) {
console.log("Failed getting statement from TransferWise");
return;
}
const txns1 = await res1.json();
txns = txns1.transactions.map(txn => ({
dated_on: txn.date.substr(0, 10),
description: txn.details.type === "DEPOSIT" ? `${txn.details.paymentReference} from ${txn.details.senderName}` : txn.details.type === "TRANSFER" ? `To ${txn.details.recipient.name} ref ${txn.details.paymentReference}` : `${txn.details.description}`,
amount: txn.amount.value,
}));
const accessToken = (await (await fetch(`https://api.freeagent.com/v2/token_endpoint`, {
headers: {
"Authorization": `Basic ${Buffer.from(`${FREEAGENT_CLIENT_ID}:${FREEAGENT_CLIENT_SECRET}`).toString("base64")}`,
"Content-Type": "application/x-www-form-urlencoded",
},
method: "POST",
body: `grant_type=refresh_token&refresh_token=${encodeURIComponent(FREEAGENT_REFRESH_TOKEN)}`,
})).json()).access_token;
const res2 = await fetch(`https://api.freeagent.com/v2/bank_transactions/statement?bank_account=${FREEAGENT_BANK_ACCOUNT_ID}`, {
headers: {
"Authorization": `Bearer ${accessToken}`,
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({ statement: txns }),
});
if (res2.ok) {
console.log("Yep");
} else {
console.log("Nope - ", await res2.json());
}
}