Magento redirect payment to a third-party gateway

Hi guys. I'm trying to implement my new payment method, its working mode. But my requirement is a little different. I need to redirect the user to the payment gateway page. This is how I try to implement

When the user clicks the "Place an order" button, the authorization method Namespace_Bank_Model_Payment → changes. My gateway. Send the initial request. Based on the data provided by the gateway, send the URL and payment ID. At this URL, the user must be redirected if the customer actually makes the payment. I have two actions in the success of the controller and an error for processing the final response.

Like, this code is called in an ajax request, I cannot redirect the user to another site. Can someone teach me how to do this?

Thank you very much in advance


Hi, thanks, Here is my code and I implemented the method getOrderPlaceRedirectUrl().

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 call causes. I see the message Mage :: log. but the url does not exist. I mean, the value $this->_redirectUrldoes not exist during the function call.

And one more thing: I do not plan to show the client any page, for example, "You are being redirected."

+3
source share
2 answers

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

, Mage_Payment_Model_Method_Abstract, :

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

, , /mymodule/payment/redirect, . , - " ".

, , , , Mage::getSingleton('checkout/session').

Magento , , Paypal. , app/code/core/Mage/Paypal/{Model/Standard.php,controllers/StandardController.php}.

+8

, , .

(. )

$this->_redirectUrl = $rurl;

Mage::getSingleton('customer/session')->setRedirectUrl($rurl);

& getOrderPlaceRedirectUrl()

public function getOrderPlaceRedirectUrl()
{
    Mage::Log('returning redirect url:: ' . Mage::getSingleton('customer/session')->getRedirectUrl() );
    return Mage::getSingleton('customer/session')->getRedirectUrl();        ;  
}

, , u

0

All Articles