Post charges

This method is no longer recommended—use the Payment Intents API to initiate a new payment instead. Confirmation of the PaymentIntent creates the Charge object used to request payment.

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 charges
6
 * This method is no longer recommended—use the Payment Intents API
7
to initiate a new payment instead. Confirmation of the PaymentIntent creates the Charge
8
object used to request payment.
9
 */
10
export async function main(
11
  auth: Stripe,
12
  body: {
13
    amount?: number;
14
    application_fee?: number;
15
    application_fee_amount?: number;
16
    capture?: boolean;
17
    card?:
18
      | {
19
          address_city?: string;
20
          address_country?: string;
21
          address_line1?: string;
22
          address_line2?: string;
23
          address_state?: string;
24
          address_zip?: string;
25
          cvc?: string;
26
          exp_month: number;
27
          exp_year: number;
28
          metadata?: { [k: string]: string };
29
          name?: string;
30
          number: string;
31
          object?: "card";
32
          [k: string]: unknown;
33
        }
34
      | string;
35
    currency?: string;
36
    customer?: string;
37
    description?: string;
38
    destination?:
39
      | { account: string; amount?: number; [k: string]: unknown }
40
      | string;
41
    expand?: string[];
42
    metadata?: { [k: string]: string } | "";
43
    on_behalf_of?: string;
44
    radar_options?: { session?: string; [k: string]: unknown };
45
    receipt_email?: string;
46
    shipping?: {
47
      address: {
48
        city?: string;
49
        country?: string;
50
        line1?: string;
51
        line2?: string;
52
        postal_code?: string;
53
        state?: string;
54
        [k: string]: unknown;
55
      };
56
      carrier?: string;
57
      name: string;
58
      phone?: string;
59
      tracking_number?: string;
60
      [k: string]: unknown;
61
    };
62
    source?: string;
63
    statement_descriptor?: string;
64
    statement_descriptor_suffix?: string;
65
    transfer_data?: {
66
      amount?: number;
67
      destination: string;
68
      [k: string]: unknown;
69
    };
70
    transfer_group?: string;
71
  }
72
) {
73
  const url = new URL(`https://api.stripe.com/v1/charges`);
74

75
  const response = await fetch(url, {
76
    method: "POST",
77
    headers: {
78
      "Content-Type": "application/x-www-form-urlencoded",
79
      Authorization: "Bearer " + auth.token,
80
    },
81
    body: encodeParams(body),
82
  });
83
  if (!response.ok) {
84
    const text = await response.text();
85
    throw new Error(`${response.status} ${text}`);
86
  }
87
  return await response.json();
88
}
89

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