Post sources

Creates a new source object.

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 sources
6
 * Creates a new source object.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  body: {
11
    amount?: number;
12
    currency?: string;
13
    customer?: string;
14
    expand?: string[];
15
    flow?: "code_verification" | "none" | "receiver" | "redirect";
16
    mandate?: {
17
      acceptance?: {
18
        date?: number;
19
        ip?: string;
20
        offline?: { contact_email: string; [k: string]: unknown };
21
        online?: {
22
          date?: number;
23
          ip?: string;
24
          user_agent?: string;
25
          [k: string]: unknown;
26
        };
27
        status: "accepted" | "pending" | "refused" | "revoked";
28
        type?: "offline" | "online";
29
        user_agent?: string;
30
        [k: string]: unknown;
31
      };
32
      amount?: number | "";
33
      currency?: string;
34
      interval?: "one_time" | "scheduled" | "variable";
35
      notification_method?:
36
        | "deprecated_none"
37
        | "email"
38
        | "manual"
39
        | "none"
40
        | "stripe_email";
41
      [k: string]: unknown;
42
    };
43
    metadata?: { [k: string]: string };
44
    original_source?: string;
45
    owner?: {
46
      address?: {
47
        city?: string;
48
        country?: string;
49
        line1?: string;
50
        line2?: string;
51
        postal_code?: string;
52
        state?: string;
53
        [k: string]: unknown;
54
      };
55
      email?: string;
56
      name?: string;
57
      phone?: string;
58
      [k: string]: unknown;
59
    };
60
    receiver?: {
61
      refund_attributes_method?: "email" | "manual" | "none";
62
      [k: string]: unknown;
63
    };
64
    redirect?: { return_url: string; [k: string]: unknown };
65
    source_order?: {
66
      items?: {
67
        amount?: number;
68
        currency?: string;
69
        description?: string;
70
        parent?: string;
71
        quantity?: number;
72
        type?: "discount" | "shipping" | "sku" | "tax";
73
        [k: string]: unknown;
74
      }[];
75
      shipping?: {
76
        address: {
77
          city?: string;
78
          country?: string;
79
          line1: string;
80
          line2?: string;
81
          postal_code?: string;
82
          state?: string;
83
          [k: string]: unknown;
84
        };
85
        carrier?: string;
86
        name?: string;
87
        phone?: string;
88
        tracking_number?: string;
89
        [k: string]: unknown;
90
      };
91
      [k: string]: unknown;
92
    };
93
    statement_descriptor?: string;
94
    token?: string;
95
    type?: string;
96
    usage?: "reusable" | "single_use";
97
  }
98
) {
99
  const url = new URL(`https://api.stripe.com/v1/sources`);
100

101
  const response = await fetch(url, {
102
    method: "POST",
103
    headers: {
104
      "Content-Type": "application/x-www-form-urlencoded",
105
      Authorization: "Bearer " + auth.token,
106
    },
107
    body: encodeParams(body),
108
  });
109
  if (!response.ok) {
110
    const text = await response.text();
111
    throw new Error(`${response.status} ${text}`);
112
  }
113
  return await response.json();
114
}
115

116
function encodeParams(o: any) {
117
  function iter(o: any, path: string) {
118
    if (Array.isArray(o)) {
119
      o.forEach(function (a) {
120
        iter(a, path + "[]");
121
      });
122
      return;
123
    }
124
    if (o !== null && typeof o === "object") {
125
      Object.keys(o).forEach(function (k) {
126
        iter(o[k], path + "[" + k + "]");
127
      });
128
      return;
129
    }
130
    data.push(path + "=" + o);
131
  }
132
  const data: string[] = [];
133
  Object.keys(o).forEach(function (k) {
134
    if (o[k] !== undefined) {
135
      iter(o[k], k);
136
    }
137
  });
138
  return new URLSearchParams(data.join("&"));
139
}
140