Retrieves payments for invoices and credit notes

Script xero Verified

by hugo697 ยท 12/20/2024

The script

Submitted by hugo697 Bun
Verified 515 days ago
1
//native
2
type Xero = {
3
	token: string
4
}
5
/**
6
 * Retrieves payments for invoices and credit notes
7
 *
8
 */
9
export async function main(
10
	auth: Xero,
11
	where: string | undefined,
12
	order: string | undefined,
13
	page: string | undefined,
14
	pageSize: string | undefined,
15
	xero_tenant_id: string,
16
	If_Modified_Since: string
17
) {
18
	const url = new URL(`https://api.xero.com/api.xro/2.0/Payments`)
19
	for (const [k, v] of [
20
		['where', where],
21
		['order', order],
22
		['page', page],
23
		['pageSize', pageSize]
24
	]) {
25
		if (v !== undefined && v !== '' && k !== undefined) {
26
			url.searchParams.append(k, v)
27
		}
28
	}
29
	const response = await fetch(url, {
30
		method: 'GET',
31
		headers: {
32
			Accept: 'application/json',
33
			'xero-tenant-id': xero_tenant_id,
34
			'If-Modified-Since': If_Modified_Since,
35
			Authorization: 'Bearer ' + auth.token
36
		},
37
		body: undefined
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