1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Get Tax Codes |
8 | * **Summary Description** |
9 |
|
10 | The GET Tax Codes by list by Company ID endpoint allows users to retrieve a comprehensive list of Tax Codes details from a client's Paylocity instance. |
11 | */ |
12 | export async function main( |
13 | auth: Paylocity, |
14 | companyId: string, |
15 | includeTotalCount: string | undefined, |
16 | offset: string | undefined, |
17 | limit: string | undefined, |
18 | testMode?: string |
19 | ) { |
20 | const url = new URL( |
21 | `https://dc1prodgwext.paylocity.com/apiHub/payroll/v1/companies/${companyId}/taxes` |
22 | ) |
23 | for (const [k, v] of [ |
24 | ['includeTotalCount', includeTotalCount], |
25 | ['offset', offset], |
26 | ['limit', limit] |
27 | ]) { |
28 | if (v !== undefined && v !== '' && k !== undefined) { |
29 | url.searchParams.append(k, v) |
30 | } |
31 | } |
32 | const response = await fetch(url, { |
33 | method: 'GET', |
34 | headers: { |
35 | ...(testMode ? { testMode: testMode } : {}), |
36 | Authorization: |
37 | 'Bearer ' + |
38 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
39 | }, |
40 | body: undefined |
41 | }) |
42 | if (!response.ok) { |
43 | const text = await response.text() |
44 | throw new Error(`${response.status} ${text}`) |
45 | } |
46 | return await response.json() |
47 | } |
48 |
|
49 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
50 | const params = new URLSearchParams({ |
51 | grant_type: 'client_credentials', |
52 | client_id: auth.clientId, |
53 | client_secret: auth.clientSecret |
54 | }) |
55 |
|
56 | const response = await fetch(tokenUrl, { |
57 | method: 'POST', |
58 | headers: { |
59 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
60 | 'Content-Type': 'application/x-www-form-urlencoded' |
61 | }, |
62 | body: params.toString() |
63 | }) |
64 |
|
65 | if (!response.ok) { |
66 | const text = await response.text() |
67 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
68 | } |
69 |
|
70 | const data = await response.json() |
71 | return data.access_token |
72 | } |
73 |
|