0

Worker Pay Component

by
Published Oct 17, 2025

Update a single pay component associated to the "Active" worker.

Script paychex Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
/**
3
 * Worker Pay Component
4
 *  Update a single pay component associated to the "Active" worker.
5
 */
6
export async function main(
7
	auth: RT.Paychex,
8
	workerId: string,
9
	workerComponentId: string,
10
	body: Body
11
) {
12
	const accessToken = await getOAuthToken(auth, 'https://api.paychex.com/auth/oauth/v2/token')
13
	const url = new URL(
14
		`https://api.paychex.com/workers/${workerId}/paycomponents/${workerComponentId}`
15
	)
16

17
	const response = await fetch(url, {
18
		method: 'PATCH',
19
		headers: {
20
			'Content-Type': 'application/json',
21
			Authorization: 'Bearer ' + accessToken
22
		},
23
		body: JSON.stringify(body)
24
	})
25
	if (!response.ok) {
26
		const text = await response.text()
27
		throw new Error(`${response.status} ${text}`)
28
	}
29
	return await response.json()
30
}
31

32
async function getOAuthToken(auth: RT.Paychex, tokenUrl: string): Promise<string> {
33
	const params = new URLSearchParams({
34
		grant_type: 'client_credentials'
35
	})
36

37
	const response = await fetch(tokenUrl, {
38
		method: 'POST',
39
		headers: {
40
			Authorization: 'Basic ' + btoa(`${auth.client_id}:${auth.client_secret}`),
41
			'Content-Type': 'application/x-www-form-urlencoded'
42
		},
43
		body: params.toString()
44
	})
45

46
	if (!response.ok) {
47
		const text = await response.text()
48
		throw new Error(`OAuth token request failed: ${response.status} ${text}`)
49
	}
50

51
	const data = await response.json()
52
	return data.access_token
53
}
54

55
/* eslint-disable */
56
/**
57
 * This file was automatically generated by json-schema-to-typescript.
58
 * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
59
 * and run json-schema-to-typescript to regenerate this file.
60
 */
61

62
/**
63
 * Worker recurring deduction
64
 */
65
export interface Body {
66
	/**
67
	 * The id of a single pay component that a workers has.
68
	 */
69
	workerComponentId?: string
70
	/**
71
	 * The unique identifier of the pay component. This data field cannot be PATCHED.
72
	 */
73
	componentId?: string
74
	/**
75
	 * Name of the pay component. This data field will be populated automatically based on componentId.
76
	 */
77
	name?: string
78
	/**
79
	 * The type of calculation that will be applied for the pay component .
80
	 */
81
	calculationType?:
82
		| 'FLAT_DOLLAR_AMOUNT:This is used for a specific dollar amount'
83
		| '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'
84
		| 'RATE_X_UNITS:This is used for apply the rate against units and will need a calculationBaseId to specify what to apply against'
85
		| 'RATE_X_HOURS:This is used for apply the rate against hours and will need a calculationBaseId to specify what to apply against.'
86
		| 'SCHEDULED_AMOUNT:This is used for POD PayComponents.'
87
	/**
88
	 * This is required if you are not using a FLAT_DOLLAR_AMOUNT Calculation Type.
89
	 */
90
	calculationBaseId?: string
91
	/**
92
	 * This is used to specify the value that is used against the calculationType.
93
	 */
94
	value?: number
95
	/**
96
	 * 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.
97
	 */
98
	startDate?: string
99
	/**
100
	 * Date which this pay component has started for the worker.
101
	 */
102
	effectiveDate?: string
103
	/**
104
	 * Date which this pay component has ended for the worker.
105
	 */
106
	endDate?: string
107
	/**
108
	 * 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
109
	 */
110
	effectOnPay?: string
111
	/**
112
	 * The category that this component falls into.
113
	 */
114
	classificationType?: string
115
	checkLimit?: {
116
		/**
117
		 * This is used to determine what type of limitations to apply.
118
		 */
119
		type?:
120
			| 'NONE:This is the default when no check limits exist'
121
			| 'STOPS_AT:This is used to stop when a specific amount is reached'
122
			| 'STOPS_AT_NEW_AMOUNT:This is the latest amount that is used to stop when a specific amount is reached'
123
			| 'BASED_ON_YTD_CASH_WAGES:This is used to stop when a specific amount is reached within the year against cash wages'
124
			| 'AMOUNT_PER_CHECK:This is used to stop when a specific amount is reached on a check'
125
		/**
126
		 * This is the the amount that the calculation will begin with.
127
		 */
128
		startsAt?: string
129
		/**
130
		 * This is the upper limit of the amount that can be applied.
131
		 */
132
		stopsAt?: string
133
		/**
134
		 * This is the amount that will be taken from each check
135
		 */
136
		amount?: string
137
		[k: string]: unknown
138
	}
139
	/**
140
	 * Worker pay frequency
141
	 */
142
	frequency?: {
143
		/**
144
		 * Currently we only support a BY_PAY_PERIOD value for the API.
145
		 */
146
		applied?: string
147
		/**
148
		 * 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.
149
		 */
150
		occurrence?: string
151
		/**
152
		 * 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.
153
		 */
154
		occurrenceIntervals?: {
155
			/**
156
			 * First interval
157
			 */
158
			interval1?: string
159
			/**
160
			 * Second interval
161
			 */
162
			interval2?: string
163
			[k: string]: unknown
164
		}
165
		/**
166
		 * 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.
167
		 */
168
		effectedChecks?: string
169
		[k: string]: unknown
170
	}
171
	workerPayCompScheduledEntries?: {
172
		id?: number
173
		workerCalcMethodId?: number
174
		clientPayrollId?: number
175
		checkDate?: string
176
		scheduledAmount?: number
177
		processedAmount?: number
178
		sequenceId?: number
179
		/**
180
		 * Setting Worker Pay Component Scheduled Entry Transactions Data.
181
		 */
182
		transactionDetails?: {
183
			scheduledEntryId?: number
184
			earliestTransactionDate?: string
185
			latestTransactionDate?: string
186
			transactions?: {
187
				transactionId?: string
188
				date?: string
189
				amount?: number
190
				[k: string]: unknown
191
			}[]
192
			[k: string]: unknown
193
		}
194
		[k: string]: unknown
195
	}[]
196
	/**
197
	 * Pay Component transaction details.
198
	 */
199
	transactions?: {
200
		transactionId?: string
201
		date?: string
202
		amount?: number
203
		[k: string]: unknown
204
	}[]
205
	links?: {
206
		rel?: string
207
		href?: string
208
		hreflang?: string
209
		media?: string
210
		title?: string
211
		type?: string
212
		deprecation?: string
213
		profile?: string
214
		name?: string
215
		[k: string]: unknown
216
	}[]
217
	[k: string]: unknown
218
}
219