-
-
Notifications
You must be signed in to change notification settings - Fork 260
/
privacy.ts
106 lines (87 loc) · 2.75 KB
/
privacy.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
/* eslint-disable camelcase */
import crypto from 'crypto';
import Axios from 'axios';
import config from 'config';
import Debug from 'debug';
import { Request } from 'express';
import { find, pick } from 'lodash';
import { Card, PagingParams, PrivacyResponse, Transaction } from '../types/privacy';
const debug = Debug('privacy');
const axios = Axios.create({
baseURL: config.privacy.apiUrl,
});
const sanitizeError = e => {
e.config = pick(e.config, ['url', 'method', 'params', 'data', 'baseUrl']);
return e;
};
const rethrowSanitizedError = e => {
throw sanitizeError(e);
};
export const listTransactions = async (
token: string,
card_token?: string,
paging?: PagingParams,
status: 'all' | 'approvals' | 'declines' = 'all',
): Promise<PrivacyResponse<Transaction[]>> => {
const url = `/v1/transaction/${status}`;
debug(`GET ${url}: ${card_token}`);
const response = await axios
.get(url, {
headers: { Authorization: `api-key ${token}` },
params: { card_token, ...paging },
})
.catch(rethrowSanitizedError);
return response.data;
};
export const listCards = async (token: string, card_token?: string, paging?: PagingParams): Promise<Card[]> => {
const url = `/v1/card`;
debug(`GET ${url}`);
const response = await axios
.get(url, {
headers: { Authorization: `api-key ${token}` },
params: { ...paging, card_token },
})
.catch(rethrowSanitizedError);
return response.data?.data;
};
export const findCard = async (token: string, cardProperties: Partial<Card>): Promise<Card> => {
debug(`Searching for card ${JSON.stringify(cardProperties)}`);
let page = 1;
let keepGoing = true;
while (keepGoing) {
const cards = await listCards(token, undefined, { page, page_size: 500 });
debug(`got page ${page} with ${cards.length} cards...`);
if (cards.length === 0) {
return undefined;
}
const card = find(cards, cardProperties);
if (card) {
keepGoing = false;
return card;
}
page++;
}
};
export const updateCard = async (
token: string,
cardProperties: Partial<Card> | { card_token: string },
): Promise<Card> => {
const url = `/v1/card`;
debug(`PUT ${url}`);
const response = await axios
.put(url, cardProperties, {
headers: { Authorization: `api-key ${token}` },
})
.catch(rethrowSanitizedError);
return response.data;
};
export const verifyEvent = (req: Request & { body: Transaction; rawBody: string }, key: string): Transaction => {
const signature = req.headers['X-Lithic-HMAC'] as string;
const hmac = crypto.createHmac('sha256', key);
hmac.update(req.rawBody);
const verified = signature === hmac.digest('base64');
if (!verified) {
throw new Error('Could not verify event signature');
}
return req.body;
};