0

Company Pay Periods

by
Published Oct 17, 2025

Array of pay periods associated with the company.

Script paychex Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
/**
3
 * Company Pay Periods
4
 * Array of pay periods associated with the company.
5
 */
6
export async function main(
7
	auth: RT.Paychex,
8
	companyId: string,
9
	status?: string | undefined,
10
	from?: string | undefined,
11
	to?: string | undefined
12
) {
13
	const accessToken = await getOAuthToken(auth, 'https://api.paychex.com/auth/oauth/v2/token')
14
	const url = new URL(`https://api.paychex.com/companies/${companyId}/payperiods`)
15
	for (const [k, v] of [
16
		['status', status],
17
		['from', from],
18
		['to', to]
19
	]) {
20
		if (v !== undefined && v !== '') {
21
			url.searchParams.append(k, v)
22
		}
23
	}
24
	const response = await fetch(url, {
25
		method: 'GET',
26
		headers: {
27
			Authorization: 'Bearer ' + accessToken
28
		},
29
		body: undefined
30
	})
31
	if (!response.ok) {
32
		const text = await response.text()
33
		throw new Error(`${response.status} ${text}`)
34
	}
35
	return await response.json()
36
}
37

38
async function getOAuthToken(auth: RT.Paychex, tokenUrl: string): Promise<string> {
39
	const params = new URLSearchParams({
40
		grant_type: 'client_credentials'
41
	})
42

43
	const response = await fetch(tokenUrl, {
44
		method: 'POST',
45
		headers: {
46
			Authorization: 'Basic ' + btoa(`${auth.client_id}:${auth.client_secret}`),
47
			'Content-Type': 'application/x-www-form-urlencoded'
48
		},
49
		body: params.toString()
50
	})
51

52
	if (!response.ok) {
53
		const text = await response.text()
54
		throw new Error(`OAuth token request failed: ${response.status} ${text}`)
55
	}
56

57
	const data = await response.json()
58
	return data.access_token
59
}
60