Post treasury financial accounts

Creates a new FinancialAccount. For now, each connected account can only have one FinancialAccount.

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 treasury financial accounts
6
 * Creates a new FinancialAccount. For now, each connected account can only have one FinancialAccount.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  body: {
11
    expand?: string[];
12
    features?: {
13
      card_issuing?: { requested: boolean; [k: string]: unknown };
14
      deposit_insurance?: { requested: boolean; [k: string]: unknown };
15
      financial_addresses?: {
16
        aba?: { requested: boolean; [k: string]: unknown };
17
        [k: string]: unknown;
18
      };
19
      inbound_transfers?: {
20
        ach?: { requested: boolean; [k: string]: unknown };
21
        [k: string]: unknown;
22
      };
23
      intra_stripe_flows?: { requested: boolean; [k: string]: unknown };
24
      outbound_payments?: {
25
        ach?: { requested: boolean; [k: string]: unknown };
26
        us_domestic_wire?: { requested: boolean; [k: string]: unknown };
27
        [k: string]: unknown;
28
      };
29
      outbound_transfers?: {
30
        ach?: { requested: boolean; [k: string]: unknown };
31
        us_domestic_wire?: { requested: boolean; [k: string]: unknown };
32
        [k: string]: unknown;
33
      };
34
      [k: string]: unknown;
35
    };
36
    metadata?: { [k: string]: string };
37
    platform_restrictions?: {
38
      inbound_flows?: "restricted" | "unrestricted";
39
      outbound_flows?: "restricted" | "unrestricted";
40
      [k: string]: unknown;
41
    };
42
    supported_currencies: string[];
43
  }
44
) {
45
  const url = new URL(`https://api.stripe.com/v1/treasury/financial_accounts`);
46

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

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