Post invoices invoice lines line item id

Updates an invoice’s line item. Some fields, such as tax_amounts, only live on the invoice line item, so they can only be updated through this endpoint. Other fields, such as amount, live on both the invoice item and the invoice line item, so updates on this endpoint will propagate to the invoice item as well. Updating an invoice’s line item is only possible before the invoice is finalized.

Script stripe Verified

by hugo697 · 3/6/2024

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 368 days ago
1
type Stripe = {
2
  token: string;
3
};
4
/**
5
 * Post invoices invoice lines line item id
6
 * Updates an invoice’s line item. Some fields, such as tax_amounts, only live on the invoice line item,
7
so they can only be updated through this endpoint. Other fields, such as amount, live on both the invoice
8
item and the invoice line item, so updates on this endpoint will propagate to the invoice item as well.
9
Updating an invoice’s line item is only possible before the invoice is finalized.
10
 */
11
export async function main(
12
  auth: Stripe,
13
  invoice: string,
14
  line_item_id: string,
15
  body: {
16
    amount?: number;
17
    description?: string;
18
    discountable?: boolean;
19
    discounts?:
20
      | { coupon?: string; discount?: string; [k: string]: unknown }[]
21
      | "";
22
    expand?: string[];
23
    metadata?: { [k: string]: string } | "";
24
    period?: { end: number; start: number; [k: string]: unknown };
25
    price?: string;
26
    price_data?: {
27
      currency: string;
28
      product?: string;
29
      product_data?: {
30
        description?: string;
31
        images?: string[];
32
        metadata?: { [k: string]: string };
33
        name: string;
34
        tax_code?: string;
35
        [k: string]: unknown;
36
      };
37
      tax_behavior?: "exclusive" | "inclusive" | "unspecified";
38
      unit_amount?: number;
39
      unit_amount_decimal?: string;
40
      [k: string]: unknown;
41
    };
42
    quantity?: number;
43
    tax_amounts?:
44
      | {
45
          amount: number;
46
          tax_rate_data: {
47
            country?: string;
48
            description?: string;
49
            display_name: string;
50
            inclusive: boolean;
51
            jurisdiction?: string;
52
            percentage: number;
53
            state?: string;
54
            tax_type?:
55
              | "amusement_tax"
56
              | "communications_tax"
57
              | "gst"
58
              | "hst"
59
              | "igst"
60
              | "jct"
61
              | "lease_tax"
62
              | "pst"
63
              | "qst"
64
              | "rst"
65
              | "sales_tax"
66
              | "vat";
67
            [k: string]: unknown;
68
          };
69
          taxable_amount: number;
70
          [k: string]: unknown;
71
        }[]
72
      | "";
73
    tax_rates?: string[] | "";
74
  }
75
) {
76
  const url = new URL(
77
    `https://api.stripe.com/v1/invoices/${invoice}/lines/${line_item_id}`
78
  );
79

80
  const response = await fetch(url, {
81
    method: "POST",
82
    headers: {
83
      "Content-Type": "application/x-www-form-urlencoded",
84
      Authorization: "Bearer " + auth.token,
85
    },
86
    body: encodeParams(body),
87
  });
88
  if (!response.ok) {
89
    const text = await response.text();
90
    throw new Error(`${response.status} ${text}`);
91
  }
92
  return await response.json();
93
}
94

95
function encodeParams(o: any) {
96
  function iter(o: any, path: string) {
97
    if (Array.isArray(o)) {
98
      o.forEach(function (a) {
99
        iter(a, path + "[]");
100
      });
101
      return;
102
    }
103
    if (o !== null && typeof o === "object") {
104
      Object.keys(o).forEach(function (k) {
105
        iter(o[k], path + "[" + k + "]");
106
      });
107
      return;
108
    }
109
    data.push(path + "=" + o);
110
  }
111
  const data: string[] = [];
112
  Object.keys(o).forEach(function (k) {
113
    if (o[k] !== undefined) {
114
      iter(o[k], k);
115
    }
116
  });
117
  return new URLSearchParams(data.join("&"));
118
}
119