本文介绍了magento 将结帐付款重定向到第 3 方网关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实施我的新付款方式,它工作正常.但我的要求有点不同.我需要将用户重定向到支付网关页面.这就是我正在尝试实施的方式.

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.

当用户点击 Place Order my Namespace_Bank_Model_Payment >>授权方法被调用.我的网关说发送一个初始请求,根据给定网关的详细信息发送一个 URL &付款编号.在此 URL 上,用户必须重定向到客户实际付款的位置.我在 Controller success & 中有两个操作错误处理最终响应.

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.

因为,此代码在 ajax 请求中被调用,我无法将用户重定向到另一个网站.有人可以指导我如何完成它吗?

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?

这是我的代码.我已经实现了 getOrderPlaceRedirectUrl() 方法.

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

这是我的课::

<?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("
", ' ', $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("

 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;
  }

}

现在 getOrderPlaceRedirectUrl() 它被调用了.我可以看到 Mage::log 消息.但网址不存在.我的意思是 $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".

推荐答案

Magento 标准支持这种类型的支付网关,并直接支持将用户重定向到第三方站点进行支付.

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

在您的付款模型中,即扩展 Mage_Payment_Model_Method_Abstract 的付款模型,您需要实现该方法:

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';

通常您将用户重定向到您网站上的某个页面,例如 /mymodule/payment/redirect,然后在控制器的 action 中处理重定向逻辑.这使您的支付模型保持干净和无状态,同时允许您收到某种您现在正在被转移到支付网关"消息.

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.

在会话变量中保存决定重定向到哪里所需的一切,同样通常 Mage::getSingleton('checkout/session').

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

Magento 为 Paypal 标准提供了一个非常可靠的实现,即使很混乱.您可以在 app/code/core/Mage/Paypal/{Model/Standard.php,controllers/StandardController.php} 中查看他们的做法.

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}.

这篇关于magento 将结帐付款重定向到第 3 方网关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 02:17