1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Add/update primary state tax |
8 | * Sends new or updated employee primary state tax information directly to Paylocity Payroll/HR solution. |
9 | */ |
10 | export async function main( |
11 | auth: Paylocity, |
12 | companyId: string, |
13 | employeeId: string, |
14 | body: { |
15 | amount?: number |
16 | deductionsAmount?: number |
17 | dependentsAmount?: number |
18 | exemptions?: number |
19 | exemptions2?: number |
20 | filingStatus?: string |
21 | higherRate?: false | true |
22 | otherIncomeAmount?: number |
23 | percentage?: number |
24 | specialCheckCalc?: string |
25 | taxCalculationCode?: string |
26 | taxCode?: string |
27 | w4FormYear?: number |
28 | } |
29 | ) { |
30 | const url = new URL( |
31 | `https://dc1prodgwext.paylocity.com/api/v2/companies/${companyId}/employees/${employeeId}/primaryStateTax` |
32 | ) |
33 |
|
34 | const response = await fetch(url, { |
35 | method: 'PUT', |
36 | headers: { |
37 | 'Content-Type': 'application/json', |
38 | Authorization: |
39 | 'Bearer ' + |
40 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
41 | }, |
42 | body: JSON.stringify(body) |
43 | }) |
44 | if (!response.ok) { |
45 | const text = await response.text() |
46 | throw new Error(`${response.status} ${text}`) |
47 | } |
48 | return await response.text() |
49 | } |
50 |
|
51 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
52 | const params = new URLSearchParams({ |
53 | grant_type: 'client_credentials', |
54 | client_id: auth.clientId, |
55 | client_secret: auth.clientSecret |
56 | }) |
57 |
|
58 | const response = await fetch(tokenUrl, { |
59 | method: 'POST', |
60 | headers: { |
61 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
62 | 'Content-Type': 'application/x-www-form-urlencoded' |
63 | }, |
64 | body: params.toString() |
65 | }) |
66 |
|
67 | if (!response.ok) { |
68 | const text = await response.text() |
69 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
70 | } |
71 |
|
72 | const data = await response.json() |
73 | return data.access_token |
74 | } |
75 |
|