1 | |
2 |
|
3 | |
4 | * Update Customer |
5 | * Update a customer. Empty fields are left unchanged; metadata keys are merged into the existing metadata. `extra_params` passes any other documented field through as-is. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Stripe, |
9 | customer_id: string, |
10 | name: string | undefined, |
11 | email: string | undefined, |
12 | phone: string | undefined, |
13 | description: string | undefined, |
14 | address: |
15 | | { |
16 | line1?: string |
17 | line2?: string |
18 | city?: string |
19 | state?: string |
20 | postal_code?: string |
21 | country?: string |
22 | } |
23 | | undefined, |
24 | metadata: { [key: string]: string } | undefined, |
25 | extra_params: { [key: string]: any } | undefined |
26 | ) { |
27 | const response = await fetch( |
28 | `https://api.stripe.com/v1/customers/${customer_id}`, |
29 | { |
30 | method: "POST", |
31 | headers: { |
32 | Authorization: `Bearer ${auth.token}`, |
33 | "Content-Type": "application/x-www-form-urlencoded", |
34 | Accept: "application/json", |
35 | "Stripe-Version": "2025-03-31.basil", |
36 | }, |
37 | body: toForm({ |
38 | name, |
39 | email, |
40 | phone, |
41 | description, |
42 | address, |
43 | metadata, |
44 | ...extra_params, |
45 | }), |
46 | } |
47 | ) |
48 |
|
49 | if (!response.ok) { |
50 | throw new Error(`${response.status} ${await response.text()}`) |
51 | } |
52 |
|
53 | return await response.json() |
54 | } |
55 |
|
56 | |
57 | function toForm( |
58 | params: { [key: string]: any }, |
59 | prefix = "", |
60 | out = new URLSearchParams() |
61 | ) { |
62 | for (const [key, value] of Object.entries(params)) { |
63 | if (value == null || value === "") continue |
64 | const name = prefix ? `${prefix}[${key}]` : key |
65 | if (typeof value === "object") toForm(value, name, out) |
66 | else out.append(name, String(value)) |
67 | } |
68 | return out |
69 | } |
70 |
|