//native
/**
* Cancel Order
* Cancel an order, optionally refunding it to the original payment methods and restocking its items. Cancellation runs asynchronously: the result holds a job id and jobResult (status, errors); fetch the order later to confirm cancelledAt.
*/
export async function main(
auth: RT.Shopify,
order_id: string,
reason: "CUSTOMER" | "DECLINED" | "FRAUD" | "INVENTORY" | "STAFF" | "OTHER",
refund: boolean | undefined,
restock: boolean | undefined,
notify_customer: boolean | undefined,
staff_note: string | undefined
) {
const orderId = order_id.startsWith("gid://")
? order_id
: `gid://shopify/Order/${order_id}`
const response = await fetch(
`https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
{
method: "POST",
headers: {
"X-Shopify-Access-Token": auth.token,
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify({
query: `mutation ($orderId: ID!, $reason: OrderCancelReason!, $restock: Boolean!, $refundMethod: OrderCancelRefundMethodInput, $notifyCustomer: Boolean, $staffNote: String) { orderCancel(orderId: $orderId, reason: $reason, restock: $restock, refundMethod: $refundMethod, notifyCustomer: $notifyCustomer, staffNote: $staffNote) { job { id done } jobResult { id done status errors { code field message } } orderCancelUserErrors { code field message } } }`,
variables: {
orderId,
reason,
restock: restock || false,
refundMethod: { originalPaymentMethodsRefund: refund || false },
notifyCustomer: notify_customer || false,
staffNote: staff_note || undefined,
},
}),
}
)
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
const { data, errors } = await response.json()
if (errors) {
throw new Error(JSON.stringify(errors))
}
if (data.orderCancel.orderCancelUserErrors.length) {
throw new Error(JSON.stringify(data.orderCancel.orderCancelUserErrors))
}
return data.orderCancel
}
Submitted by hugo989 3 hours ago