shopify-api-node Create Refunds

Hi,

I’m using NodeJs and the shopify-api-node npm package. I first call the refund calculate endpoint to get the transaction and then use that in the params to call the refund create function, but it just returns the error below. I assume I’m missing a param, but I’ve looked at the documentation and it seems fine. I’ve attached images of the code and the calculation response.

HTTPError: Response code 400 (Bad Request)
at Request. (/Users/caelstewart/Desktop/test-JWT/AI/node_modules/got/dist/source/as-promise/index.js:118:42)
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
code: ‘ERR_NON_2XX_3XX_RESPONSE’,
timings: {
start: 1680890450679,
socket: 1680890450680,
lookup: 1680890450680,
connect: 1680890450690,
secureConnect: 1680890450704,
upload: 1680890450704,
response: 1680890450886,
end: 1680890450887,
error: undefined,
abort: undefined,
phases: {
wait: 1,
dns: 0,
tcp: 10,
tls: 14,
request: 0,
firstByte: 182,
download: 1,
total: 208
}
}

The code is:

const order = await shopify.order.get(“5290218357032”)

// Get the first line item ID
const lineItemId = order.line_items[0].id;
const quantity = order.line_items[0].quantity;
console.log(‘Line item ID:’, lineItemId);
console.log("quantity: ", quantity)

// specify the refund details
const refundDetails = {
“refund”: {
“currency”: “USD”,
“notify”: true,
“shipping”: {
“full_refund”: true // or number
},
“refund_line_items”: [
{
“line_item_id”: lineItemId, // replace with actual line item ID
“quantity”: quantity, // replace with actual quantity
“restock_type”: “no_restock”
},
// Add more objects for each item in the order
]
}
};

try {
let calculation = await shopify.refund.calculate(“5290218357032”, {
shipping: { full_refund: true },
refund_line_items: [
{
line_item_id: lineItemId,
quantity: quantity,
},
],
})
console.log(calculation)

let createRefund = await shopify.refund.create(“5290218357032”, {
currency: “USD”,
notify: true,
restock: false,
note: ‘Reason for refund’,
shipping: { full_refund: true },
refund_line_items: [
{
line_item_id: lineItemId,
quantity: quantity,
restock_type: ‘no_restock’,
},
],
transactions: calculation.transactions,
});
console.log(createRefund)

} catch (e) {
console.log(e)
}

1 Like

Hi @caelstewart ,

While the community may respond further, the official place for support with the Shopify Node JS library is the issues board for the public GitHub repository at https://github.com/Shopify/shopify-api-js/issues

Hope you have a great day

1 Like