0

Apply credits to invoices

by
Published Oct 17, 2025

Apply credit note to existing invoices.

Script zoho Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Zoho = {
3
  token: string;
4
};
5
/**
6
 * Apply credits to invoices
7
 * Apply credit note to existing invoices.
8
 */
9
export async function main(
10
  auth: Zoho,
11
  creditnote_id: string,
12
  organization_id: string | undefined,
13
  body: {
14
    invoices?: { invoice_id?: string; amount_applied?: number }[];
15
    invoice_id: string;
16
    amount_applied: number;
17
  },
18
) {
19
  const url = new URL(
20
    `https://www.zohoapis.com/inventory/v1/creditnotes/${creditnote_id}/invoices`,
21
  );
22
  for (const [k, v] of [["organization_id", organization_id]]) {
23
    if (v !== undefined && v !== "" && k !== undefined) {
24
      url.searchParams.append(k, v);
25
    }
26
  }
27
  const response = await fetch(url, {
28
    method: "POST",
29
    headers: {
30
      "Content-Type": "application/json",
31
      Authorization: "Zoho-oauthtoken " + auth.token,
32
    },
33
    body: JSON.stringify(body),
34
  });
35
  if (!response.ok) {
36
    const text = await response.text();
37
    throw new Error(`${response.status} ${text}`);
38
  }
39
  return await response.json();
40
}
41