Kredi Kartı Tanıma

<?php
error_reporting(4);
if(!defined(PHP_CREDITCARD_CLASS))
{

define(PHP_CREDITCARD_CLASS, 1);

define(UNKNOWN, 0);
define(MASTERCARD, 1);
define(VISA, 2);
define(AMEX, 3);
define(DINNERS, 4);
define(DISCOVER, 5);
define(ENROUTE, 6);
define(JCB, 7);

define(CC_OK, 0);
define(CC_ECALL, 1);
define(CC_EARG, 2);
define(CC_ETYPE, 3);
define(CC_ENUMBER, 4);
define(CC_EFORMAT, 5);
define(CC_ECANTYPE, 6);

class creditcard
{
  var $number;
  var $type;
  var $errno;

  function creditcard()
  {
    $this->number = 0;
    $this->type = UNKNOWN;
    $this->errno = CC_OK;
  }
  function check($cardnum)
  {
    $this->number = $this->_strtonum($cardnum);

    if(!$this->detectType($this->number))
    {
      $this->errno = CC_ETYPE;
      return false;
    }

    if($this->mod10($this->number))
    {
      $this->errno = CC_ENUMBER;
      return false;
    }

    return true;
  }
  function detectType($cardnum = 0)
  {
    if($cardnum)
      $this->number = $this->_strtonum($cardnum);
    if(!$this->number)
    {
      $this->errno = CC_ECALL;
      return UNKNOWN;
    }

    if(preg_match("/^5[1-5]d{14}$/", $this->number))
      $this->type = MASTERCARD;

    else if(preg_match("/^4(d{12}|d{15})$/", $this->number))
      $this->type = VISA;

    else if(preg_match("/^3[47]d{13}$/", $this->number))
      $this->type = AMEX;

    else if(preg_match("/^[300-305]d{11}$/", $this->number) ||
      preg_match("/^3[68]d{12}$/", $this->number))
      $this->type = DINNERS;

    else if(preg_match("/^6011d{12}$/", $this->number))
      $this->type = DISCOVER;

    else if(preg_match("/^2(014|149)d{11}$/", $this->number))
      $this->type = ENROUTE;

    else if(preg_match("/^3d{15}$/", $this->number) ||
      preg_match("/^(2131|1800)d{11}$/", $this->number))
      $this->type = JCB;

    if(!$this->type)
    {
      $this->errno = CC_ECANTYPE;
      return UNKNOWN;
    }

    return $this->type;
  }
  function detectTypeString($cardnum = 0)
  {
    if(!$cardnum)
    {
      if(!$this->type)
        $this->errno = CC_EARG;
    }
    else
      $this->type = $this->detectType($cardnum);

    if(!$this->type)
    {
      $this->errno = CC_ETYPE;
      return NULL;
    }

    switch($this->type)
    {
      case MASTERCARD:
        return "MASTERCARD";
      case VISA:
        return "VISA";
      case AMEX:
        return "AMEX";
      case DINNERS:
        return "DINNERS";
      case DISCOVER:
        return "DISCOVER";
      case ENROUTE:
        return "ENROUTE";
      case JCB:
        return "JCB";
      default:
        $this->errno = CC_ECANTYPE;
        return NULL;
    }
  }

  function getCardNumber()
  {
    if(!$this->number)
    {
      $this->errno = CC_ECALL;
      return 0;
    }

    return $this->number;
  }
  function errno()
  {
    return $this->errno;
  }

  function mod10($cardnum)
  {
    for($sum=0, $i=strlen($cardnum)-1; $i >= 0; $i--)
    {
      $sum += $cardnum[$i];
      $doubdigit = "".(2 * $cardnum[--$i]);
      for($j = strlen($doubdigit)-1; $j >= 0; $j--)
        $sum += $doubdigit[$j];
    }
    return $sum % 10;
  }

  function resetCard()
  {
    $this->number = 0;
    $this->type = 0;
  }

  function strError()
  {
    switch($this->errno)
    {
      case CC_ECALL:
        return "Geçersiz Arama";
      case CC_ETYPE:
        return "Bilinmeyen Kart Tipi";
      case CC_ENUMBER:
        return "Geçersiz Kart Numarası";
      case CC_EFORMAT:
        return "Geçersiz Format";
      case CC_ECANTYPE:
        return "Kart tipi taranamadı";
      case CC_OK:
        return "Başarılı";
    }
  }

  function _strtonum($string)
  {
    for($i=0; $i< strlen($string); $i++)
    {
      if(!is_numeric($string[$i]))
        continue;
      $nstr = "$nstr".$string[$i];
    }
    return $nstr;
  }

}


}

//ÖRNEK 
$cc = new creditcard;
$card=$_GET[card];
if($card)
{
  printf("%s  nolu kart %s <br>n", $card,
    (($ccret = $cc->check($card))?" gecerli":"gecersiz"));

  if($ccret)
  {
    printf("Kart Tipi: %s veya %d<br>n", $cc->detectTypeString(),
      $cc->detectType());
    printf("Kart Numarasi: %s<br>n", $cc->getCardNumber());
  }
  else
    printf("%s<br>n", $cc->strError());

}

?>

<form action="" method=get>
<input type=text name=card size=20><p>
<input type=submit value="Hesapla">
</form>

</body>

 

Sizin Değerli Görüşlerinize İhtiyacımız Var.