0

List Payment Intents

by
Published Oct 30, 2023

List PaymentIntents, newest first, optionally for one customer. `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 Payment Intents
5
 * List PaymentIntents, newest first, optionally for one customer. `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
  customer: string | undefined,
10
  created_gte: string | undefined,
11
  created_lte: string | undefined,
12
  limit: number | undefined,
13
  starting_after: string | undefined,
14
  ending_before: string | undefined
15
) {
16
  const url = new URL(`https://api.stripe.com/v1/payment_intents`)
17
  url.search = toForm({
18
    customer,
19
    created: { gte: ts(created_gte), lte: ts(created_lte) },
20
    limit,
21
    starting_after,
22
    ending_before,
23
  }).toString()
24

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

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

37
  return await response.json()
38
}
39

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

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

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