Post accounts account people

Creates a new person.

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 people
6
 * Creates a new person.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  account: string,
11
  body: {
12
    additional_tos_acceptances?: {
13
      account?: {
14
        date?: number;
15
        ip?: string;
16
        user_agent?: string | "";
17
        [k: string]: unknown;
18
      };
19
      [k: string]: unknown;
20
    };
21
    address?: {
22
      city?: string;
23
      country?: string;
24
      line1?: string;
25
      line2?: string;
26
      postal_code?: string;
27
      state?: string;
28
      [k: string]: unknown;
29
    };
30
    address_kana?: {
31
      city?: string;
32
      country?: string;
33
      line1?: string;
34
      line2?: string;
35
      postal_code?: string;
36
      state?: string;
37
      town?: string;
38
      [k: string]: unknown;
39
    };
40
    address_kanji?: {
41
      city?: string;
42
      country?: string;
43
      line1?: string;
44
      line2?: string;
45
      postal_code?: string;
46
      state?: string;
47
      town?: string;
48
      [k: string]: unknown;
49
    };
50
    dob?:
51
      | { day: number; month: number; year: number; [k: string]: unknown }
52
      | "";
53
    documents?: {
54
      company_authorization?: { files?: (string | "")[]; [k: string]: unknown };
55
      passport?: { files?: (string | "")[]; [k: string]: unknown };
56
      visa?: { files?: (string | "")[]; [k: string]: unknown };
57
      [k: string]: unknown;
58
    };
59
    email?: string;
60
    expand?: string[];
61
    first_name?: string;
62
    first_name_kana?: string;
63
    first_name_kanji?: string;
64
    full_name_aliases?: string[] | "";
65
    gender?: string;
66
    id_number?: string;
67
    id_number_secondary?: string;
68
    last_name?: string;
69
    last_name_kana?: string;
70
    last_name_kanji?: string;
71
    maiden_name?: string;
72
    metadata?: { [k: string]: string } | "";
73
    nationality?: string;
74
    person_token?: string;
75
    phone?: string;
76
    political_exposure?: string;
77
    registered_address?: {
78
      city?: string;
79
      country?: string;
80
      line1?: string;
81
      line2?: string;
82
      postal_code?: string;
83
      state?: string;
84
      [k: string]: unknown;
85
    };
86
    relationship?: {
87
      director?: boolean;
88
      executive?: boolean;
89
      legal_guardian?: boolean;
90
      owner?: boolean;
91
      percent_ownership?: number | "";
92
      representative?: boolean;
93
      title?: string;
94
      [k: string]: unknown;
95
    };
96
    ssn_last_4?: string;
97
    verification?: {
98
      additional_document?: {
99
        back?: string;
100
        front?: string;
101
        [k: string]: unknown;
102
      };
103
      document?: { back?: string; front?: string; [k: string]: unknown };
104
      [k: string]: unknown;
105
    };
106
  }
107
) {
108
  const url = new URL(`https://api.stripe.com/v1/accounts/${account}/people`);
109

110
  const response = await fetch(url, {
111
    method: "POST",
112
    headers: {
113
      "Content-Type": "application/x-www-form-urlencoded",
114
      Authorization: "Bearer " + auth.token,
115
    },
116
    body: encodeParams(body),
117
  });
118
  if (!response.ok) {
119
    const text = await response.text();
120
    throw new Error(`${response.status} ${text}`);
121
  }
122
  return await response.json();
123
}
124

125
function encodeParams(o: any) {
126
  function iter(o: any, path: string) {
127
    if (Array.isArray(o)) {
128
      o.forEach(function (a) {
129
        iter(a, path + "[]");
130
      });
131
      return;
132
    }
133
    if (o !== null && typeof o === "object") {
134
      Object.keys(o).forEach(function (k) {
135
        iter(o[k], path + "[" + k + "]");
136
      });
137
      return;
138
    }
139
    data.push(path + "=" + o);
140
  }
141
  const data: string[] = [];
142
  Object.keys(o).forEach(function (k) {
143
    if (o[k] !== undefined) {
144
      iter(o[k], k);
145
    }
146
  });
147
  return new URLSearchParams(data.join("&"));
148
}
149