Edits history of script submission #17600 for ' Worker Federal Tax (paychex)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    /**
     * Worker Federal Tax
     * Update the federal tax setup for an "Active" or "In-progress" worker. The patching will allow you to change the combination that you have with the extra and overrides.
     */
    export async function main(auth: RT.Paychex, workerId: string, body: Body) {
    	const accessToken = await getOAuthToken(auth, 'https://api.paychex.com/auth/oauth/v2/token')
    	const url = new URL(`https://api.paychex.com/workers/${workerId}/federaltax`)
    
    	const response = await fetch(url, {
    		method: 'PATCH',
    		headers: {
    			'Content-Type': 'application/json',
    			Authorization: 'Bearer ' + accessToken
    		},
    		body: JSON.stringify(body)
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.json()
    }
    
    async function getOAuthToken(auth: RT.Paychex, tokenUrl: string): Promise<string> {
    	const params = new URLSearchParams({
    		grant_type: 'client_credentials'
    	})
    
    	const response = await fetch(tokenUrl, {
    		method: 'POST',
    		headers: {
    			Authorization: 'Basic ' + btoa(`${auth.client_id}:${auth.client_secret}`),
    			'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
    }
    
    /* eslint-disable */
    /**
     * This file was automatically generated by json-schema-to-typescript.
     * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
     * and run json-schema-to-typescript to regenerate this file.
     */
    
    /**
     * The representational state of the a worker's federal tax setup.
     */
    export interface Body {
    	/**
    	 * The ID for the federal tax item.
    	 */
    	taxId?: string
    	/**
    	 * Filing status.
    	 */
    	filingStatus?:
    		| 'SINGLE_OR_MARRIED_FILING_SEPARATELY'
    		| 'MARRIED_FILING_JOINTLY'
    		| 'HEAD_OF_HOUSEHOLD'
    	/**
    	 * See federal W-4 instructions.
    	 */
    	multipleJobs?: 'true' | 'false'
    	/**
    	 * See federal W-4 instructions.
    	 */
    	dependentsAmount?: string
    	/**
    	 * See federal W-4 instructions.
    	 */
    	otherIncome?: string
    	/**
    	 * See federal W-4 instructions.
    	 */
    	deductionsAmount?: string
    	/**
    	 * Should federal taxes be withheld:
    	 * true means federal taxes are withheld,
    	 * false means federal taxes are NOT withheld. Earnings will still be reported to state and federal agencies.
    	 */
    	taxesWithheld?: 'true' | 'false'
    	/**
    	 * See federal W-4 instructions. Can be used with extraWithholdingPercentage or overrideWithholdingPercentage. Cannot be used with overrideWithholdingAmount. If extraWithholdingAmount is entered, existing overrideWithholdingAmount data will be removed.
    	 */
    	extraWithholdingAmount?: string
    	/**
    	 * See federal W-4 instructions. Cannot be used with extraWithholdingAmount, extraWithholdingPercentage, or overrideWithholdingPercentage. If entered, existing extraWithholdingAmount, extraWithholdingPercentage, and overrideWithholdingPercentage data will be removed.
    	 */
    	overrideWithholdingAmount?: string
    	/**
    	 * See federal W-4 instructions. Can be used with extraWithholdingAmount. Cannot be used with overrideWithholdingAmount or overrideWithholdingPercentage. If entered, existing overrideWithholdingAmount and overrideWithholdingPercentage data will be removed.
    	 */
    	extraWithholdingPercentage?: string
    	/**
    	 * See federal W-4 instructions. Can be used with extraWithholdingAmount. Cannot be used with overrideWithholdingAmount or extraWithholdingPercentage. If entered, existing overrideWithholdingAmount and extraWithholdingPercentage data will be removed.
    	 */
    	overrideWithholdingPercentage?: string
    	[k: string]: unknown
    }
    

    Submitted by hugo697 235 days ago