网银在线的motopay站内支付模块开发了有半年了,碰到了诸多问题,最近碰到的比较多,比较忙都没有记录,今天稍微有点时间整理一下其中一个比较常见的问题,估计站内信用卡支付都有可能碰到,包括 paypal,authorize等相关的信用卡站内支付都有可能存在这样的问题。
问题描述:
在站内输入信用卡后,点击无法进入到下一步。通过firebug,发现显然没有触发ajax request,所以根本无从到下一步,从而也定性应该是js方面的错误,会不会是事件绑定出了问题,不太可能。故意输入信用卡卡号发现事件的触发是正常的。最后还是通过firebug,得到了这点提示:
validateCreditCard is not defined. 显然是说这个函数没有定义。
奇怪了,那么多在用的商户都没碰到这个问题,我自己的服务器上也没有问题。会不会是版本的问题,这个被自己否定了,因为这个模块我在所有的magento版本上测试过。而且自己开发的模块几乎没有涉及到JS。即使有用到信用卡账户的验证也是使用MAGENTO带的。最后发现,我开发测试全部使用的default模板,而且显然通过排除也可以断定这个问题是由模板引起的。模板可能引起的问题有很多,但大部分局限于JS,CSS这块,因为模板的概念就是这些内容的集合。最后查明:由于有些模板开发时删除掉ccard.js 文件的调用,导致在验证信用卡账户时对这个函数调用出错并终止了JS的执行,自然也就没有发起 ajax request,所以就无法进行到下一步。模板删除掉ccard.js是因为并不是所有网站都会有使用这个文件。
解决:
<action method="addJs"><script>lib/ccard.js</script></action>
把这个添加到模板目录下的page.xml
当然不可能让用户第安装模块时都手工去添加一下,所以在模块中加入了模块的layout文件:
<?xml version="1.0"?>
<!--
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE_AFL.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category design
* @package base_default
* @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*
*
* @author George Zheng <xinhaozheng@gmail.com>
* @date 2010-12
*
*/
-->
<layout version="0.1.0">
<!--
Default layout, loads most of the pages
-->
<default>
<reference name="head">
<action method="addJs"><script>lib/ccard.js</script></action>
</reference>
</default>
</layout>
考虑到版本兼容性,我将这个文件motopay.xml 同时放在了base/default/layout 和default/default/layout目录下。