10/29/2013

How to add Diners Club credit card in magento

Default Magento doesn't have this card in his payment gateway. If you want to add then follow the below steps, I have describe here in core file you need to override all core files before doing any changes

Go to app/code/core/Mage/Payemtn/etc/Config.xml then go to line number 56 you can find there are already some card has defined, you need to add your code there

<payment> <!--(already defined)-->
<cc> <!--(already defined)-->
<types> <!--(already defined)-->
<DC>
<code>DC</code>
<name>Diners Club</name>
<order>80</order>
</DC>
</types> <!--(already defined)-->
</cc><!--(already defined)-->
</types><!--(already defined)-->

You can copy paste this section in your custom module to add the Diners Club card else Copy from <DC> to </DC> and paste it after the </OT>

Now you successfully added the card in to Credit card Dropdown.

Next you have to validate the card, Magento use two types of validation one Javascript validation another PHP validation You have to validate both way.

1) Javascript Validation
Go to magento root directory then js/prototype/validation.js then go to end of the page you will find there already all card validation added, The code will be something like this

Validation.creditCartTypes = $H({
'SS': [new RegExp('^((6759[0-9]{12})|(5018|5020|5038|6304|6759|6761|6763[0-9]{12,19})|(49[013][1356][0-9]{12})|(6333[0-9]{12})|(6334[0-4]\d{11})|(633110[0-9]{10})|(564182[0-9]{10}))([0-9]{2,3})?$'), new RegExp('^([0-9]{3}|[0-9]{4})?$'), true],
'SO': [new RegExp('^(6334[5-9]([0-9]{11}|[0-9]{13,14}))|(6767([0-9]{12}|[0-9]{14,15}))$'), new RegExp('^([0-9]{3}|[0-9]{4})?$'), true],
'SM': [new RegExp('(^(5[0678])[0-9]{11,18}$)|(^(6[^05])[0-9]{11,18}$)|(^(601)[^1][0-9]{9,16}$)|(^(6011)[0-9]{9,11}$)|(^(6011)[0-9]{13,16}$)|(^(65)[0-9]{11,13}$)|(^(65)[0-9]{15,18}$)|(^(49030)[2-9]([0-9]{10}$|[0-9]{12,13}$))|(^(49033)[5-9]([0-9]{10}$|[0-9]{12,13}$))|(^(49110)[1-2]([0-9]{10}$|[0-9]{12,13}$))|(^(49117)[4-9]([0-9]{10}$|[0-9]{12,13}$))|(^(49118)[0-2]([0-9]{10}$|[0-9]{12,13}$))|(^(4936)([0-9]{12}$|[0-9]{14,15}$))'), new RegExp('^([0-9]{3}|[0-9]{4})?$'), true],
'VI': [new RegExp('^4[0-9]{12}([0-9]{3})?$'), new RegExp('^[0-9]{3}$'), true],
'MC': [new RegExp('^5[1-5][0-9]{14}$'), new RegExp('^[0-9]{3}$'), true],
'AE': [new RegExp('^3[47][0-9]{13}$'), new RegExp('^[0-9]{4}$'), true],
'DI': [new RegExp('^6011[0-9]{12}$'), new RegExp('^[0-9]{3}$'), true],
'JCB': [new RegExp('^(3[0-9]{15}|(2131|1800)[0-9]{11})$'), new RegExp('^[0-9]{4}$'), true],
'OT': [false, new RegExp('^([0-9]{3}|[0-9]{4})?$'), false]
});

then replace this function with the below content

Validation.creditCartTypes = $H({
'SS': [new RegExp('^((6759[0-9]{12})|(5018|5020|5038|6304|6759|6761|6763[0-9]{12,19})|(49[013][1356][0-9]{12})|(6333[0-9]{12})|(6334[0-4]\d{11})|(633110[0-9]{10})|(564182[0-9]{10}))([0-9]{2,3})?$'), new RegExp('^([0-9]{3}|[0-9]{4})?$'), true],
'SO': [new RegExp('^(6334[5-9]([0-9]{11}|[0-9]{13,14}))|(6767([0-9]{12}|[0-9]{14,15}))$'), new RegExp('^([0-9]{3}|[0-9]{4})?$'), true],
'SM': [new RegExp('(^(5[0678])[0-9]{11,18}$)|(^(6[^05])[0-9]{11,18}$)|(^(601)[^1][0-9]{9,16}$)|(^(6011)[0-9]{9,11}$)|(^(6011)[0-9]{13,16}$)|(^(65)[0-9]{11,13}$)|(^(65)[0-9]{15,18}$)|(^(49030)[2-9]([0-9]{10}$|[0-9]{12,13}$))|(^(49033)[5-9]([0-9]{10}$|[0-9]{12,13}$))|(^(49110)[1-2]([0-9]{10}$|[0-9]{12,13}$))|(^(49117)[4-9]([0-9]{10}$|[0-9]{12,13}$))|(^(49118)[0-2]([0-9]{10}$|[0-9]{12,13}$))|(^(4936)([0-9]{12}$|[0-9]{14,15}$))'), new RegExp('^([0-9]{3}|[0-9]{4})?$'), true],
'VI': [new RegExp('^4[0-9]{12}([0-9]{3})?$'), new RegExp('^[0-9]{3}$'), true],
'MC': [new RegExp('^5[1-5][0-9]{14}$'), new RegExp('^[0-9]{3}$'), true],
'AE': [new RegExp('^3[47][0-9]{13}$'), new RegExp('^[0-9]{4}$'), true],
'DI': [new RegExp('^6011[0-9]{12}$'), new RegExp('^[0-9]{3}$'), true],
'JCB': [new RegExp('^(3[0-9]{15}|(2131|1800)[0-9]{11})$'), new RegExp('^[0-9]{4}$'), true],
'OT': [false, new RegExp('^([0-9]{3}|[0-9]{4})?$'), false],
'DC': [new RegExp('^3(?:0[0-5]|[68][0-9])[0-9]{11}$'), new RegExp('^[0-9]{3}$'), true]
});

So this way you can do the javascript validation

If you don't want to edit the js core file then add the below script in to your form/cc.phtml or in your custom phtml file which is responsible for showing the payment Block

<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>

2) PHP validation
Go to app/code/core/Mage/Payment/Model/Method/Cc.php
there you can find the validation line number near about 111

$ccTypeRegExpList = array(
//Solo, Switch or Maestro. International safe
'SO' => '/(^(6334)[5-9](\d{11}$|\d{13,14}$))|(^(6767)(\d{12}$|\d{14,15}$))/', // Solo only
'SM' => '/(^(5[0678])\d{11,18}$)|(^(6[^05])\d{11,18}$)|(^(601)[^1]\d{9,16}$)|(^(6011)\d{9,11}$)|(^(6011)\d{13,16}$)|(^(65)\d{11,13}$)|(^(65)\d{15,18}$)|(^(49030)[2-9](\d{10}$|\d{12,13}$))|(^(49033)[5-9](\d{10}$|\d{12,13}$))|(^(49110)[1-2](\d{10}$|\d{12,13}$))|(^(49117)[4-9](\d{10}$|\d{12,13}$))|(^(49118)[0-2](\d{10}$|\d{12,13}$))|(^(4936)(\d{12}$|\d{14,15}$))/',

'SS' => '/^((6759[0-9]{12})|(6334|6767[0-9]{12})|(6334|6767[0-9]{14,15})|(5018|5020|5038|6304|6759|6761|6763[0-9]{12,19})|(49[013][1356][0-9]{12})|(633[34][0-9]{12})|(633110[0-9]{10})|(564182[0-9]{10}))([0-9]{2,3})?$/', // Maestro / Solo
'VI' => '/^4[0-9]{12}([0-9]{3})?$/', // Visa
'MC' => '/^5[1-5][0-9]{14}$/', // Master Card
'AE' => '/^3[47][0-9]{13}$/', // American Express
'DI' => '/^6011[0-9]{12}$/', // Discovery
'JCB' => '/^(3[0-9]{15}|(2131|1800)[0-9]{11})$/', // JCB
);

replace the above code with the below code

$ccTypeRegExpList = array(
//Solo, Switch or Maestro. International safe
'SO' => '/(^(6334)[5-9](\d{11}$|\d{13,14}$))|(^(6767)(\d{12}$|\d{14,15}$))/', // Solo only
'SM' => '/(^(5[0678])\d{11,18}$)|(^(6[^05])\d{11,18}$)|(^(601)[^1]\d{9,16}$)|(^(6011)\d{9,11}$)|(^(6011)\d{13,16}$)|(^(65)\d{11,13}$)|(^(65)\d{15,18}$)|(^(49030)[2-9](\d{10}$|\d{12,13}$))|(^(49033)[5-9](\d{10}$|\d{12,13}$))|(^(49110)[1-2](\d{10}$|\d{12,13}$))|(^(49117)[4-9](\d{10}$|\d{12,13}$))|(^(49118)[0-2](\d{10}$|\d{12,13}$))|(^(4936)(\d{12}$|\d{14,15}$))/',

'SS' => '/^((6759[0-9]{12})|(6334|6767[0-9]{12})|(6334|6767[0-9]{14,15})|(5018|5020|5038|6304|6759|6761|6763[0-9]{12,19})|(49[013][1356][0-9]{12})|(633[34][0-9]{12})|(633110[0-9]{10})|(564182[0-9]{10}))([0-9]{2,3})?$/', // Maestro / Solo
'VI' => '/^4[0-9]{12}([0-9]{3})?$/', // Visa
'MC' => '/^5[1-5][0-9]{14}$/', // Master Card
'AE' => '/^3[47][0-9]{13}$/', // American Express
'DI' => '/^6011[0-9]{12}$/', // Discovery
'JCB' => '/^(3[0-9]{15}|(2131|1800)[0-9]{11})$/', // JCB
'DC' => '/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/', //Diners Club
);

Now go to Line number 183 there already a function which validate CVV number . The codes are

public function getVerificationRegEx()
{
$verificationExpList = array(
'VI' => '/^[0-9]{3}$/', // Visa
'MC' => '/^[0-9]{3}$/', // Master Card
'AE' => '/^[0-9]{4}$/', // American Express
'DI' => '/^[0-9]{3}$/', // Discovery
'SS' => '/^[0-9]{3,4}$/',
'SM' => '/^[0-9]{3,4}$/', // Switch or Maestro
'SO' => '/^[0-9]{3,4}$/', // Solo
'OT' => '/^[0-9]{3,4}$/',
'JCB' => '/^[0-9]{4}$/' //JCB
);
return $verificationExpList;
}

Now replace this function with below function

public function getVerificationRegEx()
{
$verificationExpList = array(
'VI' => '/^[0-9]{3}$/', // Visa
'MC' => '/^[0-9]{3}$/', // Master Card
'AE' => '/^[0-9]{4}$/', // American Express
'DI' => '/^[0-9]{3}$/', // Discovery
'SS' => '/^[0-9]{3,4}$/',
'SM' => '/^[0-9]{3,4}$/', // Switch or Maestro
'SO' => '/^[0-9]{3,4}$/', // Solo
'OT' => '/^[0-9]{3,4}$/',
'JCB' => '/^[0-9]{4}$/', //JCB
'DC' => '/^[0-9]{3}$/' // Diners Club
);
return $verificationExpList;
}

That's All , Now magento will successfully validate your Diners club code. Instead of doing this changes in magento core files try to override the model class then change it in your custom module.