Post identity verification sessions

Creates a VerificationSession object. After the VerificationSession is created, display a verification modal using the session client_secret or send your users to the session’s url. If your API key is in test mode, verification checks won’t actually process, though everything else will occur as if in live mode. Related guide: Verify your users’ identity documents

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
6
 * Creates a VerificationSession object.
7

8
After the VerificationSession is created, display a verification modal using the session client_secret or send your users to the session’s url.
9

10
If your API key is in test mode, verification checks won’t actually process, though everything else will occur as if in live mode.
11

12
Related guide: Verify your users’ identity documents
13
 */
14
export async function main(
15
  auth: Stripe,
16
  body: {
17
    client_reference_id?: string;
18
    expand?: string[];
19
    metadata?: { [k: string]: string };
20
    options?: {
21
      document?:
22
        | {
23
            allowed_types?: ("driving_license" | "id_card" | "passport")[];
24
            require_id_number?: boolean;
25
            require_live_capture?: boolean;
26
            require_matching_selfie?: boolean;
27
            [k: string]: unknown;
28
          }
29
        | "";
30
      [k: string]: unknown;
31
    };
32
    return_url?: string;
33
    type: "document" | "id_number";
34
  }
35
) {
36
  const url = new URL(
37
    `https://api.stripe.com/v1/identity/verification_sessions`
38
  );
39

40
  const response = await fetch(url, {
41
    method: "POST",
42
    headers: {
43
      "Content-Type": "application/x-www-form-urlencoded",
44
      Authorization: "Bearer " + auth.token,
45
    },
46
    body: encodeParams(body),
47
  });
48
  if (!response.ok) {
49
    const text = await response.text();
50
    throw new Error(`${response.status} ${text}`);
51
  }
52
  return await response.json();
53
}
54

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