Hi @vaibhav9017
From your provided information, I understand that you’re encountering a problem when trying to automatically refund an order created via the REST API. The error message you’re seeing typically indicates that the payment gateway used for the order does not support automatic refunds.
However, it’s interesting that you’re able to manually refund the same order and also successfully apply refunds using the Refund API on orders not created via the REST API. This suggests that the issue might be related to how the order is created via the REST API.
I’d recommend checking the payment method used when creating the order via the API. Ensure that it’s a method that supports automatic refunds. Some gateways don’t support this feature, which could be causing the error.
Regarding the two cases you mentioned:
- To create an order and apply a refund on it, you should first create the order using the Create an Order endpoint (/wp-json/wc/v3/orders). After the order is created, you can then use the Create a Refund endpoint (/wp-json/wc/v3/orders/<id>/refunds) to apply a refund to the order.
- To create a partial order using partial payment, you would need to create the order with a status of ‘pending’ or ‘on-hold’. Once the full payment is ready to be processed, you can then update the order status to ‘processing’ or ‘completed’, depending on your workflow. This can be done using the Update an Order endpoint (/wp-json/wc/v3/orders/).
Further information on WooCommerce REST API can be found here: https://woocommerce.github.io/woocommerce-rest-api-docs/#introduction
I hope the above information helps! If you have any further questions or need additional clarification, please don’t hesitate to ask.
Thanks!
Thread Starter
Vaibhav
(@vaibhav9017)
Hello Shameem (woo-hc);
Thank you for your valuable inputs. I am using same order API of which url you shared, still causing an issue of refund on same order. Will look more in details and post if found anything. Below is sample code of order creation, please have a look and suggest if have any thing suspected in it.
const orderData = {
payment_method: 'bacs', // Payment method
payment_method_title: 'Direct Bank Transfer', // Payment method title
set_paid: true, // Set to true to mark the order as paid
billing: {
first_name: 'Vaibhav',
last_name: 'Bhutkar',
address_1: '123 Main Street',
city: 'Pune',
state: 'Maharashtra',
postcode: '411028',
country: 'India',
email: '',
phone: '123-456-7890',
},
shipping: {
first_name: 'Vaibhav',
last_name: 'Bhutkar',
address_1: '456 Shipping Street',
city: 'Pune',
state: 'Maharashtra',
postcode: '411028',
country: 'India',
},
line_items: [
{
product_id: 78011, // Replace with the actual product ID
quantity: 1,
},
// Add more line items as needed
],
};
const response = await WooCommerce.post("orders", orderData);
if(response){
res.status(200).json(response.data);
//return response.data;
}
Able to place order successfully, however if aply reund on same getting the above error.
Thank you,
Vaibhav Bhutkar
Hi @vaibhav9017
From the code snippet you’ve shared, I see that you’re creating an order with the API correctly. However, the refund process is not included in this code.
To create a refund, you will need to use the Create a Refund API endpoint. The URL should look something like this: json/wc/v3/orders/<id>/refunds where <id> is the ID of the order you want to refund.
The payload for the refund API should look like this:
{
"amount": "10",
"reason": "Optional refund reason"
}
You can replace "10" with the amount you want to refund, and "Optional refund reason" with a brief description of why the refund is being made.
Here’s how you might implement this in your code:
const refundData = {
amount: '10', // Replace with the amount to be refunded
reason: 'Optional refund reason' // Replace with the refund reason
};
const refundResponse = await WooCommerce.post(orders/${response.data.id}/refunds, refundData);
if (refundResponse) {
res.status(200).json(refundResponse.data);
}
Please replace the placeholders with the actual values you want to use. This should create a refund for the order.
If you’re still encountering issues after trying this, Please note that we can’t provide support for code customization as per our support policy. However, if you have any further questions on development or custom coding, don’t hesitate to reach out to some of our great resources available for support. Our WooCommerce community is brimming with skilled open-source developers who are active on the following channels:
I hope this helps! If you have any other questions, feel free to ask.
Thanks!
Thread Starter
Vaibhav
(@vaibhav9017)
Hello Shameem (woo-hc) ,
Thank you for your reply. I am using the same code for refund, what you shared. Below is the sample of it.
//To get order id fromm request
const orderId: any = req.body?.OrderId;
const refundData = {
amount: '1.00', // The amount to be refunded
reason: 'Customer requested a refund', // The reason for the refund
};
//Verify order ID before calling Refund API
if(orderId) {
const response = await WooCommerce.post(orders/${orderId}/refunds, refundData);
if(response){
res.status(200).json(response.data);
//return response.data;
}
}else {
res.status(200).json({ message: Order Id not passed, please verify});
}
}
Also, I am not looking for code customization, I am checking is any one having same issue in past or not and that will be helpfull.
Thank you for your valuable inputs and throught.
Thank You,
Hello @vaibhav9017,
Thank you for the clarification!
I’m getting the same error with BACS and COD orders. Seems like the /wp-json/wc/v3/orders only works with a paid order using a real payment gateway like Stripe, WooPayments, etc.
I opened a new GitHub report about this as I’m not sure if this is done by design or a bug.
We don’t have an exact timeline for when the team will investigate or resolve the issue, as bug reports are prioritized based on a few criteria.
I suggest you subscribe to the report to receive updates. :)
Cheers!