2

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 = '[email protected]'; // 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;
                        }



            }

        }
2
  • 2
    What u mean by Current order? on which controller you want to get order
    – Amit Bera
    Commented Mar 24, 2017 at 4:33
  • I have created a payment gateway module I created a redirect in JS to redirect to my own custom controller in there I want to get the order id which was placed pending and then redirect it to the payment gateway
    – woshka
    Commented Mar 24, 2017 at 6:31

1 Answer 1

0

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 = '[email protected]'; // 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;
                        }



            }

        }
5
  • Please share u controller file
    – Amit Bera
    Commented Mar 24, 2017 at 13:41
  • I have updated the question with my controller
    – woshka
    Commented Mar 24, 2017 at 23:59
  • 1
    amita my order id is always one # less than the currently placed order why ? I used this $order->getId(); for getting orderid
    – woshka
    Commented Mar 28, 2017 at 4:48
  • Please give an example order id or give me screen shot of order id
    – Amit Bera
    Commented Mar 28, 2017 at 5:16
  • if the call to getLastRealOrderId is done before order placement it returns orderId-1, and if done after like in my case, it returns null. I tried different approaches : $orderId = $this->checkoutSession->getLastRealOrder()->getEntityId(); $orderId = $this->checkoutSession->getQuote()->getReservedOrderId(); but both return null, I don't know if the session changes after placement, or some other reason
    – medmek
    Commented Jul 17, 2018 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.