0

Complete Draft Order

by
Published Nov 8, 2023

Turn a draft order into an order, marked as paid or payment pending.

Script shopify Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
/**
4
 * Complete Draft Order
5
 * Turn a draft order into an order. By default the order is marked as paid; set payment_pending to create it with "due on receipt" payment terms instead, so payment can be collected later (needs the write_payment_terms scope).
6
 */
7
export async function main(
8
  auth: RT.Shopify,
9
  draft_order_id: string,
10
  payment_pending: boolean | undefined
11
) {
12
  const id = draft_order_id.startsWith("gid://")
13
    ? draft_order_id
14
    : `gid://shopify/DraftOrder/${draft_order_id}`
15
  const endpoint = `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`
16
  const headers = {
17
    "X-Shopify-Access-Token": auth.token,
18
    "Content-Type": "application/json",
19
    Accept: "application/json",
20
  }
21
  const call = async (body: { query: string; variables?: object }) => {
22
    const response = await fetch(endpoint, {
23
      method: "POST",
24
      headers,
25
      body: JSON.stringify(body),
26
    })
27
    if (!response.ok) {
28
      throw new Error(`${response.status} ${await response.text()}`)
29
    }
30
    const { data, errors } = await response.json()
31
    if (errors) {
32
      throw new Error(JSON.stringify(errors))
33
    }
34
    return data
35
  }
36

37
  // Payment pending = the draft carries payment terms when it is completed.
38
  if (payment_pending) {
39
    const { paymentTermsTemplates } = await call({
40
      query: `query { paymentTermsTemplates(paymentTermsType: RECEIPT) { id } }`,
41
    })
42
    const { draftOrderUpdate } = await call({
43
      query: `mutation ($id: ID!, $input: DraftOrderInput!) { draftOrderUpdate(id: $id, input: $input) { draftOrder { id } userErrors { field message } } }`,
44
      variables: {
45
        id,
46
        input: {
47
          paymentTerms: { paymentTermsTemplateId: paymentTermsTemplates[0].id },
48
        },
49
      },
50
    })
51
    if (draftOrderUpdate.userErrors.length) {
52
      throw new Error(JSON.stringify(draftOrderUpdate.userErrors))
53
    }
54
  }
55

56
  const data = await call({
57
    query: `mutation ($id: ID!) { draftOrderComplete(id: $id) { draftOrder { id name status completedAt order { id legacyResourceId name displayFinancialStatus displayFulfillmentStatus totalPriceSet { shopMoney { amount currencyCode } } } } userErrors { field message } } }`,
58
    variables: { id },
59
  })
60

61
  if (data.draftOrderComplete.userErrors.length) {
62
    throw new Error(JSON.stringify(data.draftOrderComplete.userErrors))
63
  }
64

65
  return data.draftOrderComplete
66
}
67

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Shopify = {
    2
      token: string;
    3
      store_name: string;
    4
    };
    5
    /**
    6
     * Complete a draft order
    7
     * Completes a draft order.  Using the DraftOrder resource, you can create a draft order and transition it to an order.  The following flows are supported:   Create a draft order that calculates taxes and totals but accept payment from the customer outside of Shopify. Create a draft order and send an invoice to the customer.
    8
     */
    9
    export async function main(
    10
      auth: Shopify,
    11
      api_version: string = "2023-10",
    12
      draft_order_id: string
    13
    ) {
    14
      const url = new URL(
    15
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/draft_orders/${draft_order_id}/complete.json`
    16
      );
    17
    
    
    18
      const response = await fetch(url, {
    19
        method: "PUT",
    20
        headers: {
    21
          "X-Shopify-Access-Token": auth.token,
    22
        },
    23
        body: undefined,
    24
      });
    25
      if (!response.ok) {
    26
        const text = await response.text();
    27
        throw new Error(`${response.status} ${text}`);
    28
      }
    29
      return await response.json();
    30
    }
    31