0

List Refunds

by
Published Oct 30, 2023

List refunds, newest first, optionally for one payment intent or charge. 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 Refunds
5
 * List refunds, newest first, optionally for one payment intent or charge. 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
  payment_intent: string | undefined,
10
  charge: string | undefined,
11
  limit: number | undefined,
12
  starting_after: string | undefined,
13
  ending_before: string | undefined
14
) {
15
  const url = new URL(`https://api.stripe.com/v1/refunds`)
16
  url.search = toForm({
17
    payment_intent,
18
    charge,
19
    limit,
20
    starting_after,
21
    ending_before,
22
  }).toString()
23

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

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

36
  return await response.json()
37
}
38

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

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