1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Get Company 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 | includeTotalCount: string | undefined, |
15 | offset: string | undefined, |
16 | limit: string | undefined, |
17 | testMode?: string |
18 | ) { |
19 | const url = new URL( |
20 | `https://dc1prodgwext.paylocity.com/compliance/v1/companies/${companyId}/clientOnboarding/partner/billing` |
21 | ) |
22 | for (const [k, v] of [ |
23 | ['includeTotalCount', includeTotalCount], |
24 | ['offset', offset], |
25 | ['limit', limit] |
26 | ]) { |
27 | if (v !== undefined && v !== '' && k !== undefined) { |
28 | url.searchParams.append(k, v) |
29 | } |
30 | } |
31 | const response = await fetch(url, { |
32 | method: 'GET', |
33 | headers: { |
34 | ...(testMode ? { testMode: testMode } : {}), |
35 | Authorization: |
36 | 'Bearer ' + |
37 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
38 | }, |
39 | body: undefined |
40 | }) |
41 | if (!response.ok) { |
42 | const text = await response.text() |
43 | throw new Error(`${response.status} ${text}`) |
44 | } |
45 | return await response.json() |
46 | } |
47 |
|
48 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
49 | const params = new URLSearchParams({ |
50 | grant_type: 'client_credentials', |
51 | client_id: auth.clientId, |
52 | client_secret: auth.clientSecret |
53 | }) |
54 |
|
55 | const response = await fetch(tokenUrl, { |
56 | method: 'POST', |
57 | headers: { |
58 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
59 | 'Content-Type': 'application/x-www-form-urlencoded' |
60 | }, |
61 | body: params.toString() |
62 | }) |
63 |
|
64 | if (!response.ok) { |
65 | const text = await response.text() |
66 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
67 | } |
68 |
|
69 | const data = await response.json() |
70 | return data.access_token |
71 | } |
72 |
|