Edits history of script submission #21997 for ' Update a payment using a custom field's unique value (zoho)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Zoho = {
      token: string;
    };
    /**
     * Update a payment using a custom field's unique value
     * A custom field will have unique values if it's configured to not accept duplicate values.
     */
    export async function main(
      auth: Zoho,
      organization_id: string | undefined,
      X_Unique_Identifier_Key: string,
      X_Unique_Identifier_Value: string,
      body: {
        customer_id: string;
        payment_mode: string;
        amount: number;
        date?: string;
        reference_number?: string;
        description?: string;
        invoices: { invoice_id?: string; amount_applied?: number }[];
        exchange_rate?: number;
        payment_form?: string;
        bank_charges?: number;
        custom_fields?: { label?: string; value?: string }[];
        invoice_id: string;
        amount_applied: number;
        tax_amount_withheld?: number;
        location_id?: string;
        account_id?: string;
      },
      X_Upsert?: string,
    ) {
      const url = new URL(`https://www.zohoapis.com/books/v3/customerpayments`);
      for (const [k, v] of [["organization_id", organization_id]]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "PUT",
        headers: {
          "X-Unique-Identifier-Key": X_Unique_Identifier_Key,
          "X-Unique-Identifier-Value": X_Unique_Identifier_Value,
          ...(X_Upsert ? { "X-Upsert": X_Upsert } : {}),
          "Content-Type": "application/json",
          Authorization: "Zoho-oauthtoken " + auth.token,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 235 days ago