1 | |
2 | type Zoho = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Update a payment using a custom field's unique value |
7 | * A custom field will have unique values if it's configured to not accept duplicate values. |
8 | */ |
9 | export async function main( |
10 | auth: Zoho, |
11 | organization_id: string | undefined, |
12 | X_Unique_Identifier_Key: string, |
13 | X_Unique_Identifier_Value: string, |
14 | body: { |
15 | customer_id: string; |
16 | payment_mode: string; |
17 | amount: number; |
18 | date?: string; |
19 | reference_number?: string; |
20 | description?: string; |
21 | invoices: { invoice_id?: string; amount_applied?: number }[]; |
22 | exchange_rate?: number; |
23 | payment_form?: string; |
24 | bank_charges?: number; |
25 | custom_fields?: { label?: string; value?: string }[]; |
26 | invoice_id: string; |
27 | amount_applied: number; |
28 | tax_amount_withheld?: number; |
29 | location_id?: string; |
30 | account_id?: string; |
31 | }, |
32 | X_Upsert?: string, |
33 | ) { |
34 | const url = new URL(`https://www.zohoapis.com/books/v3/customerpayments`); |
35 | for (const [k, v] of [["organization_id", organization_id]]) { |
36 | if (v !== undefined && v !== "" && k !== undefined) { |
37 | url.searchParams.append(k, v); |
38 | } |
39 | } |
40 | const response = await fetch(url, { |
41 | method: "PUT", |
42 | headers: { |
43 | "X-Unique-Identifier-Key": X_Unique_Identifier_Key, |
44 | "X-Unique-Identifier-Value": X_Unique_Identifier_Value, |
45 | ...(X_Upsert ? { "X-Upsert": X_Upsert } : {}), |
46 | "Content-Type": "application/json", |
47 | Authorization: "Zoho-oauthtoken " + auth.token, |
48 | }, |
49 | body: JSON.stringify(body), |
50 | }); |
51 | if (!response.ok) { |
52 | const text = await response.text(); |
53 | throw new Error(`${response.status} ${text}`); |
54 | } |
55 | return await response.json(); |
56 | } |
57 |
|