0
Get radar early fraud warnings early fraud warning
One script reply has been approved by the moderators Verified

Retrieves the details of an early fraud warning that has previously been created.

Please refer to the early fraud warning object reference for more details.

Created by hugo697 314 days ago Viewed 9000 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 314 days ago
1
type Stripe = {
2
  token: string;
3
};
4
/**
5
 * Get radar early fraud warnings early fraud warning
6
 * Retrieves the details of an early fraud warning that has previously been created. 
7

8
Please refer to the early fraud warning object reference for more details.
9
 */
10
export async function main(
11
  auth: Stripe,
12
  early_fraud_warning: string,
13
  expand: any
14
) {
15
  const url = new URL(
16
    `https://api.stripe.com/v1/radar/early_fraud_warnings/${early_fraud_warning}`
17
  );
18
  encodeParams({ expand }).forEach((v, k) => {
19
    if (v !== undefined && v !== "") {
20
      url.searchParams.append(k, v);
21
    }
22
  });
23
  const response = await fetch(url, {
24
    method: "GET",
25
    headers: {
26
      "Content-Type": "application/x-www-form-urlencoded",
27
      Authorization: "Bearer " + auth.token,
28
    },
29
    body: undefined,
30
  });
31
  if (!response.ok) {
32
    const text = await response.text();
33
    throw new Error(`${response.status} ${text}`);
34
  }
35
  return await response.json();
36
}
37

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