00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 class Mage_Payment_Model_Method_Cc extends Mage_Payment_Model_Method_Abstract
00029 {
00030 protected $_formBlockType = 'payment/form_cc';
00031 protected $_infoBlockType = 'payment/info_cc';
00032 protected $_canSaveCc = false;
00033
00034
00035
00036
00037
00038
00039
00040 public function assignData($data)
00041 {
00042 if (!($data instanceof Varien_Object)) {
00043 $data = new Varien_Object($data);
00044 }
00045 $info = $this->getInfoInstance();
00046 $info->setCcType($data->getCcType())
00047 ->setCcOwner($data->getCcOwner())
00048 ->setCcLast4(substr($data->getCcNumber(), -4))
00049 ->setCcNumber($data->getCcNumber())
00050 ->setCcCid($data->getCcCid())
00051 ->setCcExpMonth($data->getCcExpMonth())
00052 ->setCcExpYear($data->getCcExpYear());
00053 return $this;
00054 }
00055
00056
00057
00058
00059
00060
00061 public function prepareSave()
00062 {
00063 $info = $this->getInfoInstance();
00064 if ($this->_canSaveCc) {
00065 $info->setCcNumberEnc($info->encrypt($info->getCcNumber()));
00066 }
00067
00068 $info->setCcNumber(null)
00069 ->setCcCid(null);
00070 return $this;
00071 }
00072
00073
00074
00075
00076
00077
00078
00079 public function validate()
00080 {
00081
00082
00083
00084 parent::validate();
00085
00086 $info = $this->getInfoInstance();
00087 $errorMsg = false;
00088 $availableTypes = explode(',',$this->getConfigData('cctypes'));
00089
00090 $ccNumber = $info->getCcNumber();
00091
00092
00093 $ccNumber = preg_replace('/[\-\s]+/', '', $ccNumber);
00094 $info->setCcNumber($ccNumber);
00095
00096 $ccType = '';
00097
00098 if (!$this->_validateExpDate($info->getCcExpYear(), $info->getCcExpMonth())) {
00099 $errorCode = 'ccsave_expiration,ccsave_expiration_yr';
00100 $errorMsg = $this->_getHelper()->__('Incorrect credit card expiration date');
00101 }
00102
00103 if (in_array($info->getCcType(), $availableTypes)){
00104 if ($this->validateCcNum($ccNumber)
00105
00106 || ($this->OtherCcType($info->getCcType()) && $this->validateCcNumOther($ccNumber))) {
00107
00108 $ccType = 'OT';
00109 $ccTypeRegExpList = array(
00110 'VI' => '/^4[0-9]{12}([0-9]{3})?$/',
00111 'MC' => '/^5[1-5][0-9]{14}$/',
00112 'AE' => '/^3[47][0-9]{13}$/',
00113 'DI' => '/^6011[0-9]{12}$/',
00114 'SS' => '/^((6759[0-9]{12})|(49[013][1356][0-9]{13})|(633[34][0-9]{12})|(633110[0-9]{10})|(564182[0-9]{10}))([0-9]{2,3})?$/'
00115 );
00116
00117 foreach ($ccTypeRegExpList as $ccTypeMatch=>$ccTypeRegExp) {
00118 if (preg_match($ccTypeRegExp, $ccNumber)) {
00119 $ccType = $ccTypeMatch;
00120 break;
00121 }
00122 }
00123
00124 if (!$this->OtherCcType($info->getCcType()) && $ccType!=$info->getCcType()) {
00125 $errorCode = 'ccsave_cc_type,ccsave_cc_number';
00126 $errorMsg = $this->_getHelper()->__('Credit card number mismatch with credit card type');
00127 }
00128 }
00129 else {
00130 $errorCode = 'ccsave_cc_number';
00131 $errorMsg = $this->_getHelper()->__('Invalid Credit Card Number');
00132 }
00133
00134 }
00135 else {
00136 $errorCode = 'ccsave_cc_type';
00137 $errorMsg = $this->_getHelper()->__('Credit card type is not allowed for this payment method');
00138 }
00139
00140
00141 if ($errorMsg === false && $this->hasVerification()) {
00142 $verifcationRegEx = $this->getVerificationRegEx();
00143 $regExp = isset($verifcationRegEx[$info->getCcType()]) ? $verifcationRegEx[$info->getCcType()] : '';
00144 if (!$info->getCcCid() || !$regExp || !preg_match($regExp ,$info->getCcCid())){
00145 $errorMsg = $this->_getHelper()->__('Please enter a valid credit card verification number.');
00146 }
00147 }
00148
00149 if($errorMsg){
00150 Mage::throwException($errorMsg);
00151
00152 }
00153
00154 return $this;
00155 }
00156
00157 public function hasVerification()
00158 {
00159 $configData = $this->getConfigData('useccv');
00160 if(is_null($configData)){
00161 return true;
00162 }
00163 return (bool) $configData;
00164 }
00165
00166 public function getVerificationRegEx()
00167 {
00168 $verificationExpList = array(
00169 'VI' => '/^[0-9]{3}$/',
00170 'MC' => '/^[0-9]{3}$/',
00171 'AE' => '/^[0-9]{4}$/',
00172 'DI' => '/^[0-9]{3}$/',
00173 'SS' => '/^[0-9]{4}$/',
00174 'OT' => '/^[0-9]{3,4}$/'
00175 );
00176 return $verificationExpList;
00177 }
00178
00179 protected function _validateExpDate($expYear, $expMonth)
00180 {
00181 $date = Mage::app()->getLocale()->date();
00182 if (!$expYear || !$expMonth || ($date->compareYear($expYear)==1) || ($date->compareYear($expYear) == 0 && ($date->compareMonth($expMonth)==1 ) )) {
00183 return false;
00184 }
00185 return true;
00186 }
00187
00188 public function OtherCcType($type)
00189 {
00190 return $type=='OT';
00191 }
00192
00193
00194
00195
00196
00197
00198
00199 public function validateCcNum($ccNumber)
00200 {
00201 $cardNumber = strrev($ccNumber);
00202 $numSum = 0;
00203
00204 for ($i=0; $i<strlen($cardNumber); $i++) {
00205 $currentNum = substr($cardNumber, $i, 1);
00206
00207
00208
00209
00210 if ($i % 2 == 1) {
00211 $currentNum *= 2;
00212 }
00213
00214
00215
00216
00217 if ($currentNum > 9) {
00218 $firstNum = $currentNum % 10;
00219 $secondNum = ($currentNum - $firstNum) / 10;
00220 $currentNum = $firstNum + $secondNum;
00221 }
00222
00223 $numSum += $currentNum;
00224 }
00225
00226
00227
00228
00229 return ($numSum % 10 == 0);
00230 }
00231
00232
00233
00234
00235
00236
00237
00238 public function validateCcNumOther($ccNumber)
00239 {
00240 return preg_match('/^\\d+$/', $ccNumber);
00241 }
00242
00243 }