0

List Payouts

by
Published Oct 30, 2023

List payouts, newest first, filtered by status, destination bank account/card or creation date. `created_gte`/`created_lte` take ISO 8601 date-times. Returns one page (`limit` 1-100, default 10); pass the last object's id as `starting_after` for the next page while `has_more` is true.

Script stripe Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
/**
4
 * List Payouts
5
 * List payouts, newest first, filtered by status, destination bank account/card or creation date. `created_gte`/`created_lte` take ISO 8601 date-times. Returns one page (`limit` 1-100, default 10); pass the last object's id as `starting_after` for the next page while `has_more` is true.
6
 */
7
export async function main(
8
  auth: RT.Stripe,
9
  status: "pending" | "paid" | "failed" | "canceled" | undefined,
10
  destination: string | undefined,
11
  created_gte: string | undefined,
12
  created_lte: string | undefined,
13
  limit: number | undefined,
14
  starting_after: string | undefined,
15
  ending_before: string | undefined
16
) {
17
  const url = new URL(`https://api.stripe.com/v1/payouts`)
18
  url.search = toForm({
19
    status,
20
    destination,
21
    created: { gte: ts(created_gte), lte: ts(created_lte) },
22
    limit,
23
    starting_after,
24
    ending_before,
25
  }).toString()
26

27
  const response = await fetch(url, {
28
    headers: {
29
      Authorization: `Bearer ${auth.token}`,
30
      Accept: "application/json",
31
      "Stripe-Version": "2025-03-31.basil",
32
    },
33
  })
34

35
  if (!response.ok) {
36
    throw new Error(`${response.status} ${await response.text()}`)
37
  }
38

39
  return await response.json()
40
}
41

42
// Stripe takes form-encoded params with bracketed nesting: metadata[key]=v, items[0][price]=p
43
function toForm(
44
  params: { [key: string]: any },
45
  prefix = "",
46
  out = new URLSearchParams()
47
) {
48
  for (const [key, value] of Object.entries(params)) {
49
    if (value == null || value === "") continue
50
    const name = prefix ? `${prefix}[${key}]` : key
51
    if (typeof value === "object") toForm(value, name, out)
52
    else out.append(name, String(value))
53
  }
54
  return out
55
}
56

57
const ts = (iso: string | undefined) =>
58
  iso ? Math.floor(new Date(iso).getTime() / 1000) : undefined
59

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Stripe = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Get payouts
    6
     * Returns a list of existing payouts sent to third-party bank accounts or payouts that Stripe sent to you. The payouts return in sorted order, with the most recently created payouts appearing first.
    7
     */
    8
    export async function main(
    9
      auth: Stripe,
    10
      arrival_date: any,
    11
      created: any,
    12
      destination: string | undefined,
    13
      ending_before: string | undefined,
    14
      expand: any,
    15
      limit: string | undefined,
    16
      starting_after: string | undefined,
    17
      status: string | undefined
    18
    ) {
    19
      const url = new URL(`https://api.stripe.com/v1/payouts`);
    20
      for (const [k, v] of [
    21
        ["destination", destination],
    22
        ["ending_before", ending_before],
    23
        ["limit", limit],
    24
        ["starting_after", starting_after],
    25
        ["status", status],
    26
      ]) {
    27
        if (v !== undefined && v !== "") {
    28
          url.searchParams.append(k, v);
    29
        }
    30
      }
    31
      encodeParams({ arrival_date, created, expand }).forEach((v, k) => {
    32
        if (v !== undefined && v !== "") {
    33
          url.searchParams.append(k, v);
    34
        }
    35
      });
    36
      const response = await fetch(url, {
    37
        method: "GET",
    38
        headers: {
    39
          "Content-Type": "application/x-www-form-urlencoded",
    40
          Authorization: "Bearer " + auth.token,
    41
        },
    42
        body: undefined,
    43
      });
    44
      if (!response.ok) {
    45
        const text = await response.text();
    46
        throw new Error(`${response.status} ${text}`);
    47
      }
    48
      return await response.json();
    49
    }
    50
    
    
    51
    function encodeParams(o: any) {
    52
      function iter(o: any, path: string) {
    53
        if (Array.isArray(o)) {
    54
          o.forEach(function (a) {
    55
            iter(a, path + "[]");
    56
          });
    57
          return;
    58
        }
    59
        if (o !== null && typeof o === "object") {
    60
          Object.keys(o).forEach(function (k) {
    61
            iter(o[k], path + "[" + k + "]");
    62
          });
    63
          return;
    64
        }
    65
        data.push(path + "=" + o);
    66
      }
    67
      const data: string[] = [];
    68
      Object.keys(o).forEach(function (k) {
    69
        if (o[k] !== undefined) {
    70
          iter(o[k], k);
    71
        }
    72
      });
    73
      return new URLSearchParams(data.join("&"));
    74
    }
    75