0

Upsert Subscription

by
Published Oct 17, 2025

> 🚧 Partner Restricted > All assessment API endpoints are restricted to assessment providers that have signed a Paylocity technology partnership agreement.

Script paylocity Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Paylocity = {
3
	clientId: string
4
	clientSecret: string
5
}
6
/**
7
 * Upsert Subscription
8
 * > 🚧 Partner Restricted
9
> All assessment API endpoints are restricted to assessment providers that have signed a Paylocity technology partnership agreement.
10
 */
11
export async function main(
12
	auth: Paylocity,
13
	companyId: string,
14
	body: {
15
		level?: string
16
		price?: number
17
		currency?: string
18
		term?: string
19
		expirationDate?: string
20
		billingCodes?: { code?: string; isActive?: false | true }[]
21
	},
22
	testMode?: string
23
) {
24
	const url = new URL(
25
		`https://dc1prodgwext.paylocity.com/apiHub/performanceManagement/v1/companies/${companyId}/assessmentOrders/subscription`
26
	)
27

28
	const response = await fetch(url, {
29
		method: 'PUT',
30
		headers: {
31
			...(testMode ? { testMode: testMode } : {}),
32
			'Content-Type': 'application/json',
33
			Authorization:
34
				'Bearer ' +
35
				(await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token'))
36
		},
37
		body: JSON.stringify(body)
38
	})
39
	if (!response.ok) {
40
		const text = await response.text()
41
		throw new Error(`${response.status} ${text}`)
42
	}
43
	return await response.json()
44
}
45

46
async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> {
47
	const params = new URLSearchParams({
48
		grant_type: 'client_credentials',
49
		client_id: auth.clientId,
50
		client_secret: auth.clientSecret
51
	})
52

53
	const response = await fetch(tokenUrl, {
54
		method: 'POST',
55
		headers: {
56
			Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`),
57
			'Content-Type': 'application/x-www-form-urlencoded'
58
		},
59
		body: params.toString()
60
	})
61

62
	if (!response.ok) {
63
		const text = await response.text()
64
		throw new Error(`OAuth token request failed: ${response.status} ${text}`)
65
	}
66

67
	const data = await response.json()
68
	return data.access_token
69
}
70