0
Get payment method domains
One script reply has been approved by the moderators Verified

Lists the details of existing payment method domains.

Created by hugo697 450 days ago Viewed 12027 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 450 days ago
1
type Stripe = {
2
  token: string;
3
};
4
/**
5
 * Get payment method domains
6
 * Lists the details of existing payment method domains.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  domain_name: string | undefined,
11
  enabled: 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_method_domains`);
18
  for (const [k, v] of [
19
    ["domain_name", domain_name],
20
    ["enabled", enabled],
21
    ["ending_before", ending_before],
22
    ["limit", limit],
23
    ["starting_after", starting_after],
24
  ]) {
25
    if (v !== undefined && v !== "") {
26
      url.searchParams.append(k, v);
27
    }
28
  }
29
  encodeParams({ expand }).forEach((v, k) => {
30
    if (v !== undefined && v !== "") {
31
      url.searchParams.append(k, v);
32
    }
33
  });
34
  const response = await fetch(url, {
35
    method: "GET",
36
    headers: {
37
      "Content-Type": "application/x-www-form-urlencoded",
38
      Authorization: "Bearer " + auth.token,
39
    },
40
    body: undefined,
41
  });
42
  if (!response.ok) {
43
    const text = await response.text();
44
    throw new Error(`${response.status} ${text}`);
45
  }
46
  return await response.json();
47
}
48

49
function encodeParams(o: any) {
50
  function iter(o: any, path: string) {
51
    if (Array.isArray(o)) {
52
      o.forEach(function (a) {
53
        iter(a, path + "[]");
54
      });
55
      return;
56
    }
57
    if (o !== null && typeof o === "object") {
58
      Object.keys(o).forEach(function (k) {
59
        iter(o[k], path + "[" + k + "]");
60
      });
61
      return;
62
    }
63
    data.push(path + "=" + o);
64
  }
65
  const data: string[] = [];
66
  Object.keys(o).forEach(function (k) {
67
    if (o[k] !== undefined) {
68
      iter(o[k], k);
69
    }
70
  });
71
  return new URLSearchParams(data.join("&"));
72
}
73