Adding custom credit card type to your payment method in Magento

At one point or another, you might want to implement a payment method with credit card types that are not implemented in Magento by default.
Please note that in this article you’ll learn how to add a new credit card type and validate it only. This article does not cover the whole payment module creation.

For the needs of this article, lets assume we have created a new payment module named NewModule and corresponding app/etc/modules/Inchoo_NewModule.xml file. We are going to implement Diners Club credit card type and validate it both server-side and client-side, the Magento way.

Steps

There are a few steps we have to go through in order to make the magic happen, the Magento way of course, and each one is equally important as the other.

Add credit card type to global payment node in our config.xml

Let Magento know which model are we using as our payment model

Implement server-side validation in our model

Implement client-side validation (Prototype)

Adding new credit card type

Lets hit it off by creating our etc/config.xml and adding credit card type to it:

Now we have to create a file Inchoo/NewModule/Model/Payment.php and it has to extend Mage_Payment_Model_Method_Cc.
This is the place where we’ll use our custom Regex pattern for credit card number and CVV.
If you’re implementing a different credit card type, you’ll need a Regex pattern that corresponds to it.

class Inchoo_NewModule_Model_Payment extends Mage_Payment_Model_Method_Cc
{protected$_code='inchoo_newmodule';publicfunction getVerificationRegEx(){returnarray_merge(parent::getVerificationRegEx(),array('DC'=>'/^[0-9]{3}$/'// Diners Club CCV));}publicfunction OtherCcType($type){returnin_array($type,array('OT','DC'));}publicfunction 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 allowedif(!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 algorithmif(!$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.)}}

Now that sums up our Diners Club card number and CCV number validation part. You’ll probably want to make additional checks in validate() method, such as credit card expiration date.

Client-side validation in Prototype

There are two ways we can tackle this issue.
The first one would be creating a .js file and including it via layout XML file where we need it. The second one would include modifying layout XML file only.
We’ll do it the second way here.

It’s time to modify the layout file and implement Javascript at checkout onepage. In case you haven’t created the layout xml file already, create it at design/frontend/base/default/layout/newmodule.xml with the following code:

1 comment

Your email address will not be published. Required fields are marked *

Comment *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.