1

Create Refund

by
Published Aug 23, 2022

Refund a payment, given its `payment_intent` or `charge`. Leave `amount` empty to refund everything that remains; otherwise it's in the smallest currency unit. Set `idempotency_key` to make retries safe: Stripe returns the first result for a repeated key within 24 hours. `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
 * Create Refund
5
 * Refund a payment, given its `payment_intent` or `charge`. Leave `amount` empty to refund everything that remains; otherwise it's in the smallest currency unit. Set `idempotency_key` to make retries safe: Stripe returns the first result for a repeated key within 24 hours. `extra_params` passes any other documented field through as-is.
6
 */
7
export async function main(
8
  auth: RT.Stripe,
9
  payment_intent: string | undefined,
10
  charge: string | undefined,
11
  amount: number | undefined,
12
  reason: "duplicate" | "fraudulent" | "requested_by_customer" | undefined,
13
  metadata: { [key: string]: string } | undefined,
14
  idempotency_key: string | undefined,
15
  extra_params: { [key: string]: any } | undefined
16
) {
17
  const response = await fetch(`https://api.stripe.com/v1/refunds`, {
18
    method: "POST",
19
    headers: {
20
      Authorization: `Bearer ${auth.token}`,
21
      "Content-Type": "application/x-www-form-urlencoded",
22
      Accept: "application/json",
23
      "Stripe-Version": "2025-03-31.basil",
24
      ...(idempotency_key ? { "Idempotency-Key": idempotency_key } : {}),
25
    },
26
    body: toForm({
27
      payment_intent,
28
      charge,
29
      amount,
30
      reason,
31
      metadata,
32
      ...extra_params,
33
    }),
34
  })
35

36
  if (!response.ok) {
37
    throw new Error(`${response.status} ${await response.text()}`)
38
  }
39

40
  return await response.json()
41
}
42

43
// Stripe takes form-encoded params with bracketed nesting: metadata[key]=v, items[0][price]=p
44
function toForm(
45
  params: { [key: string]: any },
46
  prefix = "",
47
  out = new URLSearchParams()
48
) {
49
  for (const [key, value] of Object.entries(params)) {
50
    if (value == null || value === "") continue
51
    const name = prefix ? `${prefix}[${key}]` : key
52
    if (typeof value === "object") toForm(value, name, out)
53
    else out.append(name, String(value))
54
  }
55
  return out
56
}
57

Other submissions
  • Submitted by rossmccrann Deno
    Created 528 days ago
    1
    import Stripe from "https://esm.sh/stripe?target=deno";
    2
    
    
    3
    type Stripe = {
    4
      token: string;
    5
    };
    6
    export async function main(stripe_con: Stripe, charge_id: string) {
    7
      const token = stripe_con["token"];
    8
      const stripe = Stripe(`${token}`, {
    9
        httpClient: Stripe.createFetchHttpClient(),
    10
      });
    11
    
    
    12
      const refund = await stripe.refunds.create({
    13
        charge: `${charge_id}`,
    14
      });
    15
    
    
    16
      return await refund.json();
    17
    }
    18
    
    
  • Submitted by hugo989 Bun
    Created 136 days ago
    1
    import StripeClient from "stripe@11";
    2
    
    
    3
    type Stripe = {
    4
      token: string;
    5
    };
    6
    export async function main(stripe_con: Stripe, charge_id: string) {
    7
      const token = stripe_con["token"];
    8
      const stripe = StripeClient(`${token}`, {
    9
        httpClient: StripeClient.createFetchHttpClient(),
    10
      });
    11
    
    
    12
      const refund = await stripe.refunds.create({
    13
        charge: `${charge_id}`,
    14
      });
    15
    
    
    16
      return await refund.json();
    17
    }
    18