1 | type Stripe = { |
2 | token: string; |
3 | }; |
4 | |
5 | * Post invoiceitems invoiceitem |
6 | * Updates the amount or description of an invoice item on an upcoming invoice. Updating an invoice item is only possible before the invoice it’s attached to is closed. |
7 | */ |
8 | export async function main( |
9 | auth: Stripe, |
10 | invoiceitem: string, |
11 | body: { |
12 | amount?: number; |
13 | description?: string; |
14 | discountable?: boolean; |
15 | discounts?: |
16 | | { coupon?: string; discount?: string; [k: string]: unknown }[] |
17 | | ""; |
18 | expand?: string[]; |
19 | metadata?: { [k: string]: string } | ""; |
20 | period?: { end: number; start: number; [k: string]: unknown }; |
21 | price?: string; |
22 | price_data?: { |
23 | currency: string; |
24 | product: string; |
25 | tax_behavior?: "exclusive" | "inclusive" | "unspecified"; |
26 | unit_amount?: number; |
27 | unit_amount_decimal?: string; |
28 | [k: string]: unknown; |
29 | }; |
30 | quantity?: number; |
31 | tax_behavior?: "exclusive" | "inclusive" | "unspecified"; |
32 | tax_code?: string | ""; |
33 | tax_rates?: string[] | ""; |
34 | unit_amount?: number; |
35 | unit_amount_decimal?: string; |
36 | } |
37 | ) { |
38 | const url = new URL(`https://api.stripe.com/v1/invoiceitems/${invoiceitem}`); |
39 |
|
40 | const response = await fetch(url, { |
41 | method: "POST", |
42 | headers: { |
43 | "Content-Type": "application/x-www-form-urlencoded", |
44 | Authorization: "Bearer " + auth.token, |
45 | }, |
46 | body: encodeParams(body), |
47 | }); |
48 | if (!response.ok) { |
49 | const text = await response.text(); |
50 | throw new Error(`${response.status} ${text}`); |
51 | } |
52 | return await response.json(); |
53 | } |
54 |
|
55 | function encodeParams(o: any) { |
56 | function iter(o: any, path: string) { |
57 | if (Array.isArray(o)) { |
58 | o.forEach(function (a) { |
59 | iter(a, path + "[]"); |
60 | }); |
61 | return; |
62 | } |
63 | if (o !== null && typeof o === "object") { |
64 | Object.keys(o).forEach(function (k) { |
65 | iter(o[k], path + "[" + k + "]"); |
66 | }); |
67 | return; |
68 | } |
69 | data.push(path + "=" + o); |
70 | } |
71 | const data: string[] = []; |
72 | Object.keys(o).forEach(function (k) { |
73 | if (o[k] !== undefined) { |
74 | iter(o[k], k); |
75 | } |
76 | }); |
77 | return new URLSearchParams(data.join("&")); |
78 | } |
79 |
|