0

Retrieve Payment Intent

by
Published Oct 30, 2023

Retrieve a PaymentIntent by id.

Script stripe Verified

The script

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

3
/**
4
 * Retrieve Payment Intent
5
 * Retrieve a PaymentIntent by id.
6
 */
7
export async function main(auth: RT.Stripe, payment_intent_id: string) {
8
  const response = await fetch(
9
    `https://api.stripe.com/v1/payment_intents/${payment_intent_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 payment intents intent
    6
     * Retrieves the details of a PaymentIntent that has previously been created. 
    7
    
    
    8
    You can retrieve a PaymentIntent client-side using a publishable key when the client_secret is in the query string. 
    9
    
    
    10
    If you retrieve a PaymentIntent with a publishable key, it only returns a subset of properties. Refer to the payment intent object reference for more details.
    11
     */
    12
    export async function main(
    13
      auth: Stripe,
    14
      intent: string,
    15
      client_secret: string | undefined,
    16
      expand: any
    17
    ) {
    18
      const url = new URL(`https://api.stripe.com/v1/payment_intents/${intent}`);
    19
      for (const [k, v] of [["client_secret", client_secret]]) {
    20
        if (v !== undefined && v !== "") {
    21
          url.searchParams.append(k, v);
    22
        }
    23
      }
    24
      encodeParams({ expand }).forEach((v, k) => {
    25
        if (v !== undefined && v !== "") {
    26
          url.searchParams.append(k, v);
    27
        }
    28
      });
    29
      const response = await fetch(url, {
    30
        method: "GET",
    31
        headers: {
    32
          "Content-Type": "application/x-www-form-urlencoded",
    33
          Authorization: "Bearer " + auth.token,
    34
        },
    35
        body: undefined,
    36
      });
    37
      if (!response.ok) {
    38
        const text = await response.text();
    39
        throw new Error(`${response.status} ${text}`);
    40
      }
    41
      return await response.json();
    42
    }
    43
    
    
    44
    function encodeParams(o: any) {
    45
      function iter(o: any, path: string) {
    46
        if (Array.isArray(o)) {
    47
          o.forEach(function (a) {
    48
            iter(a, path + "[]");
    49
          });
    50
          return;
    51
        }
    52
        if (o !== null && typeof o === "object") {
    53
          Object.keys(o).forEach(function (k) {
    54
            iter(o[k], path + "[" + k + "]");
    55
          });
    56
          return;
    57
        }
    58
        data.push(path + "=" + o);
    59
      }
    60
      const data: string[] = [];
    61
      Object.keys(o).forEach(function (k) {
    62
        if (o[k] !== undefined) {
    63
          iter(o[k], k);
    64
        }
    65
      });
    66
      return new URLSearchParams(data.join("&"));
    67
    }
    68