//native
/**
* Process Return
* Process the items of an open return: mark the given return line items ({ id, quantity }) as processed and optionally refund the customer. To refund, pass the order's paid transaction id (refund_transaction_id), the amount to send back to it and the order's currency_code; refund_shipping refunds the full shipping. Replaces the deprecated returnRefund. Needs the write_returns scope.
*/
export async function main(
auth: RT.Shopify,
return_id: string,
return_line_items: { id: string; quantity: number }[],
refund_transaction_id: string | undefined,
refund_amount: number | undefined,
currency_code: string | undefined,
refund_shipping: boolean | undefined,
notify_customer: boolean | undefined,
note: string | undefined
) {
if (refund_transaction_id && (refund_amount == null || !currency_code)) {
throw new Error(
"refund_amount and currency_code are required with refund_transaction_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 ($input: ReturnProcessInput!) { returnProcess(input: $input) { return { id name status closedAt returnLineItems(first: 50) { nodes { id quantity processedQuantity unprocessedQuantity refundedQuantity } } refunds(first: 10) { nodes { id createdAt totalRefundedSet { shopMoney { amount currencyCode } } } } } userErrors { field message code } } }`,
variables: {
input: {
returnId: return_id.startsWith("gid://")
? return_id
: `gid://shopify/Return/${return_id}`,
returnLineItems: return_line_items.map((li) => ({
id: li.id.startsWith("gid://")
? li.id
: `gid://shopify/ReturnLineItem/${li.id}`,
quantity: li.quantity,
})),
financialTransfer: refund_transaction_id
? {
issueRefund: {
orderTransactions: [
{
parentId: refund_transaction_id.startsWith("gid://")
? refund_transaction_id
: `gid://shopify/OrderTransaction/${refund_transaction_id}`,
transactionAmount: {
amount: refund_amount,
currencyCode: currency_code,
},
},
],
},
}
: undefined,
refundShipping: refund_shipping ? { fullRefund: true } : undefined,
notifyCustomer: notify_customer || undefined,
note: 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.returnProcess.userErrors.length) {
throw new Error(JSON.stringify(data.returnProcess.userErrors))
}
return data.returnProcess
}
Submitted by hugo989 3 hours ago