1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Add/update non-primary state tax |
8 | * Sends new or updated employee non-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 | reciprocityCode?: string |
25 | specialCheckCalc?: string |
26 | taxCalculationCode?: string |
27 | taxCode?: string |
28 | w4FormYear?: number |
29 | } |
30 | ) { |
31 | const url = new URL( |
32 | `https://dc1prodgwext.paylocity.com/api/v2/companies/${companyId}/employees/${employeeId}/nonprimaryStateTax` |
33 | ) |
34 |
|
35 | const response = await fetch(url, { |
36 | method: 'PUT', |
37 | headers: { |
38 | 'Content-Type': 'application/json', |
39 | Authorization: |
40 | 'Bearer ' + |
41 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
42 | }, |
43 | body: JSON.stringify(body) |
44 | }) |
45 | if (!response.ok) { |
46 | const text = await response.text() |
47 | throw new Error(`${response.status} ${text}`) |
48 | } |
49 | return await response.text() |
50 | } |
51 |
|
52 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
53 | const params = new URLSearchParams({ |
54 | grant_type: 'client_credentials', |
55 | client_id: auth.clientId, |
56 | client_secret: auth.clientSecret |
57 | }) |
58 |
|
59 | const response = await fetch(tokenUrl, { |
60 | method: 'POST', |
61 | headers: { |
62 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
63 | 'Content-Type': 'application/x-www-form-urlencoded' |
64 | }, |
65 | body: params.toString() |
66 | }) |
67 |
|
68 | if (!response.ok) { |
69 | const text = await response.text() |
70 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
71 | } |
72 |
|
73 | const data = await response.json() |
74 | return data.access_token |
75 | } |
76 |
|