Get issuing cards

Returns a list of Issuing Card 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 cards
6
 * Returns a list of Issuing Card 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
  cardholder: string | undefined,
11
  created: any,
12
  ending_before: string | undefined,
13
  exp_month: string | undefined,
14
  exp_year: string | undefined,
15
  expand: any,
16
  last4: string | undefined,
17
  limit: string | undefined,
18
  starting_after: string | undefined,
19
  status: "active" | "canceled" | "inactive" | undefined,
20
  type: "physical" | "virtual" | undefined
21
) {
22
  const url = new URL(`https://api.stripe.com/v1/issuing/cards`);
23
  for (const [k, v] of [
24
    ["cardholder", cardholder],
25
    ["ending_before", ending_before],
26
    ["exp_month", exp_month],
27
    ["exp_year", exp_year],
28
    ["last4", last4],
29
    ["limit", limit],
30
    ["starting_after", starting_after],
31
    ["status", status],
32
    ["type", type],
33
  ]) {
34
    if (v !== undefined && v !== "") {
35
      url.searchParams.append(k, v);
36
    }
37
  }
38
  encodeParams({ created, expand }).forEach((v, k) => {
39
    if (v !== undefined && v !== "") {
40
      url.searchParams.append(k, v);
41
    }
42
  });
43
  const response = await fetch(url, {
44
    method: "GET",
45
    headers: {
46
      "Content-Type": "application/x-www-form-urlencoded",
47
      Authorization: "Bearer " + auth.token,
48
    },
49
    body: undefined,
50
  });
51
  if (!response.ok) {
52
    const text = await response.text();
53
    throw new Error(`${response.status} ${text}`);
54
  }
55
  return await response.json();
56
}
57

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