Hey guys!
I'm sorta new to this forum, and i've been trying to do paypal ipn many times with failed attempts i get errors like:
Parse error: syntax error, unexpected T_ELSE in /home/a3372357/public_html/donation/ipnstuff/paypalipncomplete.php on line 59
On all my else's.
I understand how paypal ipn works, but it's just my code. This is it:
<?php
mysql_connect("208.11.220.249","shane","khjhggf");
mysql_select_db("darkrp123", $con);
// The majority of the following code is a direct copy of the example code specified on the Paypal site.
// Paypal POSTs HTML FORM variables to this page
// we must post all the variables back to paypal exactly unchanged and add an extra parameter cmd with value _notify-validate
// initialise a variable with the requried cmd parameter
$req = 'cmd=_notify-validate';
// go through each of the POSTed vars and add them to the variable
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
};
// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
// In a live application send it back to www.paypal.com
// but during development you will want to uswe the paypal sandbox
// comment out one of the following lines
//$fp = fsockopen ('www.sandbox.paypal.com', 80, $errno, $errstr, 30);
$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);
// or use port 443 for an SSL connection
//$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
if (!$fp) {
// HTTP ERROR Failed to connect
// You can optionally send an email to let you know of the problem
// or add other error handling.
//email
$mail_From = "From: IPN@tester.com";
$mail_To = $email;
$mail_Subject = "HTTP ERROR";
$mail_Body = $errstr;//error string from fsockopen
mail($mail_To, $mail_Subject, $mail_Body, $mail_From);
//
// If you want to log to a file as well then uncomment the following lines
// You can use these later on in the script as well
//
// $fh = fopen("logipn.txt", 'a');//open file and create if does not exist
// fwrite($fh, "\r\n/////////////////////////////////////////\r\n HTTP ERROR \r\n");//Just for spacing in log file
//
// fwrite($fh, $errstr);//write data
// fclose($fh);//close file
};
else
{
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
// assign posted variables to local variables
// the actual variables POSTed will vary depending on your application.
// there are a huge number of possible variables that can be used. See the paypal documentation.
// the ones shown here are what is needed for a simple purchase
// a "custom" variable is available for you to pass whatever you want in it.
// if you have many complex variables to pass it is possible to use session variables to pass them.
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$item_colour = $_POST['custom'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross']; //full amount of payment. payment_gross in US
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id']; //unique transaction id
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
// use the above params to look up what the price of "item_name" should be.
$amount_they_should_have_paid = lookup_price($item_name); // you need to create this code to find out what the price for the item they bought really is so that you can check it against what they have paid. This is an anti hacker check.
// the next part is also very important from a security point of view
// you must check at the least the following...
if (($payment_status == 'Completed') && //payment_status = Completed
($receiver_email == "shaneman78@gmail.com") && // receiver_email is same as your account email
($payment_amount == $amount_they_should_have_paid ) && //check they payed what they should have
($payment_currency == "GBP") && // and its the correct currency
(!txn_id_used_before($txn_id))) { //txn_id isn't same as previous to stop duplicate payments. You will need to write a function to do this check.
// everything is ok
// you will probably want to do some processing here such as logging the purchase in a database etc
$code = mt_rand(4567899214413414352, 9867868769980098087098798897899);
// you will probably want to do some processing here such as logging the purchase in a database etc
mysql_query("INSERT INTO donation_codes (email, code, used, mc_gross) VALUES ('".$payer_email."', '".$code."', '0', '".$payment_amount."')");
$emailcode = mysql_query("SELECT 'code' FROM 'donation_codes' WHERE email='".$payer_email."'");
$mail_To = $payer_email;
$mail_Subject = "Thank you for your donation!";
$mail_Body = "Donation";
$mail_Message = '
Your donation was successful!
Your donation code is: '.$emailcode.'
Remember to read our Terms & Conditions whenever you make a donation.
Thank you,
Regards,
Intense-Gaming
';
mail($mail_To, $mail_Subject, $mail_Body);
}
else
{
$mail_To = "shaneman78@gmail.com";
$mail_Subject = "PayPal IPN status not completed or security check fail";
//
//you can put whatever debug info you want in the email
//
$mail_Body = "Something wrong. \n\nThe transaction ID number is: $txn_id \n\n Payment status = $payment_status \n\n Payment amount = $payment_amount";
mail($mail_To, $mail_Subject, $mail_Body);
}
}
elseif (strcmp ($res, "INVALID") == 0) {
//
// Paypal didnt like what we sent. If you start getting these after system was working ok in the past, check if Paypal has altered its IPN format
//
$mail_To = "shaneman78@gmail.com";
$mail_Subject = "PayPal - Invalid IPN ";
$mail_Body = "We have had an INVALID response. \n\nThe transaction ID number is: $txn_id \n\n username = $username";
mail($mail_To, $mail_Subject, $mail_Body);
}
} //end of while
fclose ($fp);
}
?>
Thank you so much!
Hope you can help me! 🙂
Regards,
Shane