phpmagentopayment-gateway

magento redirect checkout payment to a 3rd party gateway


I am trying to implement my new payment method its working fine. But My requirement is little bit different. I need to redirect user to the payment gateway page. This is how I am trying to implement.

When user clicks on Place Order my Namespace_Bank_Model_Payment >> authorize method gets called. My gateway Says send an initial request, Based on details given gateway send a URL & Payment id. On this Url user must be redirected Where customer actually makes the payment. I have two actions in Controller success & error to handle the final response.

As, this code is getting called in an ajax request, I can't redirect user to another website. Can anybody guide me how to accomplish it?


Here is my code. I Have implemented getOrderPlaceRedirectUrl() method.

Here is my class::

<?php


class Namespace_Hdfc_Model_Payment extends Mage_Payment_Model_Method_Abstract
{
  protected $_isGateway = true;
  protected $_canAuthorize = true;
  protected $_canUseCheckout = true;

  protected $_code = "hdfc";


/**
     * Order instance
    */
  protected $_order;
  protected $_config;
  protected $_payment;
  protected $_redirectUrl;

/**
    * @return Mage_Checkout_Model_Session
   */
  protected function _getCheckout()
  {
    return Mage::getSingleton('checkout/session');
  }

/**
  * Return order instance loaded by increment id'
  *
  * @return Mage_Sales_Model_Order
  */
  protected function _getOrder()
  {   
    return $this->_order;
  }


/**
   * Return HDFC config instance
   *
   */
   public function getConfig()
   {
    if(empty($this->_config))
        $this->_config = Mage::getModel('hdfc/config');
    
    return $this->_config;
  }


  public function authorize(Varien_Object $payment, $amount)
  {
    if (empty($this->_order)) 
        $this->_order = $payment->getOrder();
    
    if (empty($this->_payment))
        $this->_payment = $payment;
    
    $orderId = $payment->getOrder()->getIncrementId();
    $order = $this->_getOrder();
    $billingAddress = $order->getBillingAddress();
    
    $tm = Mage::getModel('hdfc/hdfc');
    
    
    
    $qstr = $this->getQueryString();
    // adding amount
    $qstr .= '&amt='.$amount;
    //echo 'obj details:';
    //print_r(get_class_methods(get_class($billingAddress)));
    // adding UDFs
    $qstr .= '&udf1='.$order->getCustomerEmail();
    $qstr .= '&udf2='.str_replace(".", '', $billingAddress->getName() );
    $qstr .= '&udf3='.str_replace("\n", ' ', $billingAddress->getStreetFull());
    $qstr .= '&udf4='.$billingAddress->getCity();
    $qstr .= '&udf5='.$billingAddress->getCountry();
    $qstr .= '&trackid='.$orderId;
    
    // saving transaction into database;
    
    $tm->setOrderId($orderId);
    $tm->setAction(1);
    $tm->setAmount($amount);
    $tm->setTransactionAt( now() );
    $tm->setCustomerEmail($order->getCustomerEmail());
    $tm->setCustomerName($billingAddress->getName());
    $tm->setCustomerAddress($billingAddress->getStreetFull());
    $tm->setCustomerCity($billingAddress->getCity());
    $tm->setCustomerCountry($billingAddress->getCountry());
    $tm->setTempStatus('INITIAL REQUEST SENT');
    $tm->save();
    
    Mage::Log("\n\n queryString = $qstr");
    
    // posting to server
    
    try{
        $response = $this->_initiateRequest($qstr);
        // if response has error;
        if($er = strpos($response,"!ERROR!") )
        {
            $tm->setErrorDesc( $response );
            $tm->setTempStatus('TRANSACTION FAILED WHILE INITIAL REQUEST RESPONSE');
            $tm->save();
            $this->_getCheckout()->addError( $response );
            return false;
        }
            
        
        $i =  strpos($response,":");
        $paymentId = substr($response, 0, $i);
        $paymentPage = substr( $response, $i + 1);
        
        $tm->setPaymentId($paymentId);
        $tm->setPaymentPage($paymentPage);
        $tm->setTempStatus('REDIRECTING TO PAYMENT GATEWAY');
        $tm->save();
        
        // prepare url for redirection & redirect it to gateway
        
        $rurl = $paymentPage . '?PaymentID=' . $paymentId;
        
        Mage::Log("url to redicts:: $rurl");
        
        $this->_redirectUrl = $rurl;        // saving redirect rl in object
        
        // header("Location: $rurl");   // this is where I am trying to redirect as it is an ajax call so it won't work
        //exit;
    }
    catch (Exception $e) 
    {  
         Mage::throwException($e->getMessage());
    }
    
  }

  public function getOrderPlaceRedirectUrl()
  {
    Mage::Log('returning redirect url:: ' . $this->_redirectUrl );   // not in log
    return $this->_redirectUrl;
  }

}

Now getOrderPlaceRedirectUrl() its getting called. I can see the Mage::log message. but the url is not there. I mean the value of $this->_redirectUrl is not there at the time of function call.

And one more thing, I am not planning to show customer any page like "You are being redirected".


Solution

  • Magento supports this type of payment gateway as standard and directly supports redirecting the user to a third party site for payment.

    In your payment model, the one that extends Mage_Payment_Model_Method_Abstract, you'll need to implement the method:

    function getOrderPlaceRedirectUrl() {
        return 'http://www.where.should.we.pay.com/pay';
    

    Typically you redirect the user to a page on your site, /mymodule/payment/redirect for example, and then handle the redirection logic in the action of the controller. This keeps your payment model clean and stateless, while allowing you to some some kind of "You are now being transferred to the gateway for payment" message.

    Save everything you need to decide where to redirect to in a session variable, again typically Mage::getSingleton('checkout/session').

    Magento have a pretty solid, if messy, implementation of this for Paypal standard. You can checkout how they do it in app/code/core/Mage/Paypal/{Model/Standard.php,controllers/StandardController.php}.