0

Worker Pay Components

by
Published Oct 17, 2025

Batch update pay components associated to the "Active" worker.

Script paychex Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
/**
3
 * Worker Pay Components
4
 * Batch update pay components associated to the "Active" worker.
5
 */
6
export async function main(auth: RT.Paychex, workerId: string, body: Body) {
7
	const accessToken = await getOAuthToken(auth, 'https://api.paychex.com/auth/oauth/v2/token')
8
	const url = new URL(`https://api.paychex.com/workers/${workerId}/paycomponents`)
9

10
	const response = await fetch(url, {
11
		method: 'PATCH',
12
		headers: {
13
			'Content-Type': 'application/json',
14
			Authorization: 'Bearer ' + accessToken
15
		},
16
		body: JSON.stringify(body)
17
	})
18
	if (!response.ok) {
19
		const text = await response.text()
20
		throw new Error(`${response.status} ${text}`)
21
	}
22
	return await response.json()
23
}
24

25
async function getOAuthToken(auth: RT.Paychex, tokenUrl: string): Promise<string> {
26
	const params = new URLSearchParams({
27
		grant_type: 'client_credentials'
28
	})
29

30
	const response = await fetch(tokenUrl, {
31
		method: 'POST',
32
		headers: {
33
			Authorization: 'Basic ' + btoa(`${auth.client_id}:${auth.client_secret}`),
34
			'Content-Type': 'application/x-www-form-urlencoded'
35
		},
36
		body: params.toString()
37
	})
38

39
	if (!response.ok) {
40
		const text = await response.text()
41
		throw new Error(`OAuth token request failed: ${response.status} ${text}`)
42
	}
43

44
	const data = await response.json()
45
	return data.access_token
46
}
47

48
/* eslint-disable */
49
/**
50
 * This file was automatically generated by json-schema-to-typescript.
51
 * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
52
 * and run json-schema-to-typescript to regenerate this file.
53
 */
54

55
/**
56
 * Worker recurring deduction
57
 */
58
export interface Body {
59
	/**
60
	 * The id of a single pay component that a workers has.
61
	 */
62
	workerComponentId?: string
63
	/**
64
	 * The unique identifier of the pay component. This data field cannot be PATCHED.
65
	 */
66
	componentId?: string
67
	/**
68
	 * Name of the pay component. This data field will be populated automatically based on componentId.
69
	 */
70
	name?: string
71
	/**
72
	 * The type of calculation that will be applied for the pay component .
73
	 */
74
	calculationType?:
75
		| 'FLAT_DOLLAR_AMOUNT:This is used for a specific dollar amount'
76
		| 'PERCENTAGE:This is used for when a Percentage will be used to calculate the amount and will need a calculationBaseId to specify what to apply against'
77
		| 'RATE_X_UNITS:This is used for apply the rate against units and will need a calculationBaseId to specify what to apply against'
78
		| 'RATE_X_HOURS:This is used for apply the rate against hours and will need a calculationBaseId to specify what to apply against.'
79
		| 'SCHEDULED_AMOUNT:This is used for POD PayComponents.'
80
	/**
81
	 * This is required if you are not using a FLAT_DOLLAR_AMOUNT Calculation Type.
82
	 */
83
	calculationBaseId?: string
84
	/**
85
	 * This is used to specify the value that is used against the calculationType.
86
	 */
87
	value?: number
88
	/**
89
	 * Date which this pay component will start to be applied during the payruns. This is an optional field that default to current datetime if not provided. This cannot be backdated but can be added to start in the future.
90
	 */
91
	startDate?: string
92
	/**
93
	 * Date which this pay component has started for the worker.
94
	 */
95
	effectiveDate?: string
96
	/**
97
	 * Date which this pay component has ended for the worker.
98
	 */
99
	endDate?: string
100
	/**
101
	 * What the effect on pay will be (REDUCTION OR ADDITION), currently only reductions are available. This data field will be populated automatically based on componentId. This data field cannot be PATCHED
102
	 */
103
	effectOnPay?: string
104
	/**
105
	 * The category that this component falls into.
106
	 */
107
	classificationType?: string
108
	checkLimit?: {
109
		/**
110
		 * This is used to determine what type of limitations to apply.
111
		 */
112
		type?:
113
			| 'NONE:This is the default when no check limits exist'
114
			| 'STOPS_AT:This is used to stop when a specific amount is reached'
115
			| 'STOPS_AT_NEW_AMOUNT:This is the latest amount that is used to stop when a specific amount is reached'
116
			| 'BASED_ON_YTD_CASH_WAGES:This is used to stop when a specific amount is reached within the year against cash wages'
117
			| 'AMOUNT_PER_CHECK:This is used to stop when a specific amount is reached on a check'
118
		/**
119
		 * This is the the amount that the calculation will begin with.
120
		 */
121
		startsAt?: string
122
		/**
123
		 * This is the upper limit of the amount that can be applied.
124
		 */
125
		stopsAt?: string
126
		/**
127
		 * This is the amount that will be taken from each check
128
		 */
129
		amount?: string
130
		[k: string]: unknown
131
	}
132
	/**
133
	 * Worker pay frequency
134
	 */
135
	frequency?: {
136
		/**
137
		 * Currently we only support a BY_PAY_PERIOD value for the API.
138
		 */
139
		applied?: string
140
		/**
141
		 * This is how often the pay component will be applied on the pay run. The available values for this will depend on the payFrequency that a worker is paid which can be found on the worker compensations pay standards.
142
		 */
143
		occurrence?: string
144
		/**
145
		 * These are sub intervals that are used for occurrences that are larger in duration that allow you to define when to apply the pay component. You can reference the payFrequency and occurance to determine what intervals and values applicable.
146
		 */
147
		occurrenceIntervals?: {
148
			/**
149
			 * First interval
150
			 */
151
			interval1?: string
152
			/**
153
			 * Second interval
154
			 */
155
			interval2?: string
156
			[k: string]: unknown
157
		}
158
		/**
159
		 * Which check(s) within the payrun that the paycomponent will be applied to.FIRST_CHECK: The Pay Component will be applied to the first check for the worker within the pay run. This is used for a pay component that only should be applied once like a health insurance deduction.EVERY_CHECK: The Pay Component will be applied to all of the checks for the worker within the pay run. This is used for a pay component that is applied on all check like a retirement deduction.
160
		 */
161
		effectedChecks?: string
162
		[k: string]: unknown
163
	}
164
	workerPayCompScheduledEntries?: {
165
		id?: number
166
		workerCalcMethodId?: number
167
		clientPayrollId?: number
168
		checkDate?: string
169
		scheduledAmount?: number
170
		processedAmount?: number
171
		sequenceId?: number
172
		/**
173
		 * Setting Worker Pay Component Scheduled Entry Transactions Data.
174
		 */
175
		transactionDetails?: {
176
			scheduledEntryId?: number
177
			earliestTransactionDate?: string
178
			latestTransactionDate?: string
179
			transactions?: {
180
				transactionId?: string
181
				date?: string
182
				amount?: number
183
				[k: string]: unknown
184
			}[]
185
			[k: string]: unknown
186
		}
187
		[k: string]: unknown
188
	}[]
189
	/**
190
	 * Pay Component transaction details.
191
	 */
192
	transactions?: {
193
		transactionId?: string
194
		date?: string
195
		amount?: number
196
		[k: string]: unknown
197
	}[]
198
	links?: {
199
		rel?: string
200
		href?: string
201
		hreflang?: string
202
		media?: string
203
		title?: string
204
		type?: string
205
		deprecation?: string
206
		profile?: string
207
		name?: string
208
		[k: string]: unknown
209
	}[]
210
	[k: string]: unknown
211
}
212