Creates a refund

Caution For multi-currency orders, the currency property is required whenever the amount property is provided. For more information, see Migrating to support multiple currencies. Creates a refund. Use the calculate endpoint to produce the transactions to submit. Note When you use this endpoint with a Partner development store or a trial store, you can create only five refunds per minute.

Script shopify Verified

by hugo697 ยท 11/8/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 396 days ago
1
type Shopify = {
2
  token: string;
3
  store_name: string;
4
};
5
/**
6
 * Creates a refund
7
 *  Caution For multi-currency orders, the currency property is required whenever the amount property is provided. For more information, see Migrating to support multiple currencies.  Creates a refund. Use the calculate endpoint to produce the transactions to submit.   Note When you use this endpoint with a Partner development store or a trial store, you can create only five refunds per minute.
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  order_id: string,
13
  body: {
14
    refund?: {
15
      currency?: string;
16
      shipping?: { amount?: number; [k: string]: unknown };
17
      transactions?: {
18
        amount?: number;
19
        gateway?: string;
20
        kind?: string;
21
        parent_id?: number;
22
        [k: string]: unknown;
23
      }[];
24
      [k: string]: unknown;
25
    };
26
    [k: string]: unknown;
27
  }
28
) {
29
  const url = new URL(
30
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/orders/${order_id}/refunds.json`
31
  );
32

33
  const response = await fetch(url, {
34
    method: "POST",
35
    headers: {
36
      "Content-Type": "application/json",
37
      "X-Shopify-Access-Token": auth.token,
38
    },
39
    body: JSON.stringify(body),
40
  });
41
  if (!response.ok) {
42
    const text = await response.text();
43
    throw new Error(`${response.status} ${text}`);
44
  }
45
  return await response.json();
46
}
47