Get issuing cardholders

Returns a list of Issuing Cardholder objects. The objects are sorted in descending order by creation date, with the most recently created object appearing first.

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
 * Get issuing cardholders
6
 * Returns a list of Issuing Cardholder objects. The objects are sorted in descending order by creation date, with the most recently created object appearing first.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  created: any,
11
  email: string | undefined,
12
  ending_before: string | undefined,
13
  expand: any,
14
  limit: string | undefined,
15
  phone_number: string | undefined,
16
  starting_after: string | undefined,
17
  status: "active" | "blocked" | "inactive" | undefined,
18
  type: "company" | "individual" | undefined
19
) {
20
  const url = new URL(`https://api.stripe.com/v1/issuing/cardholders`);
21
  for (const [k, v] of [
22
    ["email", email],
23
    ["ending_before", ending_before],
24
    ["limit", limit],
25
    ["phone_number", phone_number],
26
    ["starting_after", starting_after],
27
    ["status", status],
28
    ["type", type],
29
  ]) {
30
    if (v !== undefined && v !== "") {
31
      url.searchParams.append(k, v);
32
    }
33
  }
34
  encodeParams({ created, expand }).forEach((v, k) => {
35
    if (v !== undefined && v !== "") {
36
      url.searchParams.append(k, v);
37
    }
38
  });
39
  const response = await fetch(url, {
40
    method: "GET",
41
    headers: {
42
      "Content-Type": "application/x-www-form-urlencoded",
43
      Authorization: "Bearer " + auth.token,
44
    },
45
    body: undefined,
46
  });
47
  if (!response.ok) {
48
    const text = await response.text();
49
    throw new Error(`${response.status} ${text}`);
50
  }
51
  return await response.json();
52
}
53

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