Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order Refund API Issue #274

Open
tomwenk opened this issue Nov 29, 2021 · 0 comments
Open

Order Refund API Issue #274

tomwenk opened this issue Nov 29, 2021 · 0 comments

Comments

@tomwenk
Copy link

tomwenk commented Nov 29, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant