0

Apply credits

by
Published Oct 17, 2025

Apply the customer credits either from credit notes or excess customer payments to an invoice. Multiple credits can be applied at once.

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
7
 * Apply the customer credits either from credit notes or excess customer payments to an invoice. Multiple credits can be applied at once.
8
 */
9
export async function main(
10
  auth: Zoho,
11
  invoice_id: string,
12
  organization_id: string | undefined,
13
  body: {
14
    invoice_payments?: { payment_id?: string; amount_applied?: number }[];
15
    apply_creditnotes?: { creditnote_id?: string; amount_applied?: number }[];
16
  },
17
) {
18
  const url = new URL(
19
    `https://www.zohoapis.com/inventory/v1/invoices/${invoice_id}/credits`,
20
  );
21
  for (const [k, v] of [["organization_id", organization_id]]) {
22
    if (v !== undefined && v !== "" && k !== undefined) {
23
      url.searchParams.append(k, v);
24
    }
25
  }
26
  const response = await fetch(url, {
27
    method: "POST",
28
    headers: {
29
      "Content-Type": "application/json",
30
      Authorization: "Zoho-oauthtoken " + auth.token,
31
    },
32
    body: JSON.stringify(body),
33
  });
34
  if (!response.ok) {
35
    const text = await response.text();
36
    throw new Error(`${response.status} ${text}`);
37
  }
38
  return await response.json();
39
}
40