Hi,
I have completed this tutorial and I keep getting this error:
Fatal error: Call to a member function Type() on a non-object in /home/content/47/4912147/html/LIYDS/orderform.php on line 172
I even copied your code exactly and when I run that I don’t get the error but it does not print out the credit card info. The information just disappears from the form fields.
Can you tell me what I am doing wrong?
Thanks!
Here is my version of your code:
<?php //Validate credit card numbers.
define(‘CARD_TYPE_MC’, 0);
define(‘CARD_TYPE_VS’, 0);
define(‘CARD_TYPE_AX’, 0);
define(‘CARD_TYPE_DC’, 0);
define(‘CARD_TYPE_DS’, 0);
define(‘CARD_TYPE_JC’, 0);
class CCreditCard { //Class members.
var $__ctype = ‘’;
var $__cname = ‘’;
var $__cnumb = ‘’;
var $__scode = 0;
var $__xmonth = 0;
var $__xyear = 0;
//Constructor
function CCreditCard ($name, $type, $numb, $expm, $expy) {
//Set member variables
if(!empty($name)){
$this->__cname = $name;
}else {
die('Must pass a name to the constructor');
}//end IF ELSE statement
//Make sure card type is valid
switch(strtolower($type)) {
case 'mc':
case 'mastercard':
case 'm':
case '1':
$this->__ctype = CARD_TYPE_MC;
break;
case 'vs':
case 'visa':
case 'v':
case '2':
$this->__ctype = CARD_TYPE_VS;
break;
case 'ax':
case 'americanexpress':
case 'a':
case '3':
$this->__ctype = CARD_TYPE_AX;
break;
case 'dc':
case 'dinersclub':
case '4':
$this->__ctype = CARD_TYPE_DC;
break;
case 'ds':
case 'discover':
case '5':
$this->__ctype = CARD_TYPE_DS;
break;
case 'jc':
case 'jcb':
case '6':
$this->__ctype = CARD_TYPE_JC;
break;
default:
die('Invalid Type '.$type.' passed to constructor.');
break;
}//end switch
// Don't check the number yet,
// just kill all non numerics
if(!empty($numb)) {
$cardNumber = ereg_replace("[^0-9]", "", $numb);
//Make sure the card number is not empty
if(!empty($cardNumber)) {
$this->__cnumb = $cardNumber;
}else {
die('Must pass number to the constructor');
}//end nested IF statement
}//end IF statement
//Make sure expiration date is valid
if(!is_numeric($expm) || $expm < 1 || $expm > 12) {
die('Invalid expiration month of ' . $expm . ' passed to constructor');
}else {
$this->__xmonth = $expm;
}//end IF ELSE statement
//Get the current year
$currentYear = date('Y');
settype($currentYear, 'integer');
if(!is_numeric($expy) || $expy < $currentYear || $expy > ($currentYear + 10)) {
die('Invalid expiration year of ' . $expy . ' passed to constructor');
}else {
$this->__xyear = $expy;
}//end IF ELSE statement
}//end function
function Name() {
return $this->__cname;
}//end function
function Type() {
switch($this->__ctype) {
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 'Dclub [4]';
break;
case CARD_TYPE_DS:
return 'Discover [5]';
break;
case CARD_TYPE_JC:
return 'JCB [6]';
break;
default:
return 'Unknown [-1]';
}//end switch
}//end function
function Number() {
return $this->__cnumb;
}//end function
function Xmonth() {
return $this->__xmonth;
}//end function
function Xyear() {
return $this->__xyear;
}//end function
function SafeNumber($char = 'x', $numToHide = 4) {
//Return only part of the number
if($numToHide < 4) {
$numToHide = 4;
}//end IF statement
if($numToHide > 10) {
$numToHide = 10;
}//end IF statement
$cardNumber = $this->__cnumb;
$cardNumber = substr($cardNumber, 0, strlen($cardNumber) - $numToHide);
for($i=0;$i < $numToHide;$i++) {
$cardNumber .= $char;
}//end FOR loop
return $cardNumber;
}//end function
function isValid() {
//Not valid by default
$validFormat = false;
$passCheck = false;
//Is the number in the correct format?
switch($this->__ctype) {
case CARD_TYPE_MC:
$validFormat = ereg("^5[1-5][0-9]{14}$", $this->__cnumb);
break;
case CARD_TYPE_VS:
$validFormat = ereg("^4[0-9]{12}([0-9]{3})?$", $this->__cnumb);
break;
case CARD_TYPE_AX:
$validFormat = ereg("^3[47][0-9]{13}$", $this->__cnumb);
break;
case CARD_TYPE_DC:
$validFormat = ereg("^3(0[0-5]|[68][0-9])[0-9]{11}$", $this->__cnumb);
break;
case CARD_TYPE_DS:
$validFormat = ereg("^6011[0-9]{12}$", $this->__cnumb);
break;
case CARD_TYPE_JC:
$validFormat = ereg("^(3[0-9]{4}|2131|1800)[0-9]{11}$", $this->__cnumb);
break;
default:
//Should never be executed
$validFormat = false;
}//end switch
//Is the number valid?
$cardNumber = strrev($this->__cnumb);
$numSum = 0;
for($i=0;$i < $strlen($cardNumber);$i++) {
$currentNum = substr($cardNumber, $i, 1);
//Double every second number
if($i % 2 == 1) {
$currentNum *= 2;
}//end IF statement
//Add digits of 2-digit numbers together
if($currentNum > 9) {
$firstNum = $currentNum % 10;
$secondNum = ($currentNum - $firstNum) / 10;
$currentNum = $firstNum + $secondNum;
}//end IF statement
$numSum += $currentSum;
}//end FOR loop
// If the total has no remainder it's OK
$passCheck = ($numSum % 10 == 0);
if($validFormat && $passCheck) {
return true;
}else {
return false;
}//end IF ELSE statement
}//end function
}//Class ends
?>