0

Retrieve Checkout Session

by
Published Oct 30, 2023

Retrieve a Checkout Session by id (`cs_...`), e.g. after a `checkout.session.completed` webhook.

Script stripe Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
/**
4
 * Retrieve Checkout Session
5
 * Retrieve a Checkout Session by id (`cs_...`), e.g. after a `checkout.session.completed` webhook.
6
 */
7
export async function main(auth: RT.Stripe, session_id: string) {
8
  const response = await fetch(
9
    `https://api.stripe.com/v1/checkout/sessions/${session_id}`,
10
    {
11
      headers: {
12
        Authorization: `Bearer ${auth.token}`,
13
        Accept: "application/json",
14
        "Stripe-Version": "2025-03-31.basil",
15
      },
16
    }
17
  )
18

19
  if (!response.ok) {
20
    throw new Error(`${response.status} ${await response.text()}`)
21
  }
22

23
  return await response.json()
24
}
25

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Stripe = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Get checkout sessions session
    6
     * Retrieves a Session object.
    7
     */
    8
    export async function main(auth: Stripe, session: string, expand: any) {
    9
      const url = new URL(`https://api.stripe.com/v1/checkout/sessions/${session}`);
    10
      encodeParams({ expand }).forEach((v, k) => {
    11
        if (v !== undefined && v !== "") {
    12
          url.searchParams.append(k, v);
    13
        }
    14
      });
    15
      const response = await fetch(url, {
    16
        method: "GET",
    17
        headers: {
    18
          "Content-Type": "application/x-www-form-urlencoded",
    19
          Authorization: "Bearer " + auth.token,
    20
        },
    21
        body: undefined,
    22
      });
    23
      if (!response.ok) {
    24
        const text = await response.text();
    25
        throw new Error(`${response.status} ${text}`);
    26
      }
    27
      return await response.json();
    28
    }
    29
    
    
    30
    function encodeParams(o: any) {
    31
      function iter(o: any, path: string) {
    32
        if (Array.isArray(o)) {
    33
          o.forEach(function (a) {
    34
            iter(a, path + "[]");
    35
          });
    36
          return;
    37
        }
    38
        if (o !== null && typeof o === "object") {
    39
          Object.keys(o).forEach(function (k) {
    40
            iter(o[k], path + "[" + k + "]");
    41
          });
    42
          return;
    43
        }
    44
        data.push(path + "=" + o);
    45
      }
    46
      const data: string[] = [];
    47
      Object.keys(o).forEach(function (k) {
    48
        if (o[k] !== undefined) {
    49
          iter(o[k], k);
    50
        }
    51
      });
    52
      return new URLSearchParams(data.join("&"));
    53
    }
    54