1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Add Billing Codes |
8 | * > 🚧 Partner Restricted |
9 | > All background check API endpoints are restricted to background check providers that have signed a Paylocity technology partnership agreement. |
10 | */ |
11 | export async function main( |
12 | auth: Paylocity, |
13 | companyId: string, |
14 | body: { billingCodes?: { billingCode?: string; isActive?: false | true }[] }, |
15 | testMode?: string |
16 | ) { |
17 | const url = new URL( |
18 | `https://dc1prodgwext.paylocity.com/compliance/v1/companies/${companyId}/clientOnboarding/billing` |
19 | ) |
20 |
|
21 | const response = await fetch(url, { |
22 | method: 'POST', |
23 | headers: { |
24 | ...(testMode ? { testMode: testMode } : {}), |
25 | 'Content-Type': 'application/json', |
26 | Authorization: |
27 | 'Bearer ' + |
28 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
29 | }, |
30 | body: JSON.stringify(body) |
31 | }) |
32 | if (!response.ok) { |
33 | const text = await response.text() |
34 | throw new Error(`${response.status} ${text}`) |
35 | } |
36 | return await response.text() |
37 | } |
38 |
|
39 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
40 | const params = new URLSearchParams({ |
41 | grant_type: 'client_credentials', |
42 | client_id: auth.clientId, |
43 | client_secret: auth.clientSecret |
44 | }) |
45 |
|
46 | const response = await fetch(tokenUrl, { |
47 | method: 'POST', |
48 | headers: { |
49 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
50 | 'Content-Type': 'application/x-www-form-urlencoded' |
51 | }, |
52 | body: params.toString() |
53 | }) |
54 |
|
55 | if (!response.ok) { |
56 | const text = await response.text() |
57 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
58 | } |
59 |
|
60 | const data = await response.json() |
61 | return data.access_token |
62 | } |
63 |
|