Magento 1.8.0.0:添加重定向到新页面的自定义支付方法



我在本地使用magento 1.8.0.0。我成功地创建了一个自定义支付方式。结账时,该方法出现在"付款信息"处的付款方式列表中。问题是,当我选择它时,它会自动显示一个信用卡表单,这不是我想要的。我想要的是选择它,一旦我点击"继续"按钮,我得到重定向到另一个php页面包含我自己的表单。

OP.

对于那些想要重定向到网关并希望网关重定向回控制器的动作方法的人来说,事情是这样的:在文件app/code/local/Yourcompany/Yourmodule/Model/PaymentMethod.php中,执行如下操作:

class Yourcompany_Yourmodule_Model_PaymentMethod extends Mage_Payment_Model_Method_Abstract{
protected $_code  = "yourmodule";
protected $_isGateway = true;
protected $_canAuthorize = true;
 protected function _getCheckout()  {
return Mage::getSingleton('checkout/session'); } 
public function getOrderPlaceRedirectUrl()  {  return Mage::getUrl(yourmodule/index/youraction', array('_secure' => true));  }}

return Mage::getUrl(yourmodule/index/youraction', array('_secure' => true));行中,"index"表示我的控制器php文件名为IndexController.php。你可以随心所欲地改名字。在文件app/code/local/Yourcompany/Yourmodule/controllers/IndexController.php中,您可以编写如下代码:

class Yourcompany_Yourmodule_IndexController extends Mage_Core_Controller_Front_Action{      
public function indexAction() { 
$this->loadLayout();
$block = $this->getLayout()->createBlock('Mage_Core_Block_Template','yourmodule',array('template' => 'yourmodule/redirect.phtml'));
$this->getLayout()->getBlock('content')->append($block);
$this->renderLayout(); }
/*In the response action, you may code like this*/
public function responseAction() {  
$status=$_REQUEST['param1'];
$orderNo=$_REQUEST['param2'];
if(somecondition)
{
/*update order status */    
$ordera = Mage::getModel('sales/order')->loadByIncrementId($orderNo);
$ordera->setState(Mage_Sales_Model_Order::STATE_PENDING_PAYMENT, true)->save(); 
$this->_redirect("checkout/onepage/success");   
}
else
{
$this->_redirect("checkout/cart/");
} 
} } 

indexAction()正在重定向到模板重定向。phtml文件。该文件将收集一些参数发送到网关(订单号、客户名称、总金额等)。你可以把html文件放在这里:

app/design/frontend/base/default/template/yourmodule/redirect.phtml

其内容可以编码如下:

<?php 
$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order = Mage::getSingleton('sales/order')->loadByIncrementId($orderId);
$order_amount=$order->getGrandTotal();
$customerData = Mage::getSingleton('customer/session')->getCustomer();
$customerAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultBilling(); //oder getDefaultShipping
$address = Mage::getModel('customer/address')->load($customerAddressId);
$customer_name=$address->getFirstname().' '.$address->getLastname();
$customer_email=$customerData->getEmail();
?>
<form name="myjs" method="post" action="http://yourgatewayaddreshere">
<input type="hidden" name="customername" value="<?php echo $customer_name; ?>">
<input type="hidden" name="customermail" value="<?php echo $customer_email; ?>">
<input type="hidden" name="TotalMoney" value="<?php echo $order_amount; ?>">
</form>
<script type="text/javascript">
document.myjs.submit();
</script>

最新更新