skip to Main Content

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 MagentoPGMBlock;

use MagentoAdminNotificationModelInbox;
use MagentoCheckoutModelSession;
use MagentoFrameworkAppObjectManager;
use MagentoFrameworkAppResponseHttp;
use MagentoFrameworkViewElementTemplate;
use MagentoFrameworkViewElementTemplateContext;
use MagentoSalesApiDataOrderAddressInterface;
use MagentoSalesModelOrderPaymentTransaction;
use MagentoSalesModelOrderPaymentTransactionBuilder as TransactionBuilder;
use MagentoSalesModelOrderFactory;
use MagentoStoreModelScopeInterface;
use MagentoPGMLoggerLogger;


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('MagentoFrameworkUrlInterface');
        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 ‘SalesModel’ 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


  1. 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.

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

    use MagentoSalesModelResourceModelOrderPaymentTransactionCollectionFactory; 
    protected $transactions;
    public function __constructor(CollectionFactory $transactions)
    {
        $this->transactions = $transactions;
    }
    

    In your method:

    $transactions = $this->transactions->create()->addOrderIdFilter($orderId);
    ...
    $transactions->setTransactionId("-1");`
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search