Jump to content

PayPal IPN Script


jon4433

Recommended Posts

I'm wanting to manage the donations that I get from players and put them in a database automatically. I have the database setup.

 

I was searching on Google yesterday evening for about an hour, for a PayPal IPN Script example. And all the ones that I found and tried, never worked. So I went with the script example that PayPal gives you.

 

Now i've filled in the missing parts (I think), but i'm always getting 'Live-INVALID IPN' when I test it through the Paypal Sandbox.

 

I have

$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);

for the post back system thing (not too sure what it is). Now that sends me the Invalid IPN when I test it.

 

But when i've used both - https://www.sandbox.paypal.com/cgi-bin/webscr and ssl:/www.sandbox.paypal.com/cgi-bin/webscr, it doesn't send an email at all.

 

My concept for this script, is to allow the player to enter their username into the textbox on my donation page. Then the IPN script gets that variables, including my custom field. Then inserts it into the database.

 

If anyone is familiar with PayPal IPN, or if anyone knows what is wrong. Could you please help me out?

 

My code is below.

 

<?php 
error_reporting(E_ALL ^ E_NOTICE); 

require("connect_to_mysql.php");

// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';

$email = 'donate@dawncraftmc.com';

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);

// assign posted variables to local variables
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
$player_username = $_POST['custom'];

if (!$fp) { // HTTP ERROR 
} else { 
// NO HTTP ERROR 
fputs ($fp, $header . $req); 

while (!feof($fp)) { 
$res = fgets ($fp, 1024); 
if (strcmp ($res, "VERIFIED") == 0) { 

	if($payment_status == 'Completed'){

		$txn_id_check = mysql_query("SELECT txn_id FROM log WHERE txn_id = ".$txn_id."");
		if(mysql_num_rows($txn_id_check) != 1){
			if($receiver_email == $email){
				if($paymount_amount == '2.50' && $payment_currency == 'GBP'){
					$log_query = mysql_query("INSERT INTO log VALUES ('','".$txn_id."','".$payer_email."')");
				}
			}
		}
	}

	// If 'VERIFIED', send an email of IPN variables and values to the 
	// specified email address 
	foreach ($_POST as $key => $value){ 
	$emailtext .= $key . " = " .$value ."\n\n"; 
	} 
	mail($email, "Live-VERIFIED IPN", $req); 
} else if (strcmp ($res, "INVALID") == 0) { 
	// If 'INVALID', send an email. TODO: Log for manual investigation. 
	foreach ($_POST as $key => $value){ 
	$emailtext .= $key . " = " .$value ."\n\n"; 
	} 
	mail($email, "Live-INVALID IPN", $req); 
}	 
} 
fclose ($fp); 
}
?>

 

When I receive an email from the script, it shows like this:

 

cmd=_notify-validate&test_ipn=1&payment_type=instant&payment_date=02%3A44%3A59+Aug+09%2C+2012+PDT&payment_status=Completed&address_status=confirmed&payer_status=verified&first_name=John&last_name=Smith&payer_email=buyer%40paypalsandbox.com&payer_id=TESTBUYERID01&address_name=John+Smith&address_country=United+States&address_country_code=US&address_zip=95131&address_state=CA&address_city=San+Jose&address_street=123%2C+any+street&receiver_email=seller%40paypalsandbox.com&receiver_id=TESTSELLERID1&residence_country=US&item_name1=something&item_number1=AK-1234&quantity1=1&tax=2.02&mc_currency=USD&mc_fee=0.44&mc_gross_1=9.34&mc_handling=2.06&mc_handling1=1.67&mc_shipping=3.02&mc_shipping1=1.02&txn_type=cart&txn_id=5989944&notify_version=2.4&custom=xyz123&invoice=abc1234&charset=windows-1252&verify_sign=AYL8uQ3n1U7VgPUynFgO9oTqZNmrA8sX.RRjXmn8mL2ld2vJhQiQGR2m
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.