在某一时刻,你也许会在Magento中添加一个自定义的信用卡支付方式。请注意,在这篇Magento教程里,你将学会如何创建和验证一个新的信 用卡类型。另外,本文并不包括整个的创建支付模型过程。
根据这篇文章提到的需求,我们假设要创建的支付模型名字为NewModule,对应 app/etc/modules/Alwayly_NewModule.xml文件。我们将要创建Diners Club信用卡,并在服务器端和客户端 用Magento方式进行验证。
步骤
这里只需要几步来实现这件神奇的事。当然,每一步都至关重要。
- 在我们的config.xml文件中添加新的信用卡类型到global payment节点;
- 让Magento识别我们所用的支付模型
- 在我们的模型里实现服务器端的验证
- 实现客户端的验证
添加新的信用卡类型
让我们创建etc/config.xml,添加下面的代码来添加信用卡类型:
<config>
<modules>
<Alwayly_NewModule>
<version>1.0.0</version>
</Alwayly_NewModule>
</modules>
<global>
<payment>
<cc>
<types>
<DC>
<code>DC</code>
<name>Diners Club</name>
<order>60</order>
</DC>
</types>
</cc>
</payment>
</global>
</config>
到这里,我们只是添加了一个信用卡类型到Magento,并没有说明它所用的模型以及如何验证它。
创建支付模型
让我添加一些代码到etc/config.xml来配置我们的模型并告知Magento,代码如下:
<config>
...
<global>
...
<models>
<newmodule>
<class>Alwayly_NewModule_Model</class>
</newmodule>
</models>
...
</global>
<default>
<payment>
<newmodule>
<model>newmodule/payment</model>
</newmodule>
</payment>
</default>
</config>
现在,我们要创建Alwayly/NewModule/Model/Payment.php文件。包含扩展了Mage_Payment_Model_Method_Cc的类。在这个文件中,我们要将自己定义的正则表达式应用到信用卡号和CVV(信用卡验证码)。如果你想实现一个不同的信用卡类型,你就需要一个与之匹配的正则表达式。
class Alwayly_NewModule_Model_Payment extends Mage_Payment_Model_Method_Cc
{
protected $_code = 'alwayly_newmodule';
public function getVerificationRegEx()
{
return array_merge(parent::getVerificationRegEx(), array(
'DC' => '/^[0-9]{3}$/' // Diners Club CCV
));
}
public function OtherCcType($type)
{
return in_array($type, array('OT', 'DC'));
}
public function validate()
{
parent::validate();
/* we call parent's validate() function!
* if the code got this far, it means the cc type is none of the supported
* ones implemented by Magento and now it gets interesting
*/
$info = $this->getInfoInstance();
$ccNumber = $info->getCcNumber();
$availableTypes = explode(',',$this->getConfigData('cctypes'));
// checks if Diners Club card is allowed
if(!in_array($info->getCcType(), $availableTypes)){
Mage::throwException($this->_getHelper()->__('Credit card type is not allowed for this payment method.'));
}
// validate credit card number against Luhn algorithm
if(!$this->validateCcNum($info->getCcNumber())){
Mage::throwException($this->_getHelper()->__('Invalid Credit Card Number'));
}
/* this is Diners Club regex pattern.
* it's different for every cc type, so beware
*/
if($info->getCcType()=='DC' && !preg_match('/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/', $ccNumber)){
Mage::throwException($this->_getHelper()->__('Credit card number mismatch with credit card type.'));
}
// now we retrieve our CCV regex pattern and validate against it
$verificationRegex = $this->getVerificationRegEx();
if(!preg_match($verificationRegex[$info->getCcType()], $info->getCcCid()))
{
Mage::throwException($this->_getHelper()->__('Please enter a valid credit card verification number.'));
}
// further validation here (expiration month, expiration year etc.)
}
}
现在,总结一下我们的Diners Club卡号和验证码部分。你可能会想对validate()方法进一步验证,例如信用卡的到期时间。
客户端验证 这里有两种方法可以完成这个功能。 第一种是创建一个.js文件并用布局XML包含到我们想要的地方。第二种只需要修改布局文件。 这里我们将采用第二种方式。
<config>
...
<frontend>
<layout>
<updates>
<newmodule>
<file>newmodule.xml</file>
</newmodule>
</updates>
</layout>
</frontend>
</config>
是时候修改布局文件并引入javascript到checkout onepage。如果在此之前你没创建布局文件,那么用下面的代码创建它。
design/frontend/base/default/layout/newmodule.xml
<layout version="1.0.0">
<checkout_onepage_index>
<reference name="head">
<block type="core/text" name="newmodule.diners.validation">
<action method="setText">
<text>
<![CDATA[<script type="text/javascript">
Validation.creditCartTypes.set('DC', [new RegExp('^3(?:0[0-5]|[68][0-9])[0-9]{11}$'), new RegExp('^[0-9]{3}$'), true]);
</script>]]>
</text>
</action>
</block>
</reference>
</checkout_onepage_index>
</layout>
在我们NewModule的config.xml中,我应该把Diners Club加入到许可的信用卡类型列表里。
<config>
...
<default>
<payment>
<newmodule>
<model>newmodule/payment</model>
<cctypes>VI,AE,DC</cctypes> //Visa (VI), American Express (AE) and Diners Club (DC)
</newmodule>
</payment>
</default>
</config>
大功告成!我们成功地在Magento中添加了一个自定义信用卡类型。
360magento团队竭诚为您服务:)