1

Capture Payment Intent

by
Published Aug 23, 2022

Capture the funds of a PaymentIntent in `requires_capture` status. Leave `amount_to_capture` empty to capture the full amount; the remainder of a partial capture is released. Uncaptured intents are canceled after 7 days by default. `extra_params` passes any other documented field through as-is.

Script stripe Verified

The script

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

3
/**
4
 * Capture Payment Intent
5
 * Capture the funds of a PaymentIntent in `requires_capture` status. Leave `amount_to_capture` empty to capture the full amount; the remainder of a partial capture is released. Uncaptured intents are canceled after 7 days by default. `extra_params` passes any other documented field through as-is.
6
 */
7
export async function main(
8
  auth: RT.Stripe,
9
  payment_intent_id: string,
10
  amount_to_capture: number | undefined,
11
  metadata: { [key: string]: string } | undefined,
12
  extra_params: { [key: string]: any } | undefined
13
) {
14
  const response = await fetch(
15
    `https://api.stripe.com/v1/payment_intents/${payment_intent_id}/capture`,
16
    {
17
      method: "POST",
18
      headers: {
19
        Authorization: `Bearer ${auth.token}`,
20
        "Content-Type": "application/x-www-form-urlencoded",
21
        Accept: "application/json",
22
        "Stripe-Version": "2025-03-31.basil",
23
      },
24
      body: toForm({ amount_to_capture, metadata, ...extra_params }),
25
    }
26
  )
27

28
  if (!response.ok) {
29
    throw new Error(`${response.status} ${await response.text()}`)
30
  }
31

32
  return await response.json()
33
}
34

35
// Stripe takes form-encoded params with bracketed nesting: metadata[key]=v, items[0][price]=p
36
function toForm(
37
  params: { [key: string]: any },
38
  prefix = "",
39
  out = new URLSearchParams()
40
) {
41
  for (const [key, value] of Object.entries(params)) {
42
    if (value == null || value === "") continue
43
    const name = prefix ? `${prefix}[${key}]` : key
44
    if (typeof value === "object") toForm(value, name, out)
45
    else out.append(name, String(value))
46
  }
47
  return out
48
}
49

Other submissions
  • Submitted by rossmccrann Deno
    Created 528 days ago
    1
    type Stripe = {
    2
      token: string;
    3
    };
    4
    export async function main(stripe_con: Stripe, id: string) {
    5
      const STRIPE_CAPTURE_PAYMENT_URL = `https://api.stripe.com/v1/payment_intents/:${id}/capture`;
    6
    
    
    7
      const token = stripe_con["token"];
    8
    
    
    9
      const response = await fetch(STRIPE_CAPTURE_PAYMENT_URL, {
    10
        method: "POST",
    11
        headers: {
    12
          Authorization: "Bearer " + token,
    13
          "Content-Type": "application/json",
    14
        },
    15
      });
    16
    
    
    17
      return await response.json();
    18
    }
    19