0

Write Off Invoice

by
Published Oct 30, 2023

Mark an open invoice as uncollectible (bad debt) for accounting purposes.

Script stripe Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
/**
4
 * Write Off Invoice
5
 * Mark an open invoice as uncollectible (bad debt) for accounting purposes.
6
 */
7
export async function main(auth: RT.Stripe, invoice_id: string) {
8
  const response = await fetch(
9
    `https://api.stripe.com/v1/invoices/${invoice_id}/mark_uncollectible`,
10
    {
11
      method: "POST",
12
      headers: {
13
        Authorization: `Bearer ${auth.token}`,
14
        Accept: "application/json",
15
        "Stripe-Version": "2025-03-31.basil",
16
      },
17
    }
18
  )
19

20
  if (!response.ok) {
21
    throw new Error(`${response.status} ${await response.text()}`)
22
  }
23

24
  return await response.json()
25
}
26

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Stripe = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Post invoices invoice mark uncollectible
    6
     * Marking an invoice as uncollectible is useful for keeping track of bad debts that can be written off for accounting purposes.
    7
     */
    8
    export async function main(
    9
      auth: Stripe,
    10
      invoice: string,
    11
      body: { expand?: string[] }
    12
    ) {
    13
      const url = new URL(
    14
        `https://api.stripe.com/v1/invoices/${invoice}/mark_uncollectible`
    15
      );
    16
    
    
    17
      const response = await fetch(url, {
    18
        method: "POST",
    19
        headers: {
    20
          "Content-Type": "application/x-www-form-urlencoded",
    21
          Authorization: "Bearer " + auth.token,
    22
        },
    23
        body: encodeParams(body),
    24
      });
    25
      if (!response.ok) {
    26
        const text = await response.text();
    27
        throw new Error(`${response.status} ${text}`);
    28
      }
    29
      return await response.json();
    30
    }
    31
    
    
    32
    function encodeParams(o: any) {
    33
      function iter(o: any, path: string) {
    34
        if (Array.isArray(o)) {
    35
          o.forEach(function (a) {
    36
            iter(a, path + "[]");
    37
          });
    38
          return;
    39
        }
    40
        if (o !== null && typeof o === "object") {
    41
          Object.keys(o).forEach(function (k) {
    42
            iter(o[k], path + "[" + k + "]");
    43
          });
    44
          return;
    45
        }
    46
        data.push(path + "=" + o);
    47
      }
    48
      const data: string[] = [];
    49
      Object.keys(o).forEach(function (k) {
    50
        if (o[k] !== undefined) {
    51
          iter(o[k], k);
    52
        }
    53
      });
    54
      return new URLSearchParams(data.join("&"));
    55
    }
    56