Get financial connections transactions

Returns a list of Financial Connections Transaction objects.

Script stripe Verified

by hugo697 ยท 3/6/2024

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 368 days ago
1
type Stripe = {
2
  token: string;
3
};
4
/**
5
 * Get financial connections transactions
6
 * Returns a list of Financial Connections Transaction objects.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  account: string | undefined,
11
  ending_before: string | undefined,
12
  expand: any,
13
  limit: string | undefined,
14
  starting_after: string | undefined,
15
  transacted_at: any,
16
  transaction_refresh: any
17
) {
18
  const url = new URL(
19
    `https://api.stripe.com/v1/financial_connections/transactions`
20
  );
21
  for (const [k, v] of [
22
    ["account", account],
23
    ["ending_before", ending_before],
24
    ["limit", limit],
25
    ["starting_after", starting_after],
26
  ]) {
27
    if (v !== undefined && v !== "") {
28
      url.searchParams.append(k, v);
29
    }
30
  }
31
  encodeParams({ expand, transacted_at, transaction_refresh }).forEach(
32
    (v, k) => {
33
      if (v !== undefined && v !== "") {
34
        url.searchParams.append(k, v);
35
      }
36
    }
37
  );
38
  const response = await fetch(url, {
39
    method: "GET",
40
    headers: {
41
      "Content-Type": "application/x-www-form-urlencoded",
42
      Authorization: "Bearer " + auth.token,
43
    },
44
    body: undefined,
45
  });
46
  if (!response.ok) {
47
    const text = await response.text();
48
    throw new Error(`${response.status} ${text}`);
49
  }
50
  return await response.json();
51
}
52

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