cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2.15 Get order ID in a custom controller in custom payment Gateway module

SOLVED

Magento 2.15 Get order ID in a custom controller in custom payment Gateway module

I am developing a custom payment gateway module which redirects the users to external website for a payment

I have created a custom redirect in a controller but I wanted to get the order id before redirection so that after a callback I would know which order id to change state from pending to completed or paid status

 

I tried \Magento\Sales\Model\OrderFactory object in the controller that extends from \Magento\Framework\App\Action\Action  but I got an error 

Recoverable Error: Argument 3 passed to MaroonHost\CustomPayment\Controller\Redirect\Index::__construct() must be an instance of Magento\Sales\Model\OrderFactory

 

Please provide me instruction on how to get the order id or any thing related to order in my custom controller

 

Does it need a model that extends or implents som of magento Cart models and then use them in my controller?

 

Thanks 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Magento 2.15 Get order ID in a custom controller in custom payment Gateway module

hi,

  you can get order id from checkout session

  

 protected $_checkoutSession;
 
public function __construct(
\Magento\Checkout\Model\Session $checkoutSession,

) {
       
        $this->_checkoutSession = $checkoutSession;
      
    }

to get id use this code

        $order = $this->_checkoutSession->getLastRealOrder();
        $orderId=$order->getEntityId();
Find helpful ? Consider Giving Kudos to this post.
Problem solved? Click Accept as Solution!"
Qaisar Satti

View solution in original post

2 REPLIES 2

Re: Magento 2.15 Get order ID in a custom controller in custom payment Gateway module

hi,

  you can get order id from checkout session

  

 protected $_checkoutSession;
 
public function __construct(
\Magento\Checkout\Model\Session $checkoutSession,

) {
       
        $this->_checkoutSession = $checkoutSession;
      
    }

to get id use this code

        $order = $this->_checkoutSession->getLastRealOrder();
        $orderId=$order->getEntityId();
Find helpful ? Consider Giving Kudos to this post.
Problem solved? Click Accept as Solution!"
Qaisar Satti

Re: Magento 2.15 Get order ID in a custom controller in custom payment Gateway module

I could get the order id but it wont return the current order that was redirected to my custom controller instead in showed the previous order number 

the current is#89 which could get order #88

 

I this is also my javascript function which handles the order click button

let me know if this is some thing I have done correctly

 

continueToMellat: function (data, event)  {
                if (this.validate() && additionalValidators.validate()) {
                    this.isPlaceOrderActionAllowed(false);

                    this.getPlaceOrderDeferredObject()
                        .fail(
                            function () {
                                self.isPlaceOrderActionAllowed(true);
                            }
                        ).done(
                            function () {
                                self.afterPlaceOrder();

                                if (self.redirectAfterPlaceOrder) {
                                    redirectOnSuccessAction.execute();
                                }
                            }
                        );
                }
               
                    //update payment method information if additional data was changed
                    
                    $.mage.redirect(url.build('redirect/redirect'));
                    this.selectPaymentMethod();
                    setPaymentMethodAction(this.messageContainer);
                    return false;
               
            }