Creates a transaction for an order
One script reply has been approved by the moderators Verified

Caution For multi-currency orders, the currency property is required when creating refund and capture transactions. The value should be the presentment currency from the order. For more information, see Migrating to support multiple currencies. Creates a transaction for an order.

Created by hugo697 883 days ago
Submitted by hugo697 Typescript (fetch-only)
Verified 337 days ago
1
type Shopify = {
2
  token: string;
3
  store_name: string;
4
};
5
/**
6
 * Creates a transaction for an order
7
 *  Caution For multi-currency orders, the currency property is required when creating refund and capture transactions. The value should be the presentment currency from the order. For more information, see Migrating to support multiple currencies.  Creates a transaction for an order.
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  order_id: string,
13
  body: {
14
    transaction?: {
15
      amount?: string;
16
      currency?: string;
17
      kind?: string;
18
      parent_id?: number;
19
      [k: string]: unknown;
20
    };
21
    [k: string]: unknown;
22
  }
23
) {
24
  const url = new URL(
25
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/orders/${order_id}/transactions.json`
26
  );
27

28
  const response = await fetch(url, {
29
    method: "POST",
30
    headers: {
31
      "Content-Type": "application/json",
32
      "X-Shopify-Access-Token": auth.token,
33
    },
34
    body: JSON.stringify(body),
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