Advertisement
Guest User

ismail

a guest
Jun 25th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.07 KB | None | 0 0
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Payment
  23. * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26.  
  27.  
  28. class Mage_Payment_Model_Method_Cc extends Mage_Payment_Model_Method_Abstract
  29. {
  30. protected $_formBlockType = 'payment/form_cc';
  31. protected $_infoBlockType = 'payment/info_cc';
  32. protected $_canSaveCc = false;
  33.  
  34. /**
  35. * Assign data to info model instance
  36. *
  37. * @param mixed $data
  38. * @return Mage_Payment_Model_Info
  39. */
  40. public function assignData($data)
  41. {
  42. if (!($data instanceof Varien_Object)) {
  43. $data = new Varien_Object($data);
  44. }
  45. $info = $this->getInfoInstance();
  46. $info->setCcType($data->getCcType())
  47. ->setCcOwner($data->getCcOwner())
  48. ->setCcLast4(substr($data->getCcNumber(), -4))
  49. ->setCcNumber($data->getCcNumber())
  50. ->setCcCid($data->getCcCid())
  51. ->setCcExpMonth($data->getCcExpMonth())
  52. ->setCcExpYear($data->getCcExpYear());
  53. return $this;
  54. }
  55.  
  56. /**
  57. * Prepare info instance for save
  58. *
  59. * @return Mage_Payment_Model_Abstract
  60. */
  61. public function prepareSave()
  62. {
  63. $info = $this->getInfoInstance();
  64. if ($this->_canSaveCc) {
  65. $info->setCcNumberEnc($info->encrypt($info->getCcNumber()));
  66. }
  67. //$info->setCcCidEnc($info->encrypt($info->getCcCid()));
  68. $info->setCcNumber(null)
  69. ->setCcCid(null);
  70. return $this;
  71. }
  72.  
  73. /**
  74. * Validate payment method information object
  75. *
  76. * @param Mage_Payment_Model_Info $info
  77. * @return Mage_Payment_Model_Abstract
  78. */
  79. public function validate()
  80. {
  81. /*
  82. * calling parent validate function
  83. */
  84. parent::validate();
  85.  
  86. $info = $this->getInfoInstance();
  87. $errorMsg = false;
  88. $availableTypes = explode(',',$this->getConfigData('cctypes'));
  89.  
  90. $ccNumber = $info->getCcNumber();
  91.  
  92. // remove credit card number delimiters such as "-" and space
  93. $ccNumber = preg_replace('/[\-\s]+/', '', $ccNumber);
  94. $info->setCcNumber($ccNumber);
  95.  
  96. $ccType = '';
  97.  
  98. if (!$this->_validateExpDate($info->getCcExpYear(), $info->getCcExpMonth())) {
  99. $errorCode = 'ccsave_expiration,ccsave_expiration_yr';
  100. $errorMsg = $this->_getHelper()->__('Incorrect credit card expiration date');
  101. }
  102.  
  103. if (in_array($info->getCcType(), $availableTypes)){
  104. if ($this->validateCcNum($ccNumber)
  105. // Other credit card type number validation
  106. || ($this->OtherCcType($info->getCcType()) && $this->validateCcNumOther($ccNumber))) {
  107.  
  108. $ccType = 'OT';
  109. $ccTypeRegExpList = array(
  110. 'VI' => '/^4[0-9]{12}([0-9]{3})?$/', // Visa
  111. 'MC' => '/^5[1-5][0-9]{14}$/', // Master Card
  112. 'AE' => '/^3[47][0-9]{13}$/', // American Express
  113. 'DI' => '/^6011[0-9]{12}$/', // Discovery
  114. '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})?$/'
  115. );
  116.  
  117. foreach ($ccTypeRegExpList as $ccTypeMatch=>$ccTypeRegExp) {
  118. if (preg_match($ccTypeRegExp, $ccNumber)) {
  119. $ccType = $ccTypeMatch;
  120. break;
  121. }
  122. }
  123.  
  124. if (!$this->OtherCcType($info->getCcType()) && $ccType!=$info->getCcType()) {
  125. $errorCode = 'ccsave_cc_type,ccsave_cc_number';
  126. $errorMsg = $this->_getHelper()->__('Credit card number mismatch with credit card type');
  127. }
  128. }
  129. else {
  130. $errorCode = 'ccsave_cc_number';
  131. $errorMsg = $this->_getHelper()->__('Invalid Credit Card Number');
  132. }
  133.  
  134. }
  135. else {
  136. $errorCode = 'ccsave_cc_type';
  137. $errorMsg = $this->_getHelper()->__('Credit card type is not allowed for this payment method');
  138. }
  139.  
  140. //validate credit card verification number
  141. if ($errorMsg === false && $this->hasVerification()) {
  142. $verifcationRegEx = $this->getVerificationRegEx();
  143. $regExp = isset($verifcationRegEx[$info->getCcType()]) ? $verifcationRegEx[$info->getCcType()] : '';
  144. if (!$info->getCcCid() || !$regExp || !preg_match($regExp ,$info->getCcCid())){
  145. $errorMsg = $this->_getHelper()->__('Please enter a valid credit card verification number.');
  146. }
  147. }
  148.  
  149. if($errorMsg){
  150. Mage::throwException($errorMsg);
  151. //throw Mage::exception('Mage_Payment', $errorMsg, $errorCode);
  152. }
  153.  
  154. return $this;
  155. }
  156.  
  157. public function hasVerification()
  158. {
  159. $configData = $this->getConfigData('useccv');
  160. if(is_null($configData)){
  161. return true;
  162. }
  163. return (bool) $configData;
  164. }
  165.  
  166. public function getVerificationRegEx()
  167. {
  168. $verificationExpList = array(
  169. 'VI' => '/^[0-9]{3}$/', // Visa
  170. 'MC' => '/^[0-9]{3}$/', // Master Card
  171. 'AE' => '/^[0-9]{4}$/', // American Express
  172. 'DI' => '/^[0-9]{3}$/', // Discovery
  173. 'SS' => '/^[0-9]{4}$/',
  174. 'OT' => '/^[0-9]{3,4}$/'
  175. );
  176. return $verificationExpList;
  177. }
  178.  
  179. protected function _validateExpDate($expYear, $expMonth)
  180. {
  181. $date = Mage::app()->getLocale()->date();
  182. if (!$expYear || !$expMonth || ($date->compareYear($expYear)==1) || ($date->compareYear($expYear) == 0 && ($date->compareMonth($expMonth)==1 ) )) {
  183. return false;
  184. }
  185. return true;
  186. }
  187.  
  188. public function OtherCcType($type)
  189. {
  190. return $type=='OT';
  191. }
  192.  
  193. /**
  194. * Validate credit card number
  195. *
  196. * @param string $cc_number
  197. * @return bool
  198. */
  199. public function validateCcNum($ccNumber)
  200. {
  201. $cardNumber = strrev($ccNumber);
  202. $numSum = 0;
  203.  
  204. for ($i=0; $i<strlen($cardNumber); $i++) {
  205. $currentNum = substr($cardNumber, $i, 1);
  206.  
  207. /**
  208. * Double every second digit
  209. */
  210. if ($i % 2 == 1) {
  211. $currentNum *= 2;
  212. }
  213.  
  214. /**
  215. * Add digits of 2-digit numbers together
  216. */
  217. if ($currentNum > 9) {
  218. $firstNum = $currentNum % 10;
  219. $secondNum = ($currentNum - $firstNum) / 10;
  220. $currentNum = $firstNum + $secondNum;
  221. }
  222.  
  223. $numSum += $currentNum;
  224. }
  225.  
  226. /**
  227. * If the total has no remainder it's OK
  228. */
  229. return ($numSum % 10 == 0);
  230. }
  231.  
  232. /**
  233. * Other credit cart type number validation
  234. *
  235. * @param string $ccNumber
  236. * @return boolean
  237. */
  238. public function validateCcNumOther($ccNumber)
  239. {
  240. return preg_match('/^\\d+$/', $ccNumber);
  241. }
  242.  
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement