Need help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tartou2
Forum Newbie
Posts: 1
Joined: Wed May 05, 2010 9:04 am

Need help

Post by tartou2 »

Hello
Can anybody help me with this paypal ipn code?
It is not working for some reason and i am trying to work on it from months but can't figure out the problem
this is the code from the purchase page and it is php:

Code: Select all

<body onLoad=\"document.paypal_form.submit();\">
<form action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\" name=\"paypal_form\">
<input type=\"hidden\" name=\"cmd\" value=\"_xclick\">
<input type=\"hidden\" name=\"business\" value=\"[my email]\">
<input type=\"hidden\" name=\"item_name\" value=\"SMS Credit Purchase\">
<input type=\"hidden\" name=\"item_number\" value=\"$no[1]\">
<input type=\"hidden\" name=\"no_note\" value=\"1\">
<input type=\"hidden\" name=\"currency_code\" value=\"USD\">
<input type=\"hidden\" name=\"return\" value=\"$full_url\">
<input type=\"hidden\" name=\"amount\" value=\"$price\">
<input type=\"hidden\" name=\"notify_url\" value=\"[my website]/paypal/ipn.php\">
</form>
And this is the code of the ipn page:

Code: Select all

<?php

$server = 'localhost';
$database = '[hidden]';
$username = '[hidden]';
$password = '[hidden]';
$connection = mysql_connect($server,$username,$password);

if (!$connection) {
die('Could not connect to MySQL database, the server return the error: '.mysql_error());
}
// $db2 = mysql_select_db($database2);
$db = @mysql_select_db($database);

if(isset($_COOKIE['username'])) 
{
$username = $_COOKIE['username'];
}
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
//Remove this line after you have debugged
//print $req;
}
// 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);
// assign posted variables to local variables
$payment_status = $_POST['payment_status'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
// email header
$em_headers  = "From: $receiver_email\n";		

$em_headers .= "Reply-To: $receiver_email\n";

$em_headers .= "Return-Path: $receiver_email\n";
if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
print "VERIFIED";
// process payment
	if ($receiver_email == "[my paypal email]" AND $payment_status == "Completed") {
	 mysql_query("UPDATE approvepaypal SET approved = 'Yes' WHERE username = '".$username."'");
	}
}
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
print "NOT VERIFIED";
mysql_query("UPDATE approvepaypal SET approved = 'Failed' WHERE username = '".$username."'") or die(mysql_error());
}
}
fclose ($fp);
}
mysql_close();
?>
I know the code still need more work to improve it and i will work on it but i just want someone to help to manage to make this ipn work and update the data in the database when someone make a purchase

Thanks
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Need help

Post by yacahuma »

may I suggest one of many websites . They go to a lot of detail explaining how it works

http://www.web-development-blog.com/arc ... aypal-ipn/
Post Reply