Magento – Magento2 How To Get Order Id from orderFactory in custom Controller in the own custom payment gateway module

magento2orders

I wanted to know how to get orderid of the current order which have now been redirected to my own custom controller in my own custom payment gateway module in Magento2 using orderFactory?

        namespace Vendor\CustomPayment\Controller\Redirect;

        use Magento\Framework\Controller\ResultFactory;
        use Magento\Framework\Webapi\Soap\ClientFactory;
        use Magento\Payment\Gateway\Http\ClientInterface;
        use Magento\Payment\Gateway\Http\ConverterInterface;
        use Magento\Payment\Gateway\Http\TransferInterface;
        use Magento\Payment\Model\Method\Logger;
        use Magento\Checkout\Model\Cart;
        use Magento\Checkout\Model\Session;
        use Magento\Sales\Model\OrderFactory;

        //use Zend\Soap\Client;
        //use Zend\Soap;
        use SoapClient;

        class Index extends \Magento\Framework\App\Action\Action 
        {


            protected $resultPageFactory;
            protected $orderFactory;

            protected $client;
            protected $MerchantID;
            protected $Amount;
            protected $Description;
            protected $Email;
            protected $Mobile;
            protected $CallbackURL;

        // URL also can be ir.CustomPayment.com or de.CustomPayment.com
        //    private $client = new SoapClient('https://www.CustomPayment.com/pg/services/WebGate/wsdl', ['encoding' => 'UTF-8']);


            /**
             * Constructor
             *
             * @param \Magento\Framework\App\Action\Context  $context
             * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
             */
            public function __construct(
                \Magento\Framework\App\Action\Context $context,
                \Magento\Framework\View\Result\PageFactory $resultPageFactory,
                    \Magento\Sales\Model\OrderFactory $orderFactory
            ) {

                $this->resultPageFactory = $resultPageFactory;
                $this->orderFactory = $orderFactory;
                parent::__construct($context);
        //        $this->_order = $_order; // <-- new line





            }

            /**
             * Execute view action
             *
             * @return \Magento\Framework\Controller\ResultInterface
             */
            public function execute()
            {

                $orderId = $this->getRequest()->getParam('order_id');
                $order = $this->orderFactory->create();
                $order->load($orderId);
                $customerId = $order->getCustomerId();
                echo $customerId;
                exit();
                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            //    $order_id = $objectManager
                $cart = $objectManager->get('\Magento\Checkout\Model\Cart'); 
                $grandTotal = $cart->getQuote()->getGrandTotal();

        //    $orderModel = $this->_objectManager->create('\Magento\Sales\Model\Order'); 
        //    $order = $orderModel->load($orderId);
        //    $currentState = $order->getState();
        //    $currentStatus = $order->getStatus();
        //
        //    $save = false;
        //    if ($currentState !== $orderModel::STATE_NEW) {
        //        $order->setState($orderModel::STATE_NEW);
        //        $save = true;
        //    }
        //
        //    if ($currentStatus !== $orderModel::STATUS_FRAUD) {
        //        $order->setStatus($orderModel::STATUS_FRAUD);
        //        $save = true;
        //    }   
        //
        //    if ($save) { $order->save(); }

                $MerchantID = '456ad7f2-ae1c-11e6-bcba-000c295eb8fc';  //Required
                $Amount = $grandTotal; //Amount will be based on Toman  - Required
                $Description = 'توضیحات تراکنش تستی';  // Required
                $Email = 'UserEmail@Mail.Com'; // Optional
                $Mobile = '09123456789'; // Optional
                $CallbackURL = 'http://bist20.com/callback/Callback';  // Required
                $client = new SoapClient('https://www.CustomPayment.com/pg/services/WebGate/wsdl', ['encoding' => 'UTF-8']);

                $result = $client->PaymentRequest([
                            'MerchantID'     => $MerchantID,
                            'Amount'         => $Amount,
                            'Description'    => $Description,
                            'Email'          => $Email,
                            'Mobile'         => $Mobile,
                            'CallbackURL'    => $CallbackURL,
                        ]);

                        //Redirect to URL You can do it also by creating a form
                        if ($result->Status == 100) {
                            $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
                            $resultRedirect->setUrl("https://www.CustomPayment.com/pg/StartPay/".$result->Authority);
                            return $resultRedirect;
                        } else {
                            echo'ERR: '.$result->Status;
                        }



            }

        }

Best Answer

At your Controller __construct() function,

you should inject Check Session class \Magento\Checkout\Model\Session and from this class you will get Order Id

Then using Order factory class you can order object

    /**
     * @var \Magento\Checkout\Model\Session
     */
    protected $_checkoutSession;

    /**
     * @var \Magento\Sales\Model\OrderFactory
     */
    protected $_orderFactory;

  public function __construct(
          ..... 
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Sales\Model\OrderFactory $orderFactory,
         .....
    ) {
        $this->_checkoutSession = $checkoutSession;
        $this->_orderFactory = $orderFactory;
        .....
    }

    /**
     * Get order object
     *
     * @return \Magento\Sales\Model\Order
     */
    protected function getOrder()
    {
        return $this->_orderFactory->create()->loadByIncrementId(
            $this->_checkoutSession->getLastRealOrderId()
        );
    }

Update

<?php
  namespace Vendor\CustomPayment\Controller\Redirect;

        use Magento\Framework\Controller\ResultFactory;
        use Magento\Framework\Webapi\Soap\ClientFactory;
        use Magento\Payment\Gateway\Http\ClientInterface;
        use Magento\Payment\Gateway\Http\ConverterInterface;
        use Magento\Payment\Gateway\Http\TransferInterface;
        use Magento\Payment\Model\Method\Logger;
        use Magento\Checkout\Model\Cart;
        use Magento\Checkout\Model\Session;

        //use Zend\Soap\Client;
        //use Zend\Soap;
        use SoapClient;

        class Index extends \Magento\Framework\App\Action\Action 
        {


            protected $resultPageFactory;
            protected $orderFactory;

            protected $client;
            protected $MerchantID;
            protected $Amount;
            protected $Description;
            protected $Email;
            protected $Mobile;
            protected $CallbackURL;
            protected $_checkoutSession;

        // URL also can be ir.CustomPayment.com or de.CustomPayment.com
        //    private $client = new SoapClient('https://www.CustomPayment.com/pg/services/WebGate/wsdl', ['encoding' => 'UTF-8']);


            /**
             * Constructor
             *
             * @param \Magento\Framework\App\Action\Context  $context
             * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
             */
            public function __construct(
                \Magento\Framework\App\Action\Context $context,
                \Magento\Framework\View\Result\PageFactory $resultPageFactory,
                \Magento\Sales\Model\OrderFactory $orderFactory,
                Session $checkoutSession
            ) {
                $this->resultPageFactory = $resultPageFactory;
                $this->orderFactory = $orderFactory;
                $this->_checkoutSession = $checkoutSession;
                parent::__construct($context);
            }

            /**
             * Execute view action
             *
             * @return \Magento\Framework\Controller\ResultInterface
             */
            public function execute()
            {

                $orderId = $this->getRequest()->getParam('order_id');
                $orderIncrementId = $this->_checkoutSession->getLastRealOrderId();
                $order = $this->orderFactory->create();
                $order->loadByIncrementId($orderIncrementId);

                $customerId = $order->getCustomerId();

                $grandTotal = $order->getGrandTotal();


                $MerchantID = '456ad7f2-ae1c-11e6-bcba-000c295eb8fc';  //Required
                $Amount = $grandTotal; //Amount will be based on Toman  - Required
                $Description = 'توضیحات تراکنش تستی';  // Required
                $Email = 'UserEmail@Mail.Com'; // Optional
                $Mobile = '09123456789'; // Optional
                $CallbackURL = 'http://bist20.com/callback/Callback';  // Required
                $client = new SoapClient('https://www.CustomPayment.com/pg/services/WebGate/wsdl', ['encoding' => 'UTF-8']);

                $result = $client->PaymentRequest([
                            'MerchantID'     => $MerchantID,
                            'Amount'         => $Amount,
                            'Description'    => $Description,
                            'Email'          => $Email,
                            'Mobile'         => $Mobile,
                            'CallbackURL'    => $CallbackURL,
                        ]);

                        //Redirect to URL You can do it also by creating a form
                        if ($result->Status == 100) {
                            $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
                            $resultRedirect->setUrl("https://www.CustomPayment.com/pg/StartPay/".$result->Authority);
                            return $resultRedirect;
                        } else {
                            echo'ERR: '.$result->Status;
                        }



            }

        }
Related Topic