Post identity verification sessions session cancel

A VerificationSession object can be canceled when it is in requires_input status. Once canceled, future submission attempts are disabled. This cannot be undone. Learn more.

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 identity verification sessions session cancel
6
 * A VerificationSession object can be canceled when it is in requires_input status.
7

8
Once canceled, future submission attempts are disabled. This cannot be undone. Learn more.
9
 */
10
export async function main(
11
  auth: Stripe,
12
  session: string,
13
  body: { expand?: string[] }
14
) {
15
  const url = new URL(
16
    `https://api.stripe.com/v1/identity/verification_sessions/${session}/cancel`
17
  );
18

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

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