create order in payment return url magento

create order in payment return url

Guys before konwing the create order in payment return url, you should first know about the custom payment module creation in magento. To learn about custom payment module please refer this article here.
The most unsatisfied thing in magento was, order creation before payment method redirection, which means once you click the place order button in checkout page, magento first will create the order using the custom payment method then it will redirect the user. This redirection is done through the following function from payment model.

	public function getOrderPlaceRedirectUrl()
{
//when you click on place order you will be redirected on this url, if you don't want this action remove this method.
return Mage::getUrl('customcard/standard/redirect', array('_secure' => true));
}
 

Methods to redirect in custom payment

Most of us want, the order should create after the return from custom payment url, this can be done by following below two methods.

1. The standard method of redirection through the custom payment method. Instead of using the function getOrderPlacedRedirectUrl(){ you can use

 public function getCheckoutRedirectUrl() {
        return Mage::getUrl('customcard/standard/redirect', array('_secure' => true));
    }

By this method user can be redirected to the third party url after completing the payment method step. Ofcourse, the default paypal express checkout in the magento following this way. The second option is

2. app/code/core/Mage/Checkout/OnepageController.php

In saveOrder() function, you need to exchange this lines.

$this->getOnepage()->getQuote()->save();
        
        if (isset($redirectUrl)) {
            $result['redirect'] = $redirectUrl;
        }

Don’t modify anything in core file :), use standard controller rewrite method to do this.

By doing this you can redirect the user to third party url without creating the order. This method was not so good when you are creating extension or modules to use in other websites.

Create order from magento session

Obviously, with the above two methods the order will not be created in default. You need to create the order in payment method return url by using checkout session. Use this following code snippet in the custom payment success function.

	public function  successAction()
    	{
        	 $quote = Mage::getSingleton('checkout/session')->getQuote();
		$quoteid = Mage::getSingleton('checkout/session')->getQuoteId();
		$quote->assignCustomer(Mage::getSingleton('customer/session')->getCustomer());
		$quote->collectTotals()->getPayment()->setMethod('your_paymentmethod');
		$service = Mage::getModel('sales/service_quote', $quote);
		$service->submitAll();
		Mage::getSingleton('checkout/session')->setLastQuoteId($quote->getId())
		    ->setLastSuccessQuoteId($quote->getId())
		    ->clearHelperData();
		$order = $service->getOrder();
		if($order){
		     Mage::getSingleton('checkout/session')->setLastOrderId($order->getId())
		        ->setLastRealOrderId($order->getIncrementId());
		}
		$quote->setIsActive(false)->save();
		$this->_redirect('checkout/onepage/success');
	}

The above code creates the order from the return of third party url. Once the order created successfully, redirect the user to magento default checkout success page.