-
Notifications
You must be signed in to change notification settings - Fork 0
/
edenredService.js
77 lines (65 loc) · 2.38 KB
/
edenredService.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
const { getAppConfigFromEnv } = require("./config");
const crypto = require('crypto');
const appConfig = getAppConfigFromEnv();
const authenticate = async () => {
u = {
userId: appConfig.EDENRED_USERNAME,
password: appConfig.EDENRED_PIN,
appType: "IOS",
appVersion: "4.1.0"
};
const token = await fetch('https://www.myedenred.pt/edenred-customer/v2/authenticate/pin?appVersion='+ appConfig.EDENRED_VERSION +'&appType=IOS&channel=MOBILE', {
method: 'POST',
body: JSON.stringify(u),
headers: {
'Content-type': 'application/json',
'User-Agent': 'EdenRED/3748 CFNetwork/1496.0.7 Darwin/23.5.0',
},
})
.then((response) => response.json())
.then((json) => json.data.token)
.catch((err) => {
console.error("error occured", err);
return '';
});
return token
}
const getAllTransactions = async (token, accountId) => {
url = 'https://www.myedenred.pt/edenred-customer/v2/protected/card/' + accountId + '/accountmovement?appVersion=1.0&appType=PORTAL&channel=WEB'
transactions = await fetch(url, {
method: 'GET',
headers: {
'Authorization': token
},
})
.then((response) => response.json())
.then((json) => json.data.movementList)
.catch((err) => {
console.error("error occured", err);
});
return transactions;
}
async function getTransactions(accountId) {
authorizationToken = await authenticate()
transactions = await getAllTransactions(authorizationToken, accountId)
parsedTransactions = []
transactions.forEach(transaction => {
transactionID = crypto.createHash('sha256').update(transaction.transactionName+transaction.transactionDate+transaction.amount).digest('hex');
date = transaction.transactionDate.split("T")[0]
if (date < '2024-06-21') {
return;
}
parsedTransactions.push({
date: date,
amount: Math.trunc(transaction.amount * 100),
payee_name: transaction.transactionName,
imported_payee: transaction.transactionName,
imported_id: transactionID,
cleared: true,
})
});
return parsedTransactions
}
module.exports = {
getTransactions
}