woocommerce/woocommerce-rest-api

Order Refund API Issue

tomwenk opened this issue · 0 comments

Dear Sir or Madam,
We are currently trying to integrate our CRM more deeply with WooCommerce. Now we want to enable our employees to make refunds via CRM. For this we read through the documentation and wrote the following code:
Unfortunately, every time we test it, we get the following error:

{
code: 'woocommerce_rest_cannot_create_order_refund',
message: 'Refund Error: You need to specify a refund amount.',
data: 500
}

Our JS code is the following:

const order_id = 44695
const username = 'XXX'
const password = 'XXX'
const https = require('https')
function request(obj, data) {
return new Promise((resolve, reject) => {
const req = https.request(obj, res => {
let data = ''
res.on('data', d => data += d)
res.on('end', () => {
if(res.statusCode >= 300) {
reject({
status: res.statusCode,
data: JSON.parse(data)
})
} else {
resolve(JSON.parse(data))
}
})
})
if(data)
req.write(new TextEncoder().encode(JSON.stringify(data)))
req.end()
})
}
request({
hostname: 'testhelden.com',
path: '/wp-json/wc/v3/orders/' + order_id,
method: 'GET',
headers: {
'Authorization': 'Basic ' + Buffer.from(username + ':' + password).toString('base64')
},
timeout: 1
}).then(res => {
request({
hostname: 'testhelden.com',
path: '/wp-json/wc/v3/orders/' + order_id + '/refunds',
method: 'POST',
headers: {
'Authorization': 'Basic ' + Buffer.from(username + ':' + password).toString('base64')
},
timeout: 1
}, {
amount: parseFloat(res.total),
line_items: res.line_items.map(li => ({
id: li.id,
quantity: li.quantity,
refund_total: parseFloat(li.subtotal),
refund_tax: li.taxes.map(t => ({
id: t.id,
refund_total: parseFloat(t.total)
}))
}))
}).then(console.log, console.log)
})

Can you please tell us how to make a refund via the WooCommerce API, where the status changes to "Refunded" and the amount is automatically refunded via our providers PayPal and Stripe?
Thank you and have a good start to the new week,
Tom