Search code examples
phpmagentomagento2magento2.4

Magento 2: Cannot getData from different controller


I have this class

class Api extends \Magento\Framework\Model\AbstractModel
{
 public function __construct(
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \MyModule\Payment\Helper\Data $helper
    ) {
        $this->messageManager = $messageManager;
        $this->scopeConfig = $scopeConfig;
        $this->storeManager = $storeManager;
        $this->helper = $helper;
        $this->contentType = $this->helper->getConfigData('content_type');
    }
    .
    .
    .
    function createOnlinePurchase($authToken, $lastOrder)
    {
        .
        .
        .
        //here I pass lastOrder's increment id to my payment gateway
        $lastOrder->setData('test','test data');
        $lastOrder->save();
        
        //make payment gateway api call, get payment url
        return url;
    }

}

this class is then used by a custom controller:

class Index extends \Magento\Framework\App\Action\Action
{
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \MyModule\Payment\Model\Api $moduleApi,
        \Magento\Checkout\Model\Session $session
    ) {
        parent::__construct($context);

        $this->moduleApi = $moduleApi;
        $this->session = $session;
    }
   
 public function execute() {
       $token = $this->moduleApi->requestAuthToken();

       if ($this->session->getLastRealOrder() && $token !== null) {
            $url = $this->moduleApi->createOnlinePurchase($token, $this->session->getLastRealOrder());

            if ($url !== null && substr($url, 0, 4) == 'http') {
               $this->session->clearStorage();     
               return $this->resultRedirectFactory->create()->setUrl($url);
            } 
            else {
                $this->messageManager->addError(__("Url invalid: ".$url));
                return $this->resultRedirectFactory->create()->setPath('checkout/onepage/failure');
            }
       }
}

on a SECOND custom controller Callback, which is triggered by my payment gateway, I retrieved the order object using $order = $this->getOrderFactory->create()->loadByIncrementId($incrementId)

where $this->getOrderFactory is an instance of \Magento\Sales\Model\OrderFactory I injected.

I got the increment id back from my payment gateway.

Somehow within this Callback class, when I use $order->getData('test'), I get nothing

My question is

Is there some core magento concept I'm missing here?

Or is there any other way I can retrieve this test data in Callback which only have the information of increment Id (because at the point of Callback, user have already left magento and come back)

It's weird to me beause I can edit and save the order from Callback but my extra data is not saved/associated with the order object itself

Thanks in advance!

UPDATE

I confirmed that I'm getting the same order object(row) by using the order id I get from my payment gateway as the one from session's Last Order

I called addStatusHistoryComment on lastOrder in Api class above and also called addStatusHistoryComment in my Callback class both calls are updating the same order in my admin dashboard

I have also confirmed calling getData('test') right after I set it gives me the data I want.

So I don't understand why getData doesn't work when called from Callback


Solution

  • You can't just add data to order object, every model is mapped automatically to DB tables columns, object will store temporarily the data and not error out but it will not persist in database. The reason why comments work, is because they have a place in database and on save and on load there is extra code that saves and adds it to the model.

    In order to persists new data in the order you need to add new order attribute or simply add a new column on sales order table. When saving, key much match exactly the name of the new column you created.