PDA

View Full Version : IPN and PHP



ultraspoon
15 Jun 2010, 06:28 AM
Hey Folks,

Ive set up an IPN for my website and tested it through Paypal sandbox, it seems to be working but not completly, the php script I have adds the user to the database and it should also email the buyer their password for the login. The script adds the details to the database but no email is recieved. Here is my ipn.php script.

<?php

mysql_connect("blank.blank.blank", "blank", "blank") or die(mysql_error());
mysql_select_db("blank") or die(mysql_error());

// read the post from PayPal system and add 'cmd'
$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 ('ssl://www.paypal.com', 443, $errno, $errstr, 30);

if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {

// PAYMENT VALIDATED & VERIFIED!

$email = $_POST['payer_email'];
$password = mt_rand(1000, 9999);

mysql_query("INSERT INTO users (email, password) VALUES('". mysql_escape_string($email) ."', '".md5($password)."' ) ") or die(mysql_error());

$to = $email;
$subject = 'Download Area | Login Credentials';
$message = '

Thank you for your purchase

Your account information
-------------------------
Email: '.$email.'
Password: '.$password.'
-------------------------

You can now login at blank/';
$headers = 'From:blank' . "\r\n";

mail($to, $subject, $message, $headers);
}
else if (strcmp ($res, "INVALID") == 0) {

// PAYMENT INVALID & INVESTIGATE MANUALY!

$email = $_POST['payer_email'];
$password = mt_rand(1000, 9999);

mysql_query("INSERT INTO users (email, password) VALUES('". mysql_escape_string($email) ."', '".md5($password)."' ) ") or die(mysql_error());
}
}
fclose ($fp);
}
?>

Thanks guys

Jason
15 Jun 2010, 10:31 AM
What I do in these situations is email myself at each turn to see where it is going wrong.

If this is on a hosted server why don't you check your e-mail logs to find out why an e-mail isn't being sent.

If this is on your computer, perhaps your server isn't configured to send an email