1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Add / Update Deduction |
8 | * Add/Update Deduction API sends new or updated employee deduction information directly to Paylocity Payroll/HR solution. |
9 | */ |
10 | export async function main( |
11 | auth: Paylocity, |
12 | body: { |
13 | deduction?: { |
14 | agency?: string |
15 | annualMaximum?: number |
16 | calcCode?: string |
17 | caseNo?: string |
18 | companyNumber?: string |
19 | costCenter1?: string |
20 | costCenter2?: string |
21 | costCenter3?: string |
22 | dcode?: string |
23 | effectiveDate?: string |
24 | employeeId?: string |
25 | endDate?: string |
26 | fipsCode?: string |
27 | frequency?: string |
28 | goal?: number |
29 | isSelfInsuredPlan?: false | true |
30 | loanFirstPaymentDate401K?: string |
31 | loanIssueDate401K?: string |
32 | loanNumber?: string |
33 | maximum?: number |
34 | medicalSupport?: false | true |
35 | minimum?: number |
36 | miscInfo?: string |
37 | paidTowardsGoal?: number |
38 | priority?: number |
39 | rate?: number |
40 | reportTerminated?: false | true |
41 | ssn?: string |
42 | startDate?: string |
43 | stateAbbrev?: string |
44 | } |
45 | } |
46 | ) { |
47 | const url = new URL(`https://dc1prodgwext.paylocity.com/api/v1/deduction`) |
48 |
|
49 | const response = await fetch(url, { |
50 | method: 'POST', |
51 | headers: { |
52 | 'Content-Type': 'application/json', |
53 | Authorization: |
54 | 'Bearer ' + |
55 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
56 | }, |
57 | body: JSON.stringify(body) |
58 | }) |
59 | if (!response.ok) { |
60 | const text = await response.text() |
61 | throw new Error(`${response.status} ${text}`) |
62 | } |
63 | return await response.text() |
64 | } |
65 |
|
66 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
67 | const params = new URLSearchParams({ |
68 | grant_type: 'client_credentials', |
69 | client_id: auth.clientId, |
70 | client_secret: auth.clientSecret |
71 | }) |
72 |
|
73 | const response = await fetch(tokenUrl, { |
74 | method: 'POST', |
75 | headers: { |
76 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
77 | 'Content-Type': 'application/x-www-form-urlencoded' |
78 | }, |
79 | body: params.toString() |
80 | }) |
81 |
|
82 | if (!response.ok) { |
83 | const text = await response.text() |
84 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
85 | } |
86 |
|
87 | const data = await response.json() |
88 | return data.access_token |
89 | } |
90 |
|