1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * GET Recurring Deduction Detail |
8 | * **Summary Description** |
9 |
|
10 | This function will allow the API user to get the details for a specific deduction code on an employee record. |
11 | */ |
12 | export async function main( |
13 | auth: Paylocity, |
14 | companyId: string, |
15 | employeeId: string, |
16 | deductionCode: string, |
17 | testMode?: string |
18 | ) { |
19 | const url = new URL( |
20 | `https://dc1prodgwext.paylocity.com/apiHub/payroll/v1/companies/${companyId}/employees/${employeeId}/deductions/${deductionCode}` |
21 | ) |
22 |
|
23 | const response = await fetch(url, { |
24 | method: 'GET', |
25 | headers: { |
26 | ...(testMode ? { testMode: testMode } : {}), |
27 | Authorization: |
28 | 'Bearer ' + |
29 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
30 | }, |
31 | body: undefined |
32 | }) |
33 | if (!response.ok) { |
34 | const text = await response.text() |
35 | throw new Error(`${response.status} ${text}`) |
36 | } |
37 | return await response.json() |
38 | } |
39 |
|
40 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
41 | const params = new URLSearchParams({ |
42 | grant_type: 'client_credentials', |
43 | client_id: auth.clientId, |
44 | client_secret: auth.clientSecret |
45 | }) |
46 |
|
47 | const response = await fetch(tokenUrl, { |
48 | method: 'POST', |
49 | headers: { |
50 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
51 | 'Content-Type': 'application/x-www-form-urlencoded' |
52 | }, |
53 | body: params.toString() |
54 | }) |
55 |
|
56 | if (!response.ok) { |
57 | const text = await response.text() |
58 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
59 | } |
60 |
|
61 | const data = await response.json() |
62 | return data.access_token |
63 | } |
64 |
|