Get payment methods

Returns a list of PaymentMethods for Treasury flows. If you want to list the PaymentMethods attached to a Customer for payments, you should use the List a Customer’s PaymentMethods API instead.

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
 * Get payment methods
6
 * Returns a list of PaymentMethods for Treasury flows. If you want to list the PaymentMethods attached to a Customer for payments, you should use the List a Customer’s PaymentMethods API instead.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  customer: string | undefined,
11
  ending_before: string | undefined,
12
  expand: any,
13
  limit: string | undefined,
14
  starting_after: string | undefined,
15
  type:
16
    | "acss_debit"
17
    | "affirm"
18
    | "afterpay_clearpay"
19
    | "alipay"
20
    | "au_becs_debit"
21
    | "bacs_debit"
22
    | "bancontact"
23
    | "blik"
24
    | "boleto"
25
    | "card"
26
    | "cashapp"
27
    | "customer_balance"
28
    | "eps"
29
    | "fpx"
30
    | "giropay"
31
    | "grabpay"
32
    | "ideal"
33
    | "klarna"
34
    | "konbini"
35
    | "link"
36
    | "oxxo"
37
    | "p24"
38
    | "paynow"
39
    | "paypal"
40
    | "pix"
41
    | "promptpay"
42
    | "revolut_pay"
43
    | "sepa_debit"
44
    | "sofort"
45
    | "swish"
46
    | "us_bank_account"
47
    | "wechat_pay"
48
    | "zip"
49
    | undefined
50
) {
51
  const url = new URL(`https://api.stripe.com/v1/payment_methods`);
52
  for (const [k, v] of [
53
    ["customer", customer],
54
    ["ending_before", ending_before],
55
    ["limit", limit],
56
    ["starting_after", starting_after],
57
    ["type", type],
58
  ]) {
59
    if (v !== undefined && v !== "") {
60
      url.searchParams.append(k, v);
61
    }
62
  }
63
  encodeParams({ expand }).forEach((v, k) => {
64
    if (v !== undefined && v !== "") {
65
      url.searchParams.append(k, v);
66
    }
67
  });
68
  const response = await fetch(url, {
69
    method: "GET",
70
    headers: {
71
      "Content-Type": "application/x-www-form-urlencoded",
72
      Authorization: "Bearer " + auth.token,
73
    },
74
    body: undefined,
75
  });
76
  if (!response.ok) {
77
    const text = await response.text();
78
    throw new Error(`${response.status} ${text}`);
79
  }
80
  return await response.json();
81
}
82

83
function encodeParams(o: any) {
84
  function iter(o: any, path: string) {
85
    if (Array.isArray(o)) {
86
      o.forEach(function (a) {
87
        iter(a, path + "[]");
88
      });
89
      return;
90
    }
91
    if (o !== null && typeof o === "object") {
92
      Object.keys(o).forEach(function (k) {
93
        iter(o[k], path + "[" + k + "]");
94
      });
95
      return;
96
    }
97
    data.push(path + "=" + o);
98
  }
99
  const data: string[] = [];
100
  Object.keys(o).forEach(function (k) {
101
    if (o[k] !== undefined) {
102
      iter(o[k], k);
103
    }
104
  });
105
  return new URLSearchParams(data.join("&"));
106
}
107