Variables posting back from paypal aren't being updated by my query in the below ipn script (beneath the mail section). I get the mail but my db isn't updating the variables in $qry2. All the names and values in the insert section appear to be in synch.
//paypal writes this into a log file on my server so variables are being retrieved
ipn - Feb 21 2006 20:57:44
Vals: sales@mysite mysite.com 70250b28f383a1af84b8711bca2e68a5 1 Completed 12:57:37 Feb 21, 2006 PST 0.50 0.31 7TK80378AG402712H web_accept myname myemail@gmail.com unverified 2.0 Ai1PaghZh5FmBLCDCTQpwG8jB264AzElpk6P85tMynFIUkbCW4kg-uob\ n
//IPN script
<?php
//------------------------------------------------------------------
// Open log file (in append mode) and write the current time into it.
// Open the DB Connection. Open the actual database.
//-------------------------------------------------------------------
$log = fopen("ipn.log", "a");
fwrite($log, "\n\nipn - " . gmstrftime ("%b %d %Y %H:%M:%S", time()) . "\n");
$db = mysql_connect("", "", "");
mysql_select_db("",$db);
//------------------------------------------------
// Read post from PayPal system and create reply
// starting with: 'cmd=_notify-validate'...
// then repeating all values sent - VALIDATION.
//------------------------------------------------
$req = 'cmd=_notify-validate';
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";
$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);
$invoice = $_POST['invoice'];
$receiver_email = $_POST['receiver_email'];
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$quantity = $_POST['quantity'];
$payment_status = $_POST['payment_status'];
$pending_reason = $_POST['pending_reason'];
$payment_date = $_POST['payment_date'];
$payment_gross = $_POST['payment_gross'];
$payment_fee = $_POST['payment_fee'];
$txn_id = $_POST['txn_id'];
$txn_type = $_POST['txn_type'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$address_street = $_POST['address_street'];
$address_city = $_POST['address_city'];
$address_state = $_POST['address_state'];
$address_zip = $_POST['address_zip'];
$address_country = $_POST['address_country'];
$address_status = $_POST['address_status'];
$payer_email = $_POST['payer_email'];
$payer_status = $_POST['payer_status'];
$payer_type = $_POST['payer_type'];
$notify_version = $_POST['notify_version'];
$verify_sign = $_POST['verify_sign'];
fwrite($log, "Vals: ". $invoice." ". $receiver_email." ". $item_name." ". $item_number." ". $quantity." ". $payment_status." ". $pending_reason." ".$payment_date." ". $payment_gross." ". $payment_fee." ". $txn_id." ". $txn_type." ". $first_name." ". $last_name." ". $address_street." ". $address_city." ". $address_state . " ".$address_zip." ". $address_country." ". $address_status." ". $payer_email. " ". $payer_status." ". $payment_type." ". $notify_version." ". $verify_sign. "\ n");
//----------------------------------------------------------------------
// Check HTTP connection made to PayPal OK, If not, print an error msg
//----------------------------------------------------------------------
if (!$fp) {
echo "$errstr ($errno)";
fwrite($log, "Failed to open HTTP connection!");
$res = "FAILED";
}
//--------------------------------------------------------
// If connected OK, write the posted values back, then...
//--------------------------------------------------------
else {
fputs ($fp, $header . $req);
//-------------------------------------------
// ...read the results of the verification...
// If VERIFIED = continue to process the TX...
//-------------------------------------------
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
//----------------------------------------------------------------------
// If the payment_status=Completed... Get the password for the product
// from the DB and email it to the customer.
//----------------------------------------------------------------------
if (strcmp ($payment_status, "Completed") == 0) {
$qry = "UPDATE catalog SET pass=\"1\" WHERE session = \"$item_number\"";
$result = mysql_query($qry,$db);
//send email with session variable
$header = "From: sales@mysite.com\n";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: text/html; charset=iso-8859-1\n";
$header .= "X-Mailer: PHP / ".phpversion()."\n";
$message = "<html>\n";
$message .= "<body>\n";
$message .= "<table border=0 cellspacing=0 cellpadding=0>\n";
$message .= "<tr>\n";
$message .= "<td><img src=\"http://www.mysite.com/images/logo.gif\"><br><br>Dear ".$first_name."<br><br><b><font color=red>Your template(s) are available for download!</font></b><br><br>Thank you for choosing <a href=\"http://www.mysite.com\" target=\"_blank\">mysite.com</a> for your template needs!.<br><br>Click on the link below to download your template(s). You will be able to access your template downloads for 24 hours after initial purchase.<br><br><a href=\"http://www.mysite.com/cart/mytemplates.php?tpl=".$item_number."&fname=".$first_name."\">http://www.mysite.com/cart/mytemplates.php?tpl=".$item_number."&fname=".$first_name."</a><br><br>Your Ticket number is: ".$item_number."<br><br>Sincerely Yours,<br><br>mysite.com Staff";
$message .= "</tr>\n";
$message .= "</table>\n";
$message .= "</body>\n";
$message .= "</html>\n";
$sendto = "".$payer_email.", info@mysite.com";
$subject = "Your Template(s) are available! - mysite.com";
mail($sendto,$subject,$message,$header);
//--------------------------------------
// Insert Transaction details into DB.
//--------------------------------------
$qry2 = "INSERT into sales (invoice, receiver_email, item_name, item_number, quantity, payment_status, pending_reason, payment_date, payment_gross, payment_fee, txn_id, txn_type, first_name, last_name, address_street, address_city, address_state, address_zip, address_country, address_status, payer_email, payer_status, payment_type, notify_version , verify_sign) VALUES ( \"$invoice\", \"$receiver_email\", \"$item_name\", \"$item_number\", \"$quantity\", \"$payment_status\", \"$pending_reason\", \"$payment_date\", \"$payment_gross\", \"$payment_fee\", \"$txn_id\", \"$txn_type\", \"$first_name\", \"$last_name\", \"$address_street\", \"$address_city\", \"$address_state\", \"$address_zip \", \"$address_country\", \"$address_status\", \"$payer_email\", \"$payer_status \", \"$payment_type\", \"$notify_version\", \"$verify_sign\" )";
$result2 = mysql_query($qry2,$db);
}
//----------------------------------------------------------------------
// If the payment_status is NOT Completed... You'll have to send the
// password later, by hand, when the funds clear...
//----------------------------------------------------------------------
//} else {
//$message .= "Dear Customer,\n Thankyou for your order.\n\nThe link for the item(s) you ordered will be sent to you when the funds have cleared.\n\nThankyou
//\n\nsales\@yourdomain.com";
//mail($payer_email, "mysite.com - Your Template(s) link...", $message, "From: info@mysite.com\nReply-To:
//info@mysite.com");
//mail($receiver_email, "mysite.com - Incomplete PayPal TX...", "An incomplete transaction requires your attention.");
else {
mail($payer_email, "mysite.com - An Error Occurred...", "Dear Customer,\n an error occurred while PayPal was processing your order. It will be investigated by a human at the earliest opportunity.\n\nWe apologise for any inconvenience.", "From: info@mysite.com\nReply-To: info@mysite.com");
mail($receiver_email, "mysite.com - Invalid PayPal TX...", "An invalid transaction requires your attention.");
}
}
}
}
//-------------------------------------------
// Close PayPal Connection, Log File and DB.
//-------------------------------------------
fclose ($fp);
fclose ($log);
mysql_close($db);
?>