I am creating a module for refunds and I have an error using the method

namespace CrediYa\CrediYa\Controller\CrediYa;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Request\Http;
use Magento\Sales\Model\Order\Creditmemo\CreationArgumentsFactory;

class Refund extends Action
    /** @var \Magento\Framework\App\Request\Http */
    protected $request;

    public function __construct(
        Context $context,
        Http $request,
        \Magento\Sales\Api\RefundInvoiceInterface $invoiceRefunder,
        CreationArgumentsFactory $creationArguments
        $this->request               = $request;
        $this->invoiceRefunder = $invoiceRefunder;
        $this->creationArguments = $creationArguments;


    public function execute()
        $items               = [];
        $order_id            = $this->request->getParam('order_id');
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $order             = $objectManager->create('Magento\Sales\Model\Order')->load($order_id);

        foreach ($order->getAllVisibleItems() as $item) 
            $items[$item->getId()] = array(
                'qty' => $item->getQtyOrdered()

            // $object['order_item_id'] = $item->getId();
            // $object['qty']           = $item->getQtyOrdered();

        $invoiceId = null;
        foreach ($order->getInvoiceCollection() as $invoice) 
            // this will always point to the latest invoice id
            $invoiceId = $invoice->getId();

            $invoiceId, // Invoice ID (NOTE: not increment ID)
            $items,             // Items array (default all)
            false       // false = offline / true = online


This is the code of my class

In other questions, they said that the $ items argument if it was sent empty, the method would take it as a total return of the order, but when doing this it gives me this error Exception #0 (Magento\Sales\Exception\DocumentValidationException): Creditmemo Document Validation Error(s): We can't create a credit memo for the invoice.

On the other hand, if I send something in $ items, this error no longer appears, but it gives another error, which originates from the magento vendor, specifically in this code area.

foreach ($items as $item) {
   $data['qtys'][$item->getOrderItemId()] = $item->getQty();

This is in the file CredimemoDocumentFactory.php, What I suppose is that I am sending the articles in the array wrong because it is trying to call a function on each item getOrderItemId() and getQty()

How should I send the data structure of my $ items array, to solve this error and can magento execute the necessary methods on each element?

1 Answer 1


In Magento, if you want to create a credit memo, then you need to use the below class:

\Magento\Sales\Api\RefundOrderInterface \Magento\Sales\Api\InvoiceRepositoryInterface \Magento\Sales\Api\Data

Example: https://pastebin.com/0tMAAhKX

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.