在Magento中添加一个自己的支付模块----第三部分

在前两部分解决了在后端配置自定义支付方式,在确认订单时显示提交方式;

  • app/code/local/Envato/Custompaymentmethod/Helper/Data.php:这是帮助文件,用来提供实体方法;
  • app/code/local/Envato/Custompaymentmethod/controllers/PaymentController.php:这是控制器文件,为支付网关页补充必要的执行文件;
  • app/design/frontend/base/default/template/custompaymentmethod/redirect.phtml: 这是支付网关页面的模板文件;

app/code/local/Envato/Custompaymentmethod/Helper/Data.php

<?php

// app/code/local/Envato/Custompaymentmethod/Helper/Data.php
class Envato_Custompaymentmethod_Helper_Data extends Mage_Core_Helper_Abstract
{
  function getPaymentGatewayUrl()
  {
    return Mage::getUrl('custompaymentmethod/payment/gateway', array('_secure' => false));
  }
}

说明:在帮助文件中,定义一个方法来返回自定义支付方式的返回地址;

app/code/local/Envato/Custompaymentmethod/controllers/PaymentController.php

<?php

// app/code/local/Envato/Custompaymentmethod/controllers/PaymentController.php
class Envato_Custompaymentmethod_PaymentController extends Mage_Core_Controller_Front_Action
{
  public function gatewayAction()
  {
    if ($this->getRequest()->get("orderId"))
    {
      $arr_querystring = array(
        'flag' => 1,
        'orderId' => $this->getRequest()->get("orderId")
      );
       
      Mage_Core_Controller_Varien_Action::_redirect('custompaymentmethod/payment/response', array('_secure' => false, '_query'=> $arr_querystring));
    }
  }
  public function redirectAction()
  {
    $this->loadLayout();
    $block = $this->getLayout()->createBlock('Mage_Core_Block_Template','custompaymentmethod',array('template' => 'custompaymentmethod/redirect.phtml'));
    $this->getLayout()->getBlock('content')->append($block);
    $this->renderLayout();
  }
  public function responseAction()
  {
    if ($this->getRequest()->get("flag") == "1" && $this->getRequest()->get("orderId"))
    {
      $orderId = $this->getRequest()->get("orderId");
      $order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
      $order->setState(Mage_Sales_Model_Order::STATE_PAYMENT_REVIEW, true, 'Payment Success.');
      $order->save();
       
      Mage::getSingleton('checkout/session')->unsQuoteId();
      Mage_Core_Controller_Varien_Action::_redirect('checkout/onepage/success', array('_secure'=> false));
    }
    else
    {
      Mage_Core_Controller_Varien_Action::_redirect('checkout/onepage/error', array('_secure'=> false));
    }
  }
}

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

<?php

$order = new Mage_Sales_Model_Order();
$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order->loadByIncrementId($orderId);
?>
<h2><?php echo $this->__('Demo Payment Gateway') ?></h2>
<p>It's a demo page acting as a payment gateway interface! So don't worry you won't get charged :-)</p>
<form name="custompaymentmethod" method="post" action="<?php echo Mage::helper('custompaymentmethod')->getPaymentGatewayUrl(); ?>">
  <input type="hidden" name="orderId" value="<?php echo $orderId; ?>">
  <input type="submit" value="<?php echo $this->__('Make Payment') ?>" />
</form>

 

posted @ 2017-08-23 14:42  刘冬青  阅读(438)  评论(0)    收藏  举报