Magento 2 orderFactory not found in vendor directory

Viewed 735

I am using Magento 2.3.4 inside a docker container for a payment gateway extension. First things first, here is the affected code:

<?php

namespace Magento\PGM\Block;

use Magento\AdminNotification\Model\Inbox;
use Magento\Checkout\Model\Session;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Response\Http;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Sales\Api\Data\OrderAddressInterface;
use Magento\Sales\Model\Order\Payment\Transaction;
use Magento\Sales\Model\Order\Payment\Transaction\Builder as TransactionBuilder;
use Magento\Sales\Model\OrderFactory;
use Magento\Store\Model\ScopeInterface;
use Magento\PGM\Logger\Logger;


class Main extends Template
{
    protected $_objectmanager;
    protected $checkoutSession;
    protected $urlBuilder;
    protected $response;
    protected $config;
    protected $messageManager;
    protected $transactionBuilder;
    protected $inbox;
    private $logger;
    private $orderFactory;

    public function __construct(Context $context, Session $checkoutSession, OrderFactory $orderFactory = null, Logger $logger, Http $response, TransactionBuilder $tb, Inbox $inbox)
    {
        $this->checkoutSession = $checkoutSession;
        $this->orderFactory = $orderFactory ?: ObjectManager::getInstance()->get(OrderFactory::class);
        $this->response = $response;
        $this->config = $context->getScopeConfig();
        $this->transactionBuilder = $tb;
        $this->logger = $logger;
        $this->inbox = $inbox;

        $this->urlBuilder = ObjectManager::getInstance()
            ->get('Magento\Framework\UrlInterface');
        parent::__construct($context);
    }

    public function getParentId()
    {
        return $this->getData(OrderAddressInterface::PARENT_ID);
    }

    protected function _prepareLayout()
    {
        $method_data = array();
        $order = $this->orderFactory->create()->load($this->getParentId());
        if ($order) {
            $payment = $order->getPayment();
            // The error is thrown here (" Call to a member function setTransactionId() on null")
            $payment->setTransactionId("-1");
            ...
            $payment->save();
            $order->save();

            ...
    }

    private function setApiData($order, $testmode, $instance)
    {
       ...
    }
}

I am getting this error:

Call to a member function setTransactionId() on null

I think that this is just a symptom though. The order object is not created, my IDE marks the $order->getPayment() method as not found at all.

The code itself should not be the problem, but the folder 'Sales\Model' does not contain an orderFactory.php file. Is the file missing or deprecated? Several modules use this file and create orders like this, for example the Paypal PGM, and use the OrderFactory.php file.

2 Answers

As i know The Factory class name is the name of Model class and append with the Factory word. So for our example, we will have TopicFactory class. You must not create this class. Magento will create it for you. Whenever Magento’s object manager encounters a class name that ends in the word ‘Factory’, it will automatically generate the Factory class in the var/generation folder if the class does not already exist. You will see the factory class in

ROOT/generated/code/<vendor_name>/<module_name>/Model/OrderFactory.php

So the first step you should go to the folder Generation to see the class is there or NOT.

If it's not there, i think you're are facing permission issue , magento cant generate (can't create file or folder) the Factory Class in Generation folder.

Hi orderFactory does not have payment in DB, so you cannot use this to get payment. You can try this:

use Magento\Sales\Model\ResourceModel\Order\Payment\Transaction\CollectionFactory; 
protected $transactions;
public function __constructor(CollectionFactory $transactions)
{
    $this->transactions = $transactions;
}

In your method:

$transactions = $this->transactions->create()->addOrderIdFilter($orderId);
...
$transactions->setTransactionId("-1");`
Related