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