Post application fees fee refunds id

Updates the specified application fee refund by setting the values of the parameters passed. Any parameters not provided will be left unchanged. This request only accepts metadata as an argument.

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
 * Post application fees fee refunds id
6
 * Updates the specified application fee refund by setting the values of the parameters passed. Any parameters not provided will be left unchanged.
7

8
This request only accepts metadata as an argument.
9
 */
10
export async function main(
11
  auth: Stripe,
12
  fee: string,
13
  id: string,
14
  body: { expand?: string[]; metadata?: { [k: string]: string } | "" }
15
) {
16
  const url = new URL(
17
    `https://api.stripe.com/v1/application_fees/${fee}/refunds/${id}`
18
  );
19

20
  const response = await fetch(url, {
21
    method: "POST",
22
    headers: {
23
      "Content-Type": "application/x-www-form-urlencoded",
24
      Authorization: "Bearer " + auth.token,
25
    },
26
    body: encodeParams(body),
27
  });
28
  if (!response.ok) {
29
    const text = await response.text();
30
    throw new Error(`${response.status} ${text}`);
31
  }
32
  return await response.json();
33
}
34

35
function encodeParams(o: any) {
36
  function iter(o: any, path: string) {
37
    if (Array.isArray(o)) {
38
      o.forEach(function (a) {
39
        iter(a, path + "[]");
40
      });
41
      return;
42
    }
43
    if (o !== null && typeof o === "object") {
44
      Object.keys(o).forEach(function (k) {
45
        iter(o[k], path + "[" + k + "]");
46
      });
47
      return;
48
    }
49
    data.push(path + "=" + o);
50
  }
51
  const data: string[] = [];
52
  Object.keys(o).forEach(function (k) {
53
    if (o[k] !== undefined) {
54
      iter(o[k], k);
55
    }
56
  });
57
  return new URLSearchParams(data.join("&"));
58
}
59