irwan

Credit Card Class

Nov 15th, 2011
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.02 KB | None | 0 0
  1. <?php
  2.  
  3. class credit{
  4.  
  5.         function credit(){
  6.        
  7.         }
  8.        
  9.         function validate($cc_number, $cvv2, $date){
  10.                 $this->validateCard($cc_number);
  11.                 $this->validateExDate($date);
  12.                 $this->validateCVV($cc_number, $cvv2);
  13.         }
  14.  
  15.         function validateCard($cc_number){
  16.                 global $e;
  17.                
  18.                 $first_number = substr($cc_number, 0, 1);
  19.                 switch ($first_number){
  20.                         case 3:
  21.                                 if (!preg_match('/^3\d{3}[ \-]?\d{6}[ \-]?\d{5}$/', $cc_number)) $e->setError("American Express number is not valid");
  22.                                 break;
  23.                         case 4:
  24.                                 if (!preg_match('/^4\d{3}[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$/', $cc_number)) $e->setError("Visa number is not valid");
  25.                                 break;
  26.                         case 5:
  27.                                 if (!preg_match('/^5\d{3}[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$/', $cc_number)) $e->setError("MasterCard number is not valid");
  28.                                 break;
  29.                         case 6:
  30.                                 if (!preg_match('/^6011[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$/', $cc_number)) $e->setError("Discover Card number is not valid");
  31.                                 break;
  32.                         default:
  33.                                  $e->setError("Credit Card number is not valid: Error 1");
  34.                                  break;
  35.                 }
  36.                
  37.                 if($e->countErrors() == 0) $this->validateCard2($cc_number);
  38.  
  39.         }
  40.        
  41.         function validateCard2($cc_number){
  42.                 global $e;
  43.                
  44.                 $checksum = 0;
  45.                
  46.                 $j = 1;
  47.                
  48.                 for ($i = strlen($cc_number) - 1; $i >= 0; $i--){
  49.                
  50.                         $calc = substr($cc_number, $i, 1) * $j;
  51.                        
  52.                         if ($calc > 9){
  53.                                 $checksum = $checksum + 1;
  54.                                 $calc     = $calc - 10;
  55.                         }
  56.                        
  57.                         $checksum += $calc;
  58.                
  59.                         if ($j == 1){
  60.                                 $j = 2;
  61.                         }else{
  62.                                 $j = 1;
  63.                         }
  64.                 }
  65.                
  66.                 if ($checksum % 10 != 0) $e->setError("Credit Card number is not valid: Error 2");
  67.         }
  68.        
  69.         function validateExDate($date){
  70.                 global $e;
  71.                
  72.                 $month =  substr($date, 0, 2);
  73.                 $year =  substr($date, 2, 2);
  74.                
  75.                 $current_month = date("m");
  76.                 $current_year  = date("y");
  77.                 if ($year < $current_year){
  78.                         $e->setError("Expiration Date is not valid : Error 3");
  79.                 }else{
  80.                         if ($year == $current_year)
  81.                         {
  82.                                 if ($month < $current_month){
  83.                                         $e->setError("Expiration Date is not valid : Error 4");
  84.                                 }
  85.                         }
  86.                 }
  87.         }
  88.        
  89.         function validateCVV($cc_number, $cvv2){
  90.                 global $e;
  91.                
  92.                 $first_number = substr($cc_number, 0, 1);
  93.                
  94.                 if ($first_number == 3){
  95.                         if (!preg_match("/^\d{4}$/", $cvv2)) $e->setError("CVV number is not valid : Error 5");
  96.                 }else{
  97.                         if (!preg_match("/^\d{3}$/", $cvv2)) $e->setError("CVV number is not valid : Error 6");
  98.                 }
  99.         }
  100.        
  101.         function maskCard($cc_number){
  102.                 $count = strlen($cc_number);
  103.                 for($i = 1; $i <= ($count -4); $i++){
  104.                         echo "X";
  105.                 }
  106.                 echo substr($cc_number, -4);
  107.         }
  108.  
  109. }
  110.  
  111. ?>
  112.  
  113.  
  114. <?php
  115.        
  116.         require_once "class.error.php";
  117.     $e = new error;
  118.         require_once "class.credit.php";
  119.         $cc = new credit;      
  120.        
  121.         if($_POST){
  122.                 $cc_number = $_POST['cc_number'];
  123.                 $expire_month = $_POST['expire_month'];
  124.                 $expire_year = $_POST['expire_year'];
  125.                 $cc_cvv = $_POST['cc_cvv'];
  126.                
  127.                 $date = $expire_month . $expire_year;
  128.                 $cc->validate($cc_number, $cc_cvv, $date);
  129.                
  130.                 if($e->countErrors() == 0){
  131.                         //process form
  132.                         header("location: somewhere.php");
  133.                         die();
  134.                 }
  135.         }
  136.                
  137. ?>
  138.    
  139.     <?php $e->showErrors(); ?>
  140.         <form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
  141.     <table border="0" cellpadding="4" width="450">
  142.       <tr>
  143.         <td width="200" class="tbl_left">Payment Type: </td>
  144.         <td width="250" class="tbl_right">Credit Card</td>
  145.       </tr>
  146.       <tr>
  147.         <td class="tbl_left">Card Holder Name: </td>
  148.         <td class="tbl_right"><input type="text" name="cc_name" /></td>
  149.       </tr>
  150.       <tr>
  151.         <td class="tbl_left">Card Type: </td>
  152.         <td class="tbl_right"><select name="cc_type">
  153.         <option value="American Express">American Express</option>
  154.         <option value="Discover">Discover</option>
  155.         <option value="Master Card">Master Card</option>
  156.         <option value="Visa">Visa</option>
  157.     </select>
  158.     </td>
  159.       </tr>
  160.       <tr>
  161.         <td class="tbl_left">Credit Card Number: </td>
  162.         <td class="tbl_right"><input type="text" name="cc_number" /></td>
  163.       </tr>
  164.       <tr>
  165.         <td class="tbl_left">Expiration Date: </td>
  166.         <td class="tbl_right"><select name="expire_month">
  167.     <option value="01">January</option>
  168.     <option value="02">February</option>
  169.     <option value="03">March</option>
  170.     <option value="04">April</option>
  171.     <option value="05">May</option>
  172.     <option value="06">June</option>
  173.     <option value="07">July</option>
  174.     <option value="08">August</option>
  175.     <option value="09">September</option>
  176.     <option value="10">October</option>
  177.     <option value="11">November</option>
  178.     <option value="12">December</option>
  179.     </select>
  180.     <select name="expire_year">
  181.     <?php
  182.     for($x=date(Y); $x<=2050; $x++){
  183.     echo '<option value="'.substr($x, -2).'">'.$x.'</option>'."\n";
  184.     }
  185.     ?>
  186.     </select>
  187.     </td>
  188.       </tr>
  189.       <tr>
  190.         <td class="tbl_left">CVV Number:</td>
  191.         <td class="tbl_right"><input type="text" name="cc_cvv" size="5" maxlength="4" /></td>
  192.       </tr>
  193.       <tr>
  194.         <td class="tbl_left">Enter a Promo Code: </td>
  195.         <td class="tbl_right"><input type="text" name="promo_code" /></td>
  196.       </tr>
  197.     </table>
  198. </form>
  199.  
  200.  
  201. echo $cc->maskCard($cc_number);
  202.  
Add Comment
Please, Sign In to add comment