Modifies an existing checkout

Modifies an existing checkout

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
 * Modifies an existing checkout
7
 * Modifies an existing checkout
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  token: string,
13
  body: {
14
    checkout?: {
15
      shipping_address?: {
16
        address1?: string;
17
        city?: string;
18
        country_code?: string;
19
        first_name?: string;
20
        last_name?: string;
21
        phone?: string;
22
        province_code?: string;
23
        zip?: string;
24
        [k: string]: unknown;
25
      };
26
      token?: string;
27
      [k: string]: unknown;
28
    };
29
    [k: string]: unknown;
30
  }
31
) {
32
  const url = new URL(
33
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/checkouts/${token}.json`
34
  );
35

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