0

Get customers customer cards id

Published Oct 30, 2023

You can always see the 10 most recent cards directly on a customer; this method lets you retrieve details about a specific card stored on the customer.

Script stripe
  • Submitted by hugo697 Typescript (fetch-only)
    Created 566 days ago
    1
    type Stripe = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Get customers customer cards id
    6
     * You can always see the 10 most recent cards directly on a customer; this method lets you retrieve details about a specific card stored on the customer.
    7
     */
    8
    export async function main(
    9
      auth: Stripe,
    10
      customer: string,
    11
      id: string,
    12
      expand: any
    13
    ) {
    14
      const url = new URL(
    15
        `https://api.stripe.com/v1/customers/${customer}/cards/${id}`
    16
      );
    17
      encodeParams({ expand }).forEach((v, k) => {
    18
        if (v !== undefined && v !== "") {
    19
          url.searchParams.append(k, v);
    20
        }
    21
      });
    22
      const response = await fetch(url, {
    23
        method: "GET",
    24
        headers: {
    25
          "Content-Type": "application/x-www-form-urlencoded",
    26
          Authorization: "Bearer " + auth.token,
    27
        },
    28
        body: undefined,
    29
      });
    30
      if (!response.ok) {
    31
        const text = await response.text();
    32
        throw new Error(`${response.status} ${text}`);
    33
      }
    34
      return await response.json();
    35
    }
    36
    
    
    37
    function encodeParams(o: any) {
    38
      function iter(o: any, path: string) {
    39
        if (Array.isArray(o)) {
    40
          o.forEach(function (a) {
    41
            iter(a, path + "[]");
    42
          });
    43
          return;
    44
        }
    45
        if (o !== null && typeof o === "object") {
    46
          Object.keys(o).forEach(function (k) {
    47
            iter(o[k], path + "[" + k + "]");
    48
          });
    49
          return;
    50
        }
    51
        data.push(path + "=" + o);
    52
      }
    53
      const data: string[] = [];
    54
      Object.keys(o).forEach(function (k) {
    55
        if (o[k] !== undefined) {
    56
          iter(o[k], k);
    57
        }
    58
      });
    59
      return new URLSearchParams(data.join("&"));
    60
    }
    61