I am looking for a way to programmatically refund part of an order within a custom module. I have been following this blog post (https://www.rakeshjesadiya.com/create-creditmemo-for-order-item-in-magento-2/) but there is no creditmemo id being generated when the load() method id called on line 41. I'm not entirely sure this is the correct way to go about it any more either, as this blog post dates back to AUGUST 9, 2018, so I was wondering if anyone knows of a better way to approach this at all?

Cheers guys


So I'm using the Magento\Sales\Api\RefundOrderInterface class to call the execute() method which requires the 2nd parameter to be an array of items of type Magento\Sales\Api\Data\CreditmemoItemCreationInterface.

I've added the Magento\Sales\Model\Order\Creditmemo\ItemCreationFactory class and I'm setting my items in a loop as follows:

$creditmemoItems = []; 

foreach ($order->getAllVisibleItems() as $item) {
    $creditmemoItem = $this->creditmemoItemFactory->create();
    $creditmemoItems[] = $creditmemoItem;

After that, I call the execute method for the refundOrder class:

    $creditmemoItems ,

the operation fails and throws the following error in the logs

Exception #0 (Magento\Sales\Exception\DocumentValidationException): Creditmemo Document Validation Error(s): The order does not allow a creditmemo to be created. The most money available to refund is 0.

1 Answer 1


I would use the API methods for the generation of the refund. You can basically call the API class method responsible for the endpoint https://magento.redoc.ly/2.4.2-admin/tag/orderorderIdrefund#operation/salesRefundOrderV1ExecutePost for this purpose.

  • thank you for your reply :) I will give this a go now thank you
    – SNAFU
    May 21, 2021 at 11:58
  • Hi Diana, can you please check my updated question please? I think I'm nearly there but not sure what's causing the error I mentioned.
    – SNAFU
    May 21, 2021 at 12:29
  • 1
    Argh, I figured it out. I've been creating a few test orders whilst I've been working on this, and I completely forgot to invoice the latest order I was testing with after your suggestion :D so the error in fact was correct, the total to refund was 0 because there were no invoiced totals to begin with. Thanks again for your help with this Diana.
    – SNAFU
    May 21, 2021 at 13:35
  • you are most welcome! glad you sorted this out :)
    – Diana
    May 21, 2021 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.