0

Update Refund

by
Published Oct 30, 2023

Update a refund's metadata (the only editable field); keys are merged into the existing metadata.

Script stripe Verified

The script

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

3
/**
4
 * Update Refund
5
 * Update a refund's metadata (the only editable field); keys are merged into the existing metadata.
6
 */
7
export async function main(
8
  auth: RT.Stripe,
9
  refund_id: string,
10
  metadata: { [key: string]: string }
11
) {
12
  const response = await fetch(
13
    `https://api.stripe.com/v1/refunds/${refund_id}`,
14
    {
15
      method: "POST",
16
      headers: {
17
        Authorization: `Bearer ${auth.token}`,
18
        "Content-Type": "application/x-www-form-urlencoded",
19
        Accept: "application/json",
20
        "Stripe-Version": "2025-03-31.basil",
21
      },
22
      body: toForm({ metadata }),
23
    }
24
  )
25

26
  if (!response.ok) {
27
    throw new Error(`${response.status} ${await response.text()}`)
28
  }
29

30
  return await response.json()
31
}
32

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

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Stripe = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Post refunds refund
    6
     * Updates the refund that you specify by setting the values of the passed parameters. Any parameters that you don’t provide remain unchanged.
    7
    
    
    8
    This request only accepts metadata as an argument.
    9
     */
    10
    export async function main(
    11
      auth: Stripe,
    12
      refund: string,
    13
      body: { expand?: string[]; metadata?: { [k: string]: string } | "" }
    14
    ) {
    15
      const url = new URL(`https://api.stripe.com/v1/refunds/${refund}`);
    16
    
    
    17
      const response = await fetch(url, {
    18
        method: "POST",
    19
        headers: {
    20
          "Content-Type": "application/x-www-form-urlencoded",
    21
          Authorization: "Bearer " + auth.token,
    22
        },
    23
        body: encodeParams(body),
    24
      });
    25
      if (!response.ok) {
    26
        const text = await response.text();
    27
        throw new Error(`${response.status} ${text}`);
    28
      }
    29
      return await response.json();
    30
    }
    31
    
    
    32
    function encodeParams(o: any) {
    33
      function iter(o: any, path: string) {
    34
        if (Array.isArray(o)) {
    35
          o.forEach(function (a) {
    36
            iter(a, path + "[]");
    37
          });
    38
          return;
    39
        }
    40
        if (o !== null && typeof o === "object") {
    41
          Object.keys(o).forEach(function (k) {
    42
            iter(o[k], path + "[" + k + "]");
    43
          });
    44
          return;
    45
        }
    46
        data.push(path + "=" + o);
    47
      }
    48
      const data: string[] = [];
    49
      Object.keys(o).forEach(function (k) {
    50
        if (o[k] !== undefined) {
    51
          iter(o[k], k);
    52
        }
    53
      });
    54
      return new URLSearchParams(data.join("&"));
    55
    }
    56