1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Get all local taxes |
8 | * Returns all local taxes for the selected employee. |
9 | */ |
10 | export async function main(auth: Paylocity, companyId: string, employeeId: string) { |
11 | const url = new URL( |
12 | `https://dc1prodgwext.paylocity.com/api/v2/companies/${companyId}/employees/${employeeId}/localTaxes` |
13 | ) |
14 |
|
15 | const response = await fetch(url, { |
16 | method: 'GET', |
17 | headers: { |
18 | Authorization: |
19 | 'Bearer ' + |
20 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
21 | }, |
22 | body: undefined |
23 | }) |
24 | if (!response.ok) { |
25 | const text = await response.text() |
26 | throw new Error(`${response.status} ${text}`) |
27 | } |
28 | return await response.json() |
29 | } |
30 |
|
31 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
32 | const params = new URLSearchParams({ |
33 | grant_type: 'client_credentials', |
34 | client_id: auth.clientId, |
35 | client_secret: auth.clientSecret |
36 | }) |
37 |
|
38 | const response = await fetch(tokenUrl, { |
39 | method: 'POST', |
40 | headers: { |
41 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
42 | 'Content-Type': 'application/x-www-form-urlencoded' |
43 | }, |
44 | body: params.toString() |
45 | }) |
46 |
|
47 | if (!response.ok) { |
48 | const text = await response.text() |
49 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
50 | } |
51 |
|
52 | const data = await response.json() |
53 | return data.access_token |
54 | } |
55 |
|