//native
type Paylocity = {
clientId: string
clientSecret: string
}
/**
* Get Local Tax for Tax Code
* Get Local Tax for Tax Code returns local tax information for the specific tax code for the selected employee.
*/
export async function main(
auth: Paylocity,
companyId: string,
employeeId: string,
taxCode: string
) {
const url = new URL(
`https://dc1prodgwext.paylocity.com/api/v1/companies/${companyId}/employees/${employeeId}/localTaxes/${taxCode}`
)
const response = await fetch(url, {
method: 'GET',
headers: {
Authorization:
'Bearer ' +
(await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token'))
},
body: undefined
})
if (!response.ok) {
const text = await response.text()
throw new Error(`${response.status} ${text}`)
}
return await response.json()
}
async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> {
const params = new URLSearchParams({
grant_type: 'client_credentials',
client_id: auth.clientId,
client_secret: auth.clientSecret
})
const response = await fetch(tokenUrl, {
method: 'POST',
headers: {
Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`),
'Content-Type': 'application/x-www-form-urlencoded'
},
body: params.toString()
})
if (!response.ok) {
const text = await response.text()
throw new Error(`OAuth token request failed: ${response.status} ${text}`)
}
const data = await response.json()
return data.access_token
}
Submitted by hugo697 235 days ago