0

Get balance history

by
Published Oct 30, 2023

Returns a list of transactions that have contributed to the Stripe account balance (e.g., charges, transfers, and so forth). The transactions are returned in sorted order, with the most recent transactions appearing first. Note that this endpoint was previously called “Balance history” and used the path /v1/balance/history.

Script stripe Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 398 days ago
1
type Stripe = {
2
  token: string;
3
};
4
/**
5
 * Get balance history
6
 * Returns a list of transactions that have contributed to the Stripe account balance (e.g., charges, transfers, and so forth). The transactions are returned in sorted order, with the most recent transactions appearing first.
7

8
Note that this endpoint was previously called “Balance history” and used the path /v1/balance/history.
9
 */
10
export async function main(
11
  auth: Stripe,
12
  created: any,
13
  currency: string | undefined,
14
  ending_before: string | undefined,
15
  expand: any,
16
  limit: string | undefined,
17
  payout: string | undefined,
18
  source: string | undefined,
19
  starting_after: string | undefined,
20
  type: string | undefined
21
) {
22
  const url = new URL(`https://api.stripe.com/v1/balance/history`);
23
  for (const [k, v] of [
24
    ["currency", currency],
25
    ["ending_before", ending_before],
26
    ["limit", limit],
27
    ["payout", payout],
28
    ["source", source],
29
    ["starting_after", starting_after],
30
    ["type", type],
31
  ]) {
32
    if (v !== undefined && v !== "") {
33
      url.searchParams.append(k, v);
34
    }
35
  }
36
  encodeParams({ created, expand }).forEach((v, k) => {
37
    if (v !== undefined && v !== "") {
38
      url.searchParams.append(k, v);
39
    }
40
  });
41
  const response = await fetch(url, {
42
    method: "GET",
43
    headers: {
44
      "Content-Type": "application/x-www-form-urlencoded",
45
      Authorization: "Bearer " + auth.token,
46
    },
47
    body: undefined,
48
  });
49
  if (!response.ok) {
50
    const text = await response.text();
51
    throw new Error(`${response.status} ${text}`);
52
  }
53
  return await response.json();
54
}
55

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