0

Post account links

by
Published Oct 30, 2023

Creates an AccountLink object that includes a single-use Stripe URL that the platform can redirect their user to in order to take them through the Connect Onboarding flow.

Script stripe Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 399 days ago
1
type Stripe = {
2
  token: string;
3
};
4
/**
5
 * Post account links
6
 * Creates an AccountLink object that includes a single-use Stripe URL that the platform can redirect their user to in order to take them through the Connect Onboarding flow.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  body: {
11
    account: string;
12
    collect?: "currently_due" | "eventually_due";
13
    collection_options?: {
14
      fields: "currently_due" | "eventually_due";
15
      future_requirements?: "include" | "omit";
16
      [k: string]: unknown;
17
    };
18
    expand?: string[];
19
    refresh_url?: string;
20
    return_url?: string;
21
    type: "account_onboarding" | "account_update";
22
  }
23
) {
24
  const url = new URL(`https://api.stripe.com/v1/account_links`);
25

26
  const response = await fetch(url, {
27
    method: "POST",
28
    headers: {
29
      "Content-Type": "application/x-www-form-urlencoded",
30
      Authorization: "Bearer " + auth.token,
31
    },
32
    body: encodeParams(body),
33
  });
34
  if (!response.ok) {
35
    const text = await response.text();
36
    throw new Error(`${response.status} ${text}`);
37
  }
38
  return await response.json();
39
}
40

41
function encodeParams(o: any) {
42
  function iter(o: any, path: string) {
43
    if (Array.isArray(o)) {
44
      o.forEach(function (a) {
45
        iter(a, path + "[]");
46
      });
47
      return;
48
    }
49
    if (o !== null && typeof o === "object") {
50
      Object.keys(o).forEach(function (k) {
51
        iter(o[k], path + "[" + k + "]");
52
      });
53
      return;
54
    }
55
    data.push(path + "=" + o);
56
  }
57
  const data: string[] = [];
58
  Object.keys(o).forEach(function (k) {
59
    if (o[k] !== undefined) {
60
      iter(o[k], k);
61
    }
62
  });
63
  return new URLSearchParams(data.join("&"));
64
}
65