Jump to content

if/else syntax error... need some guidance. **UPDATED**


classic
 Share

Recommended Posts

Hi,I am new to PHP and not sure why the following statement is giving me a syntax error on line 44:

<?phpif ($response_message=="Approved")// Receiving variables@$pfw_ip= $_SERVER['REMOTE_ADDR'];@$UserName = addslashes($_POST['UserName']);@$Password = addslashes($_POST['Password']);@$TransType = addslashes($_POST['TransType']);@$MagData = addslashes($_POST['MagData']);@$InvNum = addslashes($_POST['InvNum']);@$PNref = addslashes($_POST['PNref']);@$ExtData = addslashes($_POST['ExtData']);@$NameOnCard = addslashes($_POST['NameOnCard']);@$CardNum = addslashes($_POST['CardNum']);@$ExpDate = addslashes($_POST['ExpDate']);@$Amount = addslashes($_POST['Amount']);@$Street = addslashes($_POST['Street']);@$Zip = addslashes($_POST['Zip']);@$CVNum = addslashes($_POST['CVNum']);// Validation//Sending Email to form owner$pfw_header = "From: donations@healthtoisrael.org\n"  . "Reply-To: donations@healthtoisrael.org\n";$pfw_subject = "Donation Received!";$pfw_email_to = "donations@healthtoisrael.org";$pfw_message = "Visitor's IP: $pfw_ip\n". "UserName: $UserName\n". "Password: $Password\n". "TransType: $TransType\n". "MagData: $MagData\n". "InvNum: $InvNum\n". "PNref: $PNref\n". "ExtData: $ExtData\n". "NameOnCard: $NameOnCard\n". "Amount: $Amount\n". "Street: $Street\n". "Zip: $Zip\n". "CVNum: $CVNum\n". "";@mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ;echo "Thanks For Reserving Your Seats! Please Print this Page for your Records";elseecho "We apologize for any inconvenience. Your payment has been declined";?>

Hopefully you can see what I am trying to do (with the echo statements). Basically if the transaction is approved, echo "Thanks For Reserving Your Seats! Please Print this Page for your Records" and anything else echo "We apologize for any inconvenience. Your payment has been declined". For some reason, it is telling me the else statement is a syntax error.Thanks for any guidance.

Edited by classic
Link to comment
Share on other sites

What your error is telling you is that the else condition does not follow an if statement. How can this be?There are basically two ways to pattern an if-else structure: simple, and block-structured. Here is the simple:

if (condition)   // do something;else   // do something else;

Notice that exactly one statement follows each condition. When you want multiple statements to execute, they need to be blocked in {braces}:

if (condition){   // do something;   // and another thing;   // and, golly, even a third thing;}else{   // do something else;   // and another;}

FWIW, it's best practice always to use the block structure (braces), even if only one statement follows. It's never wrong, and it lets you add extra statements in the future with a minimum of hassle.

Edited by Deirdre's Dad
Link to comment
Share on other sites

Ah, I can see this now. Thank you so much! I'll take another look and see if I can't get it to work.

What your error is telling you is that the else condition does not follow an if statement. How can this be?There are basically two ways to pattern an if-else structure: simple, and block-structured. Here is the simple:
if (condition)   // do something;else   // do something else;

Notice that exactly one statement follows each condition. When you want multiple statements to execute, they need to be blocked in {braces}:

if (condition){   // do something;   // and another thing;   // and, golly, even a third thing;}else{   // do something else;   // and another;}

FWIW, it's best practice always to use the block structure (braces), even if only one statement follows. It's never wrong, and it lets you add extra statements in the future with a minimum of hassle.

Link to comment
Share on other sites

Okay,I thought this was working, but now regardless of the transaction message it always displays the "echo code" I put in that the payment has been declined. This happens regardless if the response message is Approved or Declined. Here is the full code for that page. Any guidance would be greatly appreciated:

<?phpif ($response_message=="APPROVED")// Receiving variables{@$pfw_ip= $_SERVER['REMOTE_ADDR'];@$UserName = addslashes($_POST['UserName']);@$Password = addslashes($_POST['Password']);@$TransType = addslashes($_POST['TransType']);@$MagData = addslashes($_POST['MagData']);@$InvNum = addslashes($_POST['InvNum']);@$PNref = addslashes($_POST['PNref']);@$ExtData = addslashes($_POST['ExtData']);@$NameOnCard = addslashes($_POST['NameOnCard']);@$CardNum = addslashes($_POST['CardNum']);@$ExpDate = addslashes($_POST['ExpDate']);@$Amount = addslashes($_POST['Amount']);@$Street = addslashes($_POST['Street']);@$Zip = addslashes($_POST['Zip']);@$CVNum = addslashes($_POST['CVNum']);// Validation//Sending Email to form owner$pfw_header = "From: donations@htl.org\n"  . "Reply-To: donations@htl.org\n";$pfw_subject = "Donation Received!";$pfw_email_to = "donations@htl.org";$pfw_message = "Visitor's IP: $pfw_ip\n". "UserName: $UserName\n". "Password: $Password\n". "TransType: $TransType\n". "MagData: $MagData\n". "InvNum: $InvNum\n". "PNref: $PNref\n". "ExtData: $ExtData\n". "NameOnCard: $NameOnCard\n". "Amount: $Amount\n". "Street: $Street\n". "Zip: $Zip\n". "CVNum: $CVNum\n". "";@mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header );echo "<b>Thanks For Reserving Your Seats! Please Print this Page as your Receipt</b><br><br>";}elseecho "<b>We apologize for any inconvenience. Your payment has been declined. Please try again! </b><br><br>";?><?php	/* 	 * This is one of the PHP file to use in comjunction with other	 * PHP sample code files to process a transaction tot he LUCY Gateway.	 * 	 * This file determines whether to use cURL or SOAP approach based on a 	 * boolean flag and then processes the resuts to display a customer receipt.	 * 	 * For questions regarding this sample code or for other code samples, 	 * contact lucydevsupport@cynergydata.com or call 866-380-0107.	 * 	 */   include 'card_txn.class.php';  $cur_txn = new CardTxn();  $useCurl = TRUE;  $read_xml = TRUE;  $MagData = "";  $PNRef = "";  $ExtData = "";  if ($useCurl == TRUE) {	$xml_result = $cur_txn->CURLTxn('Sale',$_POST['CardNum'],$_POST['ExpDate'],$MagData,$_POST['NameOnCard'],$_POST['Amount'],$_POST['InvNum'],$PNRef,$_POST['Zip'],$_POST['Street'],$_POST['CVNum'],$ExtData);	 }  else  {	$xml_result = $cur_txn->SOAPTxn('Sale',$_POST['CardNum'],$_POST['ExpDate'],$MagData,$_POST['NameOnCard'],$_POST['Amount'],$_POST['InvNum'],$PNRef,$_POST['Zip'],$_POST['Street'],$_POST['CVNum'],$ExtData);	}  //end-if for $use_curl	//print_r($xml_result);	//uncomment to view xml in page source  if ($read_xml == TRUE) {  	$reader = new XMLReader(); 	$reader->XML($xml_result);	while ($reader->read()) {	  if($reader->name != 'Response' && $reader->nodeType == XMLReader::ELEMENT )	  {		$name = $reader->name;		//echo $name .": ";	  }  	  if (in_array($reader->nodeType, array(XMLReader::TEXT, XMLReader::CDATA, XMLReader::WHITESPACE, XMLReader::SIGNIFICANT_WHITESPACE)) && $name!='')	  {		$value= $reader->value;		//echo $value ."<br>";	  }	  if(trim($reader->value) != ''){			if($name == 'Result'){ $result_code = $reader->value;}  //success = 0, any other result = error		   			if($name == 'RespMSG'){ $response_message =  $reader->value;}			if($name == 'Message'){ $message = $reader->value;}			if($name == 'PNRef'){ $pnref = $reader->value; }			if($name == 'HostCode'){ $host_code = $reader->value;}			if($name == 'AuthCode'){ $auth_code = $reader->value;}			if($name == 'GetCommercialCard'){ $commercial = $reader->value; }			if($name == 'ExtData'){ $extdata = $reader->value;}	  }   //end if	}  //end-while	$reader->close();		echo 'Credit Card Receipt' . "<br>";		echo 'Name:	' . $_POST['NameOnCard'] ."<br>";		echo 'Addr:	' . $_POST['Street'] ."<br>";		echo 'Card#:   ' . '************' . substr($_POST['CardNum'], -4) ."<br>";		echo 'Amt:	 ' . $_POST['Amount'] ."<br>";		echo 'PNRef:   ' . $pnref ."<br>";		echo 'Result:  ' . $result_code ."<br>";		echo 'RespMsg: ' . $response_message ."<br>";		echo 'Message: ' . $message ."<br>";		echo 'HostCd:  ' . $host_code ."<br>";		echo 'AuthCd:  ' . $auth_code ."<br>";		echo 'Commercial:  ' . $commercial ."<br>";		echo 'ExtData:  ' . $extdata ."<br>";	 }   else	{	 $result = explode("stdClass::__set_state(",$result);	 $hdg = $result[1];	 $body = str_replace(chr(39),"",str_replace("array(","",str_replace("))","",$result[2])));	 $body = str_replace("=>",":",$body);	 $body = str_replace(chr(44),"<br>",$body);	 echo $body;	}  ?>

Regardless of the transaction message, it displays "We apologize for any inconvenience. Your payment has been declined. Please try again!".

Edited by classic
Link to comment
Share on other sites

Here is what is displayed to the end user:----------------------------------We apologize for any inconvenience. Your payment has been declined. Please try again!Credit Card ReceiptName: testAddr:Card#: ************1114Amt: 2.00PNRef: 16505Result: 0RespMsg: ApprovedMessage: APPROVEDHostCd: 00000000AuthCd: 097435Commercial: FalseExtData: CardType=VISA,BatchNum=000000000000-------------------------Obviously we can see the test transaction was approved by the response message we get from the payment gateway. However the message displayed at the top, which I am telling to to say, is inaccurate.

Link to comment
Share on other sites

at the top of your if/else statement, what do you get for output if you add this before the start of it? i.e.

echo 'RESPONSE MESSAGE => ' . $response_message;if($response_message == 'APPROVED'){  ...}else{  .}..

Edited by thescientist
Link to comment
Share on other sites

I get:RESPONSE MESSAGE => We apologize for any inconvenience. Your payment has been declined. Please try again!Credit Card ReceiptName: testAddr:Card#: ************1114Amt: 1.50PNRef: 16512Result: 0RespMsg: ApprovedMessage: APPROVEDHostCd: 00000000AuthCd: 098905Commercial: FalseExtData: CardType=VISA,BatchNum=000000000000

Link to comment
Share on other sites

This is built from the sample code that came in the SDK for the payment gateway. There are are total of three files: donation.html (containing the form)submit_txn.php (containing the code I pasted above)card_txn.class.php (there is a call to include it in submit_txn.php)The only other place in any of the files I see that variable is in lines 115 - 140 of the above pasted code. Here is just that section:

$reader->close();		echo 'Credit Card Receipt' . "<br>";		echo 'Name:	' . $_POST['NameOnCard'] ."<br>";		echo 'Addr:	' . $_POST['Street'] ."<br>";		echo 'Card#:   ' . '************' . substr($_POST['CardNum'], -4) ."<br>";		echo 'Amt:	 ' . $_POST['Amount'] ."<br>";		echo 'PNRef:   ' . $pnref ."<br>";		echo 'Result:  ' . $result_code ."<br>";		echo 'RespMsg: ' . $response_message ."<br>";		echo 'Message: ' . $message ."<br>";		echo 'HostCd:  ' . $host_code ."<br>";		echo 'AuthCd:  ' . $auth_code ."<br>";		echo 'Commercial:  ' . $commercial ."<br>";		echo 'ExtData:  ' . $extdata ."<br>";	 }   else	{	 $result = explode("stdClass::__set_state(",$result);	 $hdg = $result[1];	 $body = str_replace(chr(39),"",str_replace("array(","",str_replace("))","",$result[2])));	 $body = str_replace("=>",":",$body);	 $body = str_replace(chr(44),"<br>",$body);	 echo $body;	}  ?>

"$response_message" does not exist in any of the other files that I have seen. Could the payment gateway declare that variable and be sending it back in response?I assume it is coming from somewhere as the response message shows later to the end user accurately:---------------Credit Card ReceiptName: testAddr:Card#: ************1114Amt: 1.50PNRef: 16512Result: 0RespMsg: Approved <---- HERE IS THE RESPONSE MESSAGE WHICH IS DYNAMICMessage: APPROVEDHostCd: 00000000AuthCd: 098905Commercial: FalseExtData: CardType=VISA,BatchNum=000000000000---------------

Link to comment
Share on other sites

Okay I did some testing and if I put the statement suggest at the end I get:We apologize for any inconvenience. Your payment has been declined. Please try again!Credit Card ReceiptName: testAddr:Card#: ************1114Amt: 1.00PNRef: 16514Result: 110RespMsg: Duplicate TransactionMessage: Duplicate transactionHostCd: 16503AuthCd:Commercial: FalseExtData: CardType=VISARESPONSE MESSAGE => Duplicate transaction (IT APPEARS TO BE WORKING HERE)Disregard the duplicate transaction message, basically it works if I put that statement at the end, rather than at the beginning. So does that mean I need to have my if/else trigger last? How to display the message at the top of the screen?

Link to comment
Share on other sites

if(trim($reader->value) != ''){ if($name == 'Result'){ $result_code = $reader->value;} //success = 0, any other result = error if($name == 'RespMSG'){ $response_message = $reader->value;} if($name == 'Message'){ $message = $reader->value;} if($name == 'PNRef'){ $pnref = $reader->value; } if($name == 'HostCode'){ $host_code = $reader->value;} if($name == 'AuthCode'){ $auth_code = $reader->value;} if($name == 'GetCommercialCard'){ $commercial = $reader->value; } if($name == 'ExtData'){ $extdata = $reader->value;} } //end if } //end-while $reader->close();======>use it here where yo get the reponse and $response_message has value intialized echo 'Credit Card Receipt' . "<br>"; .................................. ..................................

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...