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