Retrieves a specific refund

Retrieves a specific refund.

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
 * Retrieves a specific refund
7
 * Retrieves a specific refund.
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  order_id: string,
13
  refund_id: string,
14
  fields: string | undefined,
15
  in_shop_currency: string | undefined
16
) {
17
  const url = new URL(
18
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/orders/${order_id}/refunds/${refund_id}.json`
19
  );
20
  for (const [k, v] of [
21
    ["fields", fields],
22
    ["in_shop_currency", in_shop_currency],
23
  ]) {
24
    if (v !== undefined && v !== "") {
25
      url.searchParams.append(k, v);
26
    }
27
  }
28
  const response = await fetch(url, {
29
    method: "GET",
30
    headers: {
31
      "X-Shopify-Access-Token": auth.token,
32
    },
33
    body: undefined,
34
  });
35
  if (!response.ok) {
36
    const text = await response.text();
37
    throw new Error(`${response.status} ${text}`);
38
  }
39
  return await response.json();
40
}
41