Skip to content

Magento 2.4: Mass payment capture from order grid issue

I am trying to capture payment in bulk by using custom mass action on the order grid. It works fine when I select only one order from the grid but it does not work wehn I select multiple order from the grid.

Error on bulk action:
enter image description here

My massaction controller:

class MassCapture extends MagentoSalesControllerAdminhtmlOrderAbstractMassAction
{
    /**
     * @var OrderManagementInterface
     */
    protected $orderManagement;

        /**
     * @var OrderStateResolverInterface
     */
    private $orderStateResolver;

    /**
     * @param Context $context
     * @param Filter $filter
     * @param CollectionFactory $collectionFactory
     * @param OrderManagementInterface $orderManagement
     */
    public function __construct(
        Context $context,
        Filter $filter,
        CollectionFactory $collectionFactory,
        OrderManagementInterface $orderManagement,
        OrderStateResolverInterface $orderStateResolver,
        OrderRepositoryInterface $orderRepository,
        OrderConfig $config
    ) {
        parent::__construct($context, $filter);
        $this->collectionFactory = $collectionFactory;
        $this->orderManagement = $orderManagement;
        $this->orderStateResolver = $orderStateResolver;
        $this->orderRepository = $orderRepository;
        $this->config =$config;
    }

    /**
     * Hold selected orders
     *
     * @param AbstractCollection $collection
     * @return MagentoBackendModelViewResultRedirect
     */
    protected function massAction(AbstractCollection $collection)
    {
        $model = $this->_objectManager->create('MagentoSalesModelOrder');
        foreach ($collection->getItems() as $order) {
            if (!$order->getEntityId()) {
                continue;
            }

            $loadedOrder = $model->load($order->getEntityId());
            $completeStatus = MagentoSalesModelOrder::STATE_COMPLETE;
            $cancelStatus = MagentoSalesModelOrder::STATE_CANCELED;
            $closedStatus = MagentoSalesModelOrder::STATE_CLOSED;
            $completeStatus = MagentoSalesModelOrder::STATE_COMPLETE;

            $countCapturePayment = 0;

            foreach ($loadedOrder->getInvoiceCollection() as $invoice)
            {
                if ($loadedOrder->getStatus() !== $completeStatus && $loadedOrder->getStatus() !== $cancelStatus && $loadedOrder->getStatus() !== $closedStatus) {
                    try {
                        $invoice->capture();
                        $loadedOrder->setState($completeStatus);
                        $loadedOrder->setStatus($completeStatus);
                        $invoice->setState(MagentoSalesModelOrderInvoice::STATE_PAID);
                        $this->orderRepository->save($loadedOrder);
                        
                        $countCapturePayment++;
                    } catch (Exception $e) {
                        $this->messageManager->addError(__('Error capturing payment for order %1', $order->getIncrementId()));
                    }
                }
            }

        }
        $countNonCapturedOrder = $collection->count() - $countCapturePayment;

        if ($countNonCapturedOrder && $countCapturePayment) {
            $this->messageManager->addError(__('%1 payment(s) were not captured.', $countNonCapturedOrder));
        } elseif ($countNonCapturedOrder) {
            $this->messageManager->addError(__('No payment(s) were captured.'));
        }

        if ($countCapturePayment) {
            $this->messageManager->addSuccess(__('You have captured payment of %1 order(s).', $countCapturePayment));
        }

        $resultRedirect = $this->resultRedirectFactory->create();
        $resultRedirect->setPath($this->getComponentRefererUrl());
        return $resultRedirect;
    }
}

Any help would be appreciated.