Post accounts account bank accounts id

Updates the metadata, account holder name, account holder type of a bank account belonging to a Custom account, and optionally sets it as the default for its currency. Other bank account details are not editable by design. You can re-enable a disabled bank account by performing an update call without providing any arguments or changes.

Script stripe Verified

by hugo697 ยท 10/30/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 368 days ago
1
type Stripe = {
2
  token: string;
3
};
4
/**
5
 * Post accounts account bank accounts id
6
 * Updates the metadata, account holder name, account holder type of a bank account belonging to a Custom account, and optionally sets it as the default for its currency. Other bank account details are not editable by design.
7

8
You can re-enable a disabled bank account by performing an update call without providing any arguments or changes.
9
 */
10
export async function main(
11
  auth: Stripe,
12
  account: string,
13
  id: string,
14
  body: {
15
    account_holder_name?: string;
16
    account_holder_type?: "" | "company" | "individual";
17
    account_type?: "checking" | "futsu" | "savings" | "toza";
18
    address_city?: string;
19
    address_country?: string;
20
    address_line1?: string;
21
    address_line2?: string;
22
    address_state?: string;
23
    address_zip?: string;
24
    default_for_currency?: boolean;
25
    documents?: {
26
      bank_account_ownership_verification?: {
27
        files?: string[];
28
        [k: string]: unknown;
29
      };
30
      [k: string]: unknown;
31
    };
32
    exp_month?: string;
33
    exp_year?: string;
34
    expand?: string[];
35
    metadata?: { [k: string]: string } | "";
36
    name?: string;
37
  }
38
) {
39
  const url = new URL(
40
    `https://api.stripe.com/v1/accounts/${account}/bank_accounts/${id}`
41
  );
42

43
  const response = await fetch(url, {
44
    method: "POST",
45
    headers: {
46
      "Content-Type": "application/x-www-form-urlencoded",
47
      Authorization: "Bearer " + auth.token,
48
    },
49
    body: encodeParams(body),
50
  });
51
  if (!response.ok) {
52
    const text = await response.text();
53
    throw new Error(`${response.status} ${text}`);
54
  }
55
  return await response.json();
56
}
57

58
function encodeParams(o: any) {
59
  function iter(o: any, path: string) {
60
    if (Array.isArray(o)) {
61
      o.forEach(function (a) {
62
        iter(a, path + "[]");
63
      });
64
      return;
65
    }
66
    if (o !== null && typeof o === "object") {
67
      Object.keys(o).forEach(function (k) {
68
        iter(o[k], path + "[" + k + "]");
69
      });
70
      return;
71
    }
72
    data.push(path + "=" + o);
73
  }
74
  const data: string[] = [];
75
  Object.keys(o).forEach(function (k) {
76
    if (o[k] !== undefined) {
77
      iter(o[k], k);
78
    }
79
  });
80
  return new URLSearchParams(data.join("&"));
81
}
82