1 | |
2 |
|
3 | |
4 | * Create Invoice Item |
5 | * Add a line item to a draft invoice (`invoice`) or to the customer's pending items for their next invoice. Give either a `price` id (with `quantity`) or an `amount` in the smallest currency unit plus `currency`. `extra_params` passes any other documented field through as-is. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Stripe, |
9 | customer: string, |
10 | invoice: string | undefined, |
11 | price: string | undefined, |
12 | quantity: number | undefined, |
13 | amount: number | undefined, |
14 | currency: string | undefined, |
15 | description: string | undefined, |
16 | subscription: string | undefined, |
17 | metadata: { [key: string]: string } | undefined, |
18 | extra_params: { [key: string]: any } | undefined |
19 | ) { |
20 | const response = await fetch(`https://api.stripe.com/v1/invoiceitems`, { |
21 | method: "POST", |
22 | headers: { |
23 | Authorization: `Bearer ${auth.token}`, |
24 | "Content-Type": "application/x-www-form-urlencoded", |
25 | Accept: "application/json", |
26 | "Stripe-Version": "2025-03-31.basil", |
27 | }, |
28 | body: toForm({ |
29 | customer, |
30 | invoice, |
31 | pricing: { price }, |
32 | quantity, |
33 | amount, |
34 | currency, |
35 | description, |
36 | subscription, |
37 | metadata, |
38 | ...extra_params, |
39 | }), |
40 | }) |
41 |
|
42 | if (!response.ok) { |
43 | throw new Error(`${response.status} ${await response.text()}`) |
44 | } |
45 |
|
46 | return await response.json() |
47 | } |
48 |
|
49 | |
50 | function toForm( |
51 | params: { [key: string]: any }, |
52 | prefix = "", |
53 | out = new URLSearchParams() |
54 | ) { |
55 | for (const [key, value] of Object.entries(params)) { |
56 | if (value == null || value === "") continue |
57 | const name = prefix ? `${prefix}[${key}]` : key |
58 | if (typeof value === "object") toForm(value, name, out) |
59 | else out.append(name, String(value)) |
60 | } |
61 | return out |
62 | } |
63 |
|