Retrieves a list of transactions

Retrieves a list of transactions. Transactions attached to multi-currency orders are in the presentment currency by default. To retrieve transactions in the shop currency, include the URL parameter in_shop_currency=true.

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 list of transactions
7
 * Retrieves a list of transactions. Transactions attached to multi-currency orders are in the presentment currency by default. To retrieve transactions in the shop currency, include the URL parameter in_shop_currency=true.
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  order_id: string,
13
  since_id: string | undefined,
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}/transactions.json`
19
  );
20
  for (const [k, v] of [
21
    ["since_id", since_id],
22
    ["fields", fields],
23
    ["in_shop_currency", in_shop_currency],
24
  ]) {
25
    if (v !== undefined && v !== "") {
26
      url.searchParams.append(k, v);
27
    }
28
  }
29
  const response = await fetch(url, {
30
    method: "GET",
31
    headers: {
32
      "X-Shopify-Access-Token": auth.token,
33
    },
34
    body: undefined,
35
  });
36
  if (!response.ok) {
37
    const text = await response.text();
38
    throw new Error(`${response.status} ${text}`);
39
  }
40
  return await response.json();
41
}
42