[PHP]PayPal IPN Script

tbh I dunno why people use paypal, they nothing but trouble.... I ran my business correctly through em and they decided to put a reserve on my account then when I jumped to paymentwall they started picking on little things I did after I requested for the reserve to be removed.

In my opinion go to Paymentwall - The All-in-One Monetization Suite its much cleaner and easier to deal with the staff are cool too.
 
tbh I dunno why people use paypal, they nothing but trouble.... I ran my business correctly through em and they decided to put a reserve on my account then when I jumped to paymentwall they started picking on little things I did after I requested for the reserve to be removed.
paypal is still the most used payment method in the web, and its better for you than using paymentwall.

In my opinion go to Paymentwall - The All-in-One Monetization Suite its much cleaner and easier to deal with the staff are cool too.
The staff is cool, indeed, the API model is even easier because u dont need to open any socket connections and all. however, paymentwall gives around 20% tax and you must fill out a tax form, you must wait 3 months after your first earn, so my opinion paymentwall is a good alternate, but not the solution at all.
 
Pretty nice I've been actually searching for this thank you for releasing very nice

10/10
 
I can't seem to get the IPN to work on sandbox.
I changed the fp from www.paypal.com to sandbox.paypal.com, but I keep getting this error from it.
"IPN delivery failed. HTTP error code 500: Internal Server Error"
 
I can't seem to get the IPN to work on sandbox.
I changed the fp from www.paypal.com to sandbox.paypal.com, but I keep getting this error from it.
"IPN delivery failed. HTTP error code 500: Internal Server Error"

Your script is causing an internal server error.
This usually means you have a bad .htaccess file, bad SQL connection, etc.

Check the error log of your web server.
 
Your script is causing an internal server error.
This usually means you have a bad .htaccess file, bad SQL connection, etc.

Check the error log of your web server.
Ah, thanks. I looked it up from the log files and it seems that there is useless ) in the code.

At line 26 there is "$payer_email = $_POST['payer_email'] );" <- Just removed the ) and it worked.
That's even in the pastebin one so I wonder why, is that just a mistake by the author.

For some reason though, the sandbox says success, but the code wont run.
This exact same code works fine when I run it alone without the IPN.
Code:
$payment_status = $_POST['payment_status'];
$transaction_id = $_POST['txn_id'];
$payer_email 	= $_POST['payer_email'];
$custom 	= $_POST['custom'];
$amount		= $_POST['mc_gross'];

if ( $payment_status == 'Completed' )
	mysql_connect("host", "user", "pass") or die(mysql_error());
	mysql_select_db("database") or die(mysql_error());
	//1 point for each 10 cents	
	$rate = '0.10';
	//Count how much to add
	$add = $amount/$rate;
	//Find users current points
	$query = mysql_query("SELECT * FROM accounts WHERE ID = '$custom'");
	while ($row = mysql_fetch_assoc($query)) {
		$curpoints = $row['points'];
	}
	//Define new points amount
	$newpoints = $curpoints+$add;
	//Update users data with new points
	mysql_query("UPDATE accounts SET points = '$newpoints' WHERE ID = '$custom'");
}

It also doesn't write the log file, like it should.
Code:
function write2LogFile( $message, $file = "mylog.txt" ) { $file = fopen($file, "a"); fputs($file, "[".date('d-m-Y')."] ".$message."\n"); fclose($file); }

EDIT: I get this error.
Code:
PHP Notice:  Undefined variable: header in path/to/file/verify.php on line 9

And here are lines 9-11
Code:
$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";
 
Last edited:
EDIT: I get this error.
Code:
PHP Notice:  [B]Undefined variable: header[/B] in path/to/file/verify.php on line 9

And here are lines 9-11
Code:
$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";

You didn't define your $header variable which you're assassing a string of characters to.

Code:
$header = "";
$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";

This should fix that, unless I'm missing something.
 
Also, take a look at this.

PayPal said:
Starting February 1, 2013, we will require all incoming requests to have a “Host” header which complies with the HTTP 1.1 specification.
This header was not required under HTTP 1.0 IPN and PDT scripts using HTTP 1.0 may start failing with “HTTP/1.0 400 Bad Request” errors after February 1, 2013, which will result in IPN messages not being validated successfully, or PDT scripts not being able to retrieve transaction information.

And the post of Gravious should fix the undefined variable message.
 
I believe the headers should be like this:
PHP:
$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";
$header .= "Host: www.paypal.com\r\n";
$header .= "Connection: close\r\n\r\n";

Be sure to change the host header to use sandbox PayPal. (Line 3)
 
Back