-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
112 lines (102 loc) · 3.22 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
import { createLogger, format, transports } from "winston";
import "winston-daily-rotate-file";
import { eachSeries } from "async";
import moment from "moment";
import Stripe from "stripe";
import csvWriter from "csv-write-stream";
import { readFile, writeFile } from "node:fs/promises";
import { createWriteStream } from "node:fs";
import { join } from "node:path";
const lastDateFilename = "lastdatefile.dat";
const workingDir = "CHANGE_ME";
const logger = createLogger({
level: "info",
format: format.simple(),
transports: [
new transports.DailyRotateFile({
filename: "stripe-xero.log",
dirname: join(workingDir, "logs"),
datePattern: "YYYY-MM-DD",
prepend: false,
level: "info"
}),
new transports.Console({
format: format.simple()
})
]
});
async function loadConfig() {
try {
return JSON.parse(
await readFile(join(workingDir, "config.json"), "utf8")
);
} catch (err) {
logger.error(err);
return {};
}
}
async function getLastDate() {
try {
const date = await readFile(join(workingDir, lastDateFilename), "utf8");
return moment(date).unix();
} catch (err) {
return moment("2016-10-03").unix();
}
}
function writeToFile(writer, transaction, charge) {
writer.write({
Date: moment.unix(transaction.created).format("DD/MM/YYYY"),
Amount: (transaction.amount / 100.0).toFixed(2),
Payee:
charge && charge.customer && charge.customer.description
? charge.customer.description
: "",
Description: transaction.description,
Reference: transaction.source
});
if (transaction.fee > 0) {
writer.write({
Date: moment.unix(transaction.created).format("DD/MM/YYYY"),
Amount: ((0 - transaction.fee) / 100.0).toFixed(2),
Payee: "Stripe",
Description: "Stripe processing Fee",
Reference: transaction.id
});
}
}
const config = await loadConfig();
const stripe = new Stripe(config.apiKey);
const since = await getLastDate();
const outFilename = "stripestatement_since_" + since + ".csv";
try {
const transactions = await stripe.balanceTransactions.list({
created: { gt: since },
limit: 300
});
const writer = csvWriter();
writer.pipe(createWriteStream(join(workingDir, "exports", outFilename)));
eachSeries(
transactions.data,
async function (transaction) {
let charges = null;
if (transaction.type === "charge") {
try {
charges = await stripe.charges.retrieve(
transaction.source,
{ expand: ["customer"] }
);
} catch (err) {
logger.error(err);
}
}
writeToFile(writer, transaction, charges);
},
async function () {
writer.end();
await writeFile(join(workingDir, lastDateFilename), moment().format());
logger.info("Transactions saved to " + outFilename);
}
);
} catch (err) {
logger.error(err);
}