Calculates a refund
One script reply has been approved by the moderators Verified

Caution For multi-currency orders, the currency property is required whenever the amount property is provided.

Created by hugo697 883 days ago Picked 2 times
Submitted by hugo697 Typescript (fetch-only)
Verified 337 days ago
1
type Shopify = {
2
  token: string;
3
  store_name: string;
4
};
5
/**
6
 * Calculates a refund
7
 *  Caution For multi-currency orders, the currency property is required whenever the amount property is provided.
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  order_id: string,
13
  body: {
14
    refund?: {
15
      refund_line_items?: {
16
        line_item_id?: number;
17
        quantity?: number;
18
        restock_type?: string;
19
        [k: string]: unknown;
20
      }[];
21
      shipping?: { full_refund?: boolean; [k: string]: unknown };
22
      [k: string]: unknown;
23
    };
24
    [k: string]: unknown;
25
  }
26
) {
27
  const url = new URL(
28
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/orders/${order_id}/refunds/calculate.json`
29
  );
30

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