在Magento中添加一个自己的支付模块----第二部分
这里我们将在订单确认时显示我们的支付模块,将我们支付字段的信息和订单信息放在一块;
需要创建的文件:
app/code/local/Envato/Custompaymentmethod/Block/Form/Custompaymentmethod.php: 这个block文件为自定义支付方式指定一个模板文件,展示自定义支付方式的表格;app/design/frontend/base/default/template/custompaymentmethod/form/custompaymentmethod.phtml:这个是支付方式表格的模板文件;app/code/local/Envato/Custompaymentmethod/Model/Paymentmethod.php: 这个模型文件用来验证和保存自定义支付方式相关的字段信息;app/code/local/Envato/Custompaymentmethod/Block/Info/Custompaymentmethod.php: 在右边栏中给总结区设置必要信息。。。;It's a block file which will set up the necessary information for the summary section in the right sidebar.
创建 app/code/local/Envato/Custompaymentmethod/Block/Form/Custompaymentmethod.php
<?php
// app/code/local/Envato/Custompaymentmethod/Block/Form/Custompaymentmethod.phpclass Envato_Custompaymentmethod_Block_Form_Custompaymentmethod extends Mage_Payment_Block_Form{ protected function _construct() { parent::_construct(); $this->setTemplate('custompaymentmethod/form/custompaymentmethod.phtml'); }}说明:指定配置自定义支付模块字段的模板文件;
创建 app/design/frontend/base/default/template/custompaymentmethod/form/custompaymentmethod.phtml
<div class="form-list" id="payment_form_<?php echo $this->getMethodCode() ?>" style="display:none;">
<div> <label><?php echo $this->__('Custom Field One') ?>*</label> <span> <input type="text" title="<?php echo $this->__('Custom Field One') ?>" name="payment[custom_field_one]" value="<?php echo $this->htmlEscape($this->getInfoData('custom_field_one')) ?>" /> </span> </div> <div> <label><?php echo $this->__('Custom Field Two') ?>*</label> <span> <input type="text" title="<?php echo $this->__('Custom Field Two') ?>" name="payment[custom_field_two]" value="<?php echo $this->htmlEscape($this->getInfoData('custom_field_two')) ?>" /> </span> </div></div><div> <?php echo $this->getMethod()->getConfigData('message');?></div> 说明:之间创建过两个字段
custom_field_one 和custom_field_two,在这里展示这两个字段;创建 app/code/local/Envato/Custompaymentmethod/Model/Paymentmethod.php
<?php
// app/code/local/Envato/Custompaymentmethod/Model/Paymentmethod.phpclass Envato_Custompaymentmethod_Model_Paymentmethod extends Mage_Payment_Model_Method_Abstract { protected $_code = 'custompaymentmethod'; protected $_formBlockType = 'custompaymentmethod/form_custompaymentmethod'; protected $_infoBlockType = 'custompaymentmethod/info_custompaymentmethod'; public function assignData($data) { $info = $this->getInfoInstance(); if ($data->getCustomFieldOne()) { $info->setCustomFieldOne($data->getCustomFieldOne()); } if ($data->getCustomFieldTwo()) { $info->setCustomFieldTwo($data->getCustomFieldTwo()); } return $this; } public function validate() { parent::validate(); $info = $this->getInfoInstance(); if (!$info->getCustomFieldOne()) { $errorCode = 'invalid_data'; $errorMsg = $this->_getHelper()->__("CustomFieldOne is a required field.\n"); } if (!$info->getCustomFieldTwo()) { $errorCode = 'invalid_data'; $errorMsg .= $this->_getHelper()->__('CustomFieldTwo is a required field.'); } if ($errorMsg) { Mage::throwException($errorMsg); } return $this; } public function getOrderPlaceRedirectUrl() { return Mage::getUrl('custompaymentmethod/payment/redirect', array('_secure' => false)); }}说明:
1、$_code定义了自定义支付方式的唯一码;
2、$_formBlockType定义了一个块,当自定义支付方式选中时显示的块文件;
3、$_infoBlockType定义了一个块,右边栏显示的信息;
4、validate是对自定义支付相关字段的校验;
5、assignData将支付信息与订单信息绑定在一块;
6、getOrderPlaceRedirectUrl客户订单提交后的跳转地址;
创建 app/code/local/Envato/Custompaymentmethod/Block/Info/Custompaymentmethod.php
<?php
// app/code/local/Envato/Custompaymentmethod/Block/Info/Custompaymentmethod.phpclass Envato_Custompaymentmethod_Block_Info_Custompaymentmethod extends Mage_Payment_Block_Info{ protected function _prepareSpecificInformation($transport = null) { if (null !== $this->_paymentSpecificInformation) { return $this->_paymentSpecificInformation; } $data = array(); if ($this->getInfo()->getCustomFieldOne()) { $data[Mage::helper('payment')->__('Custom Field One')] = $this->getInfo()->getCustomFieldOne(); } if ($this->getInfo()->getCustomFieldTwo()) { $data[Mage::helper('payment')->__('Custom Field Two')] = $this->getInfo()->getCustomFieldTwo(); } $transport = parent::_prepareSpecificInformation($transport); return $transport->setData(array_merge($data, $transport->getData())); }}说明:这个文件可以在订单确认页面的右侧显示相关的总结信息;
我们已经完成了订单确认的过程;用户确认提交,Magento会根据getOrderPlaceRedirectUrl中的地址进行跳转;

浙公网安备 33010602011771号