[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 696
  • Last Modified:

credit card validation

$cc = new CCreditCard($ccName, $ccType, $ccNum, $ccExpM, $ccExpY);

?>

<h2>Validation Results</h2>

<b>Name: </b><?php echo $cc->Name(); ?><br>

<b>Number: </b><?php echo $cc->SafeNumber('x', 6); ?><br>

<b>Type: </b><?php echo $cc->Type(); ?><br>

<b>Expires: </b><?php echo $cc->ExpiryMonth() . '/' . $cc->ExpiryYear(); ?><br><br>


Fatal error: Call to undefined function: safenumber() in /home/gamesemp/public_html/testcc.php on line 101
0
acslater
Asked:
acslater
  • 4
1 Solution
 
Richard QuadlingSenior Software DeverloperCommented:
Hi acslater,


Does the class CCreditCard have a SafeNumber method?

If not, are you asking how to write one?



Regards,

Richard Quadling.
0
 
alextr2003frCommented:
0
 
alextr2003frCommented:
/////////////////////////////////////////////////////////
<?php

define("CARD_TYPE_MC", 0);
define("CARD_TYPE_VS", 1);
define("CARD_TYPE_AX", 2);
define("CARD_TYPE_DC", 3);
define("CARD_TYPE_DS", 4);
define("CARD_TYPE_JC", 5);

class CCreditCard
{
// Class Members
var $__ccName = '';
var $__ccType = '';
var $__ccNum = '';
var $__ccExpM = 0;
var $__ccExpY = 0;

// Constructor
function CCreditCard($name, $type, $num, $expm, $expy)
{

// Set member variables
if(!empty($name))
{
$this->__ccName = $name;
}
else
{
die('Must pass name to constructor');
}

// Make sure card type is valid
switch(strtolower($type))
{
  case 'mc':
  case 'mastercard':
  case 'm':
  case '1':
    $this->__ccType = CARD_TYPE_MC;
    break;
  case 'vs':
  case 'visa':
  case 'v':
  case '2':
    $this->__ccType = CARD_TYPE_VS;
    break;
  case 'ax':
  case 'american express':
  case 'a':
  case '3':
    $this->__ccType = CARD_TYPE_AX;
    break;
  case 'dc':
  case 'diners club':
  case '4':
    $this->__ccType = CARD_TYPE_DC;
    break;
  case 'ds':
  case 'discover':
  case '5':
    $this->__ccType = CARD_TYPE_DS;
    break;
  case 'jc':
  case 'jcb':
  case '6':
    $this->__ccType = CARD_TYPE_JC;
    break;
  default:
    die('Invalid type ' . $type . ' passed to constructor');
}

// Don't check the number yet,
// just kill all non numerics
if(!empty($num))
{
  $cardNumber = ereg_replace("[^0-9]", "", $num);
  // Make sure the card number isnt empty
  if(!empty($cardNumber))
  {
    $this->__ccNum = $cardNumber;
  }
  else
  {
    die('Must pass number to constructor');
  }
}
else
{
  die('Must pass number to constructor');
}

if(!is_numeric($expm) || $expm < 1 || $expm > 12)
{
  die('Invalid expiry month of ' . $expm . ' passed to constructor');
}
else
{
  $this->__ccExpM = $expm;
}

// Get the current year
$currentYear = date('Y');
settype($currentYear, 'integer');


if((!is_numeric($expy)) || ($expy < $currentYear) || ($expy > $currentYear + 10))
{
  die('Invalid expiry year of ' . $expy . ' passed to constructor');
}
else
{
  $this->__ccExpY = $expy;
}
}

function Name()
{
  return $this->__ccName;
}

function Type()
{
  switch($this->__ccType)
    {
    case CARD_TYPE_MC:
      return 'Mastercard [1]';
      break;
    case CARD_TYPE_VS:
      return 'Visa [2]';
      break;
    case CARD_TYPE_AX:
      return 'Amex [3]';
      break;
    case CARD_TYPE_DC:
      return 'Diners Club [4]';
      break;
    case CARD_TYPE_DS:
      return 'Discover [5]';
      break;
    case CARD_TYPE_JC:
      return 'JCB [6]';
      break;
    default:
      return 'Unknown [-1]';
  }
}

function Number()
{
  return $this->__ccNum;
}

function ExpiryMonth()
{
  return $this->__ccExpM;
}

function ExpiryYear()
{
  return $this->__ccExpY;
}

function SafeNumber($char = 'x', $numToHide = 4)
{
  // Return only part of the number
  if($numToHide < 4)
  {
    $numToHide = 4;
  }

  if($numToHide > 10)
  {
    $numToHide = 10;
  }

  $cardNumber = $this->__ccNum;
  $cardNumber = substr($cardNumber, 0, strlen($cardNumber) - $numToHide);

  for($i = 0; $i < $numToHide; $i++)
  {
    $cardNumber .= $char;
  }

  return $cardNumber;
}

function IsValid()
{
  // Not valid by default
  $validFormat = false;
  $passCheck = false;
// Is the number in the correct format?
switch($this->__ccType)
{
  case CARD_TYPE_MC:
    $validFormat = ereg("^5[1-5][0-9]{14}$", $this->__ccNum);
    break;
case CARD_TYPE_VS:
    $validFormat = ereg("^4[0-9]{12}([0-9]{3})?$", $this->__ccNum);
    break;
case CARD_TYPE_AX:
    $validFormat = ereg("^3[47][0-9]{13}$", $this->__ccNum);
    break;
case CARD_TYPE_DC:
    $validFormat = ereg("^3(0[0-5]|[68][0-9])[0-9]{11}$", $this->__ccNum);
    break;
case CARD_TYPE_DS:
    $validFormat = ereg("^6011[0-9]{12}$", $this->__ccNum);
    break;
case CARD_TYPE_JC:
    $validFormat = ereg("^(3[0-9]{4}|2131|1800)[0-9]{11}$", $this->__ccNum);
    break;
  default:
  // Should never be executed
  $validFormat = false;
}
// Is the number valid?
$cardNumber = strrev($this->__ccNum);
$numSum = 0;

for($i = 0; $i < strlen($cardNumber); $i++)
{
  $currentNum = substr($cardNumber, $i, 1);

// Double every second digit
if($i % 2 == 1)
{
  $currentNum *= 2;
}

// Add digits of 2-digit numbers together
if($currentNum > 9)
{
  $firstNum = $currentNum % 10;
  $secondNum = ($currentNum - $firstNum) / 10;
  $currentNum = $firstNum + $secondNum;
}

$numSum += $currentNum;
}

// If the total has no remainder it's OK
$passCheck = ($numSum % 10 == 0);
 if($validFormat && $passCheck) {return true;} else {return false;}
 }
 }
?>


<?php
//corrected
if(!isset($_POST['submit']))
{
?>

  <h2>Validate Credit Card</h2>
  <form name="frmCC" action="" method="post">
  Numbers used only for example!<br>
  Cardholders name: <input type="text" name="ccName" value="Card Holder"><br>
  Card number: <input type="text" name="ccNum" value="5500 0000 0000 0004"><br>
  Card type: <select name="ccType">
  <option value="1">Mastercard</option>
  <option value="2">Visa</option>
  <option value="3">Amex</option>
  <option value="4">Diners</option>
  <option value="5">Discover</option>
  <option value="6">JCB</option>
  </select><br>

  Expiry Date: <select name="ccExpM">

  <?php

    for($i = 1; $i < 13; $i++)
    { echo '<option>' . $i . '</option>'; }

  ?>

  </select>

  <select name="ccExpY">

  <?php

    for($i = 2006; $i < 2010; $i++)
    { echo '<option value="'.$i.'">' . $i . '</option>'; }

  ?>

  </select><br><br>

  <input type="submit" name="submit" value="Validate">
  </form>

  <?

  }
  else
  {
  // Check if the card is valid
  //corrected
  $cc = new CCreditCard($_POST['ccName'], $_POST['ccType'], $_POST['ccNum'], $_POST['ccExpM'], $_POST['ccExpY']);

  ?>
  <h2>Validation Results</h2>
  <b>Name: </b><?=$cc->Name(); ?><br>
  <b>Number: </b><?=$cc->SafeNumber('x', 6); ?><br>
  <b>Type: </b><?=$cc->Type(); ?><br>
  <b>Expires: </b><?=$cc->ExpiryMonth() . '/' .
  $cc->ExpiryYear(); ?><br><br>

  <?php

    echo '<font color="blue" size="2"><b>';
    if($cc->IsValid()) {echo 'VALID CARD';} else {echo 'INVALID CARD';}
    echo '</b></font>';
  }
?>
/////////////////////////////////////////////////////////
0
 
alextr2003frCommented:
the numbers in the other one was just to check the results here is the solution with a clean form :

///////////////////////////////////////////////////////
<?php



define("CARD_TYPE_MC", 0);
define("CARD_TYPE_VS", 1);
define("CARD_TYPE_AX", 2);
define("CARD_TYPE_DC", 3);
define("CARD_TYPE_DS", 4);
define("CARD_TYPE_JC", 5);

class CCreditCard
{
// Class Members
var $__ccName = '';
var $__ccType = '';
var $__ccNum = '';
var $__ccExpM = 0;
var $__ccExpY = 0;

// Constructor
function CCreditCard($name, $type, $num, $expm, $expy)
{

// Set member variables
if(!empty($name))
{
$this->__ccName = $name;
}
else
{
die('Must pass name to constructor');
}

// Make sure card type is valid
switch(strtolower($type))
{
  case 'mc':
  case 'mastercard':
  case 'm':
  case '1':
    $this->__ccType = CARD_TYPE_MC;
    break;
  case 'vs':
  case 'visa':
  case 'v':
  case '2':
    $this->__ccType = CARD_TYPE_VS;
    break;
  case 'ax':
  case 'american express':
  case 'a':
  case '3':
    $this->__ccType = CARD_TYPE_AX;
    break;
  case 'dc':
  case 'diners club':
  case '4':
    $this->__ccType = CARD_TYPE_DC;
    break;
  case 'ds':
  case 'discover':
  case '5':
    $this->__ccType = CARD_TYPE_DS;
    break;
  case 'jc':
  case 'jcb':
  case '6':
    $this->__ccType = CARD_TYPE_JC;
    break;
  default:
    die('Invalid type ' . $type . ' passed to constructor');
}

// Don't check the number yet,
// just kill all non numerics
if(!empty($num))
{
  $cardNumber = ereg_replace("[^0-9]", "", $num);
  // Make sure the card number isnt empty
  if(!empty($cardNumber))
  {
    $this->__ccNum = $cardNumber;
  }
  else
  {
    die('Must pass number to constructor');
  }
}
else
{
  die('Must pass number to constructor');
}

if(!is_numeric($expm) || $expm < 1 || $expm > 12)
{
  die('Invalid expiry month of ' . $expm . ' passed to constructor');
}
else
{
  $this->__ccExpM = $expm;
}

// Get the current year
$currentYear = date('Y');
settype($currentYear, 'integer');


if((!is_numeric($expy)) || ($expy < $currentYear) || ($expy > $currentYear + 10))
{
  die('Invalid expiry year of ' . $expy . ' passed to constructor');
}
else
{
  $this->__ccExpY = $expy;
}
}

function Name()
{
  return $this->__ccName;
}

function Type()
{
  switch($this->__ccType)
    {
    case CARD_TYPE_MC:
      return 'Mastercard [1]';
      break;
    case CARD_TYPE_VS:
      return 'Visa [2]';
      break;
    case CARD_TYPE_AX:
      return 'Amex [3]';
      break;
    case CARD_TYPE_DC:
      return 'Diners Club [4]';
      break;
    case CARD_TYPE_DS:
      return 'Discover [5]';
      break;
    case CARD_TYPE_JC:
      return 'JCB [6]';
      break;
    default:
      return 'Unknown [-1]';
  }
}

function Number()
{
  return $this->__ccNum;
}

function ExpiryMonth()
{
  return $this->__ccExpM;
}

function ExpiryYear()
{
  return $this->__ccExpY;
}

function SafeNumber($char = 'x', $numToHide = 4)
{
  // Return only part of the number
  if($numToHide < 4)
  {
    $numToHide = 4;
  }

  if($numToHide > 10)
  {
    $numToHide = 10;
  }

  $cardNumber = $this->__ccNum;
  $cardNumber = substr($cardNumber, 0, strlen($cardNumber) - $numToHide);

  for($i = 0; $i < $numToHide; $i++)
  {
    $cardNumber .= $char;
  }

  return $cardNumber;
}

function IsValid()
{
  // Not valid by default
  $validFormat = false;
  $passCheck = false;
// Is the number in the correct format?
switch($this->__ccType)
{
  case CARD_TYPE_MC:
    $validFormat = ereg("^5[1-5][0-9]{14}$", $this->__ccNum);
    break;
case CARD_TYPE_VS:
    $validFormat = ereg("^4[0-9]{12}([0-9]{3})?$", $this->__ccNum);
    break;
case CARD_TYPE_AX:
    $validFormat = ereg("^3[47][0-9]{13}$", $this->__ccNum);
    break;
case CARD_TYPE_DC:
    $validFormat = ereg("^3(0[0-5]|[68][0-9])[0-9]{11}$", $this->__ccNum);
    break;
case CARD_TYPE_DS:
    $validFormat = ereg("^6011[0-9]{12}$", $this->__ccNum);
    break;
case CARD_TYPE_JC:
    $validFormat = ereg("^(3[0-9]{4}|2131|1800)[0-9]{11}$", $this->__ccNum);
    break;
  default:
  // Should never be executed
  $validFormat = false;
}
// Is the number valid?
$cardNumber = strrev($this->__ccNum);
$numSum = 0;

for($i = 0; $i < strlen($cardNumber); $i++)
{
  $currentNum = substr($cardNumber, $i, 1);

// Double every second digit
if($i % 2 == 1)
{
  $currentNum *= 2;
}

// Add digits of 2-digit numbers together
if($currentNum > 9)
{
  $firstNum = $currentNum % 10;
  $secondNum = ($currentNum - $firstNum) / 10;
  $currentNum = $firstNum + $secondNum;
}

$numSum += $currentNum;
}

// If the total has no remainder it's OK
$passCheck = ($numSum % 10 == 0);
 if($validFormat && $passCheck) {return true;} else {return false;}
 }
 }
?>


<?php
//corrected
if(!isset($_POST['submit']))
{
?>

  <h2>Validate Credit Card</h2>
  <form name="frmCC" action="" method="post">
  Cardholders name: <input type="text" name="ccName" value=""><br>
  Card number: <input type="text" name="ccNum" value=""><br>
  Card type: <select name="ccType">
  <option value="1">Mastercard</option>
  <option value="2">Visa</option>
  <option value="3">Amex</option>
  <option value="4">Diners</option>
  <option value="5">Discover</option>
  <option value="6">JCB</option>
  </select><br>

  Expiry Date: <select name="ccExpM">

  <?php

    for($i = 1; $i < 13; $i++)
    { echo '<option>' . $i . '</option>'; }

  ?>

  </select>

  <select name="ccExpY">

  <?php

    for($i = 2005; $i < 2010; $i++)
    { echo '<option value="'.$i.'">' . $i . '</option>'; }

  ?>

  </select><br><br>

  <input type="submit" name="submit" value="Validate">
  </form>

  <?

  }
  else
  {
  // Check if the card is valid
  //corrected
  $cc = new CCreditCard($_POST['ccName'], $_POST['ccType'], $_POST['ccNum'], $_POST['ccExpM'], $_POST['ccExpY']);

  ?>
  <h2>Validation Results</h2>
  <b>Name: </b><?=$cc->Name(); ?><br>
  <b>Number: </b><?=$cc->SafeNumber('x', 6); ?><br>
  <b>Type: </b><?=$cc->Type(); ?><br>
  <b>Expires: </b><?=$cc->ExpiryMonth() . '/' .
  $cc->ExpiryYear(); ?><br><br>

  <?php

    echo '<font color="blue" size="2"><b>';
    if($cc->IsValid()) {echo 'VALID CARD';} else {echo 'INVALID CARD';}
    echo '</b></font>';
  }
?>
///////////////////////////////////////////////////
0
 
alextr2003frCommented:
if you doubt about <?= (deprecated) wich was in your original code then
you can replace
  <h2>Validation Results</h2>
  <b>Name: </b><?=$cc->Name(); ?><br>
  <b>Number: </b><?=$cc->SafeNumber('x', 6); ?><br>
  <b>Type: </b><?=$cc->Type(); ?><br>
  <b>Expires: </b><?=$cc->ExpiryMonth() . '/' .
  $cc->ExpiryYear(); ?><br><br>
with
  <h2>Validation Results</h2>
  <b>Name: </b><?php $cc->Name(); ?><br>
  <b>Number: </b><?php $cc->SafeNumber('x', 6); ?><br>
  <b>Type: </b><?php $cc->Type(); ?><br>
  <b>Expires: </b><?php $cc->ExpiryMonth() . '/' .
  $cc->ExpiryYear(); ?><br><br>

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now