Magento 2 : Error “If the invoice was created offline, try creating an offline credit memo”

Magento 2 : Error “If the invoice was created offline, try creating an offline credit memo”

I am getting this error while online refunding credit memo using a script.
Below is the custom method i am using,

Where $order is the Order in which i am refunding shipment amount.
$csvRowCounter is the row number of csv (exg 1 or 2)

/**
 * Refund shipment & create credit memo.
 */
public function _processShipmentRefund($order, $csvRowCounter)
{
    $invoice          = null;
    $invoiceColl      = $order->getInvoiceCollection();
    $response         = array();
    $data             = array();
    $qtys             = array();
    $orderIncrementId = $order->getIncrementId();
    foreach ($order->getAllItems() as $mOi) {
        $mOiId    = (string) $mOi->getId();
        $parentId = (string) $mOi->getParentItemId();
        if (!empty($parentId)) {
            $qtys[$parentId] = "0";
        } else {
            $qtys[$mOiId] = "0";
        }
    }
    // Not processing refunds for items, only refunding shipping
    $data['qtys']            = $qtys;
    $data['shipping_amount'] = $order->getBaseShippingAmount();
    $creditmemo              = $this->creditmemoFactory->createByOrder($order, $data);
    try {
        if ($creditmemo) {
            
            $creditmemoManagement = $this->_objectManagerInterface->create(
                MagentoSalesApiCreditmemoManagementInterface::class
            );

            foreach ($invoiceColl as $invoice) {
                $invoiceId = $invoice->getId();
            }
            $invoice = $this->invoiceRepository->get($invoiceId);
            $creditmemo->setInvoice($invoice);
            $doOffline = false;
            $captureTxn = $this->transactionRepository->getByTransactionId(
                    $invoice->getTransactionId(),
                    $creditmemo->getOrder()->getId(),
                    $creditmemo->getOrder()->getId()
                );
        if ($captureTxn) {
            $creditmemoManagement->refund($creditmemo, $doOffline);
            $creditmemo = $this->creditmemoRepository->save($creditmemo)
        }
    } catch (MagentoFrameworkExceptionNoSuchEntityException $ex) {
        $response['notice'][] = "There is something wrong while create credit memo for order #" . $orderIncrementId;
    }
    return $response;
}

When i run this above script.
I get “If the invoice was created offline, try creating an offline credit memo” error. (FYI – This error located in https://github.com/pepe1518/magento2/blob/89adbabc2a295c9b6f88768d541fd2858f59aae5/vendor/magento/module-sales/Model/Order/Payment.php#L659 )

I check for “$captureTxn” which checks for the “transaction id” from invoice. Then only i let the refund method to execute. I am not sure when the invoice created online. (capture online) Then also why it passes into that condition.

Here is the order image
Magento 2 : Error “If the invoice was created offline, try creating an offline credit memo”

Due to that i had manually created refund to shipment & it worked but not with script.

Please share your thoughts on this, why its happening like this.


Go to Source of this post
Author Of this post: devhs
Title Of post: Magento 2 : Error “If the invoice was created offline, try creating an offline credit memo”
Author Link: {authorlink}