Page 1 of 1

Please Help! Paypal IPN

Posted: Mon Aug 07, 2006 10:48 am
by arkdm
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I've been trying to integrate Paypal's IPN service and I just can't seem to get it to work. I've tried every piece of code I can think of. Here's what I tried last:

ipn_res.php:

Code:

Code: Select all

// made by robin kohli (robin@19.5degs.com) for 19.5 Degrees (http://www.19.5degs.com)

// ----- edit these settings

// database settings
$host="&&&&";
$ln="&&&&";
$pw="&&&&";
$db="&&&&";

// paypal email
$paypal_email = "adamclay07@yahoo.com";

// email address where script should send notifications
$error_email = "casey@arkfamily.org";

// email header
$em_headers  = "From: casey\n";		
$em_headers .= "Reply-To: casey\n";
$em_headers .= "Return-Path: from_email\n";
$em_headers .= "Organization: company_name\n";
$em_headers .= "X-Priority: 3\n";


// -----------------


require("ipn_cls.php");

$paypal_info = $HTTP_POST_VARS;
$paypal_ipn = new paypal_ipn($paypal_info);

foreach ($paypal_ipn->paypal_post_vars as $key=>$value) {
	if (getType($key)=="string") {
		eval("\$$key=\$value;");
	}
}

$paypal_ipn->send_response();
$paypal_ipn->error_email = $error_email;

if (!$paypal_ipn->is_verified()) {
	$paypal_ipn->error_out("Bad order (PayPal says it's invalid)" . $paypal_ipn->paypal_response , $em_headers);
	die();
}


switch( $paypal_ipn->get_payment_status() )
{
	case 'Pending':
		
		$pending_reason=$paypal_ipn->paypal_post_vars['pending_reason'];
					
		if ($pending_reason!="intl") {
			$paypal_ipn->error_out("Pending Payment - $pending_reason", $em_headers);
			break;
		}


	case 'Completed':
		echo "Cheese n beans";
		$qry= "SELECT i.mc_gross, i.mc_currency FROM item_table as i WHERE i.item_number='$item_number'";
		mysql_connect("$host","$ln","$pw") or die("Unable to connect to database");
		mysql_select_db("$db") or die("Unable to select database");
		$res=mysql_query ($qry);
		$config=mysql_fetch_array($res);
	
		if ($paypal_ipn->paypal_post_vars['txn_type']=="reversal") {
			$reason_code=$paypal_ipn->paypal_post_vars['reason_code'];
			$paypal_ipn->error_out("PayPal reversed an earlier transaction.", $em_headers);
			// you should mark the payment as disputed now
		} else {
					
			if (
				(strtolower(trim($paypal_ipn->paypal_post_vars['business'])) == $paypal_email) && (trim($mc_currency)==$config['mc_currency']) && (trim($mc_gross)-$tax == $quantity*$config['mc_gross']) 
				) {

				$qry="INSERT INTO paypal_table VALUES (0 , '$payer_id', '$payment_date', '$txn_id', '$first_name', '$last_name', '$payer_email', '$payer_status', '$payment_type', '$memo', '$item_name', '$item_number', $quantity, $mc_gross, '$mc_currency', '$address_name', '".nl2br($address_street)."', '$address_city', '$address_state', '$address_zip', '$address_country', '$address_status', '$payer_business_name', '$payment_status', '$pending_reason', '$reason_code', '$txn_type')";
				
				
				if (mysql_query($qry)) {

					$paypal_ipn->error_out("This was a successful transaction", $em_headers);			
					// you should add your code for sending out the download link to your customer at $payer_email here.

				} else {
					$paypal_ipn->error_out("This was a duplicate transaction", $em_headers);
				} 
			} else {
				$paypal_ipn->error_out("Someone attempted a sale using a manipulated URL", $em_headers);
			}
		}
		break;
		
	case 'Failed':
		// this will only happen in case of echeck.
		$paypal_ipn->error_out("Failed Payment", $em_headers);
	break;

	case 'Denied':
		// denied payment by us
		$paypal_ipn->error_out("Denied Payment", $em_headers);
	break;

	case 'Refunded':
		// payment refunded by us
		$paypal_ipn->error_out("Refunded Payment", $em_headers);
	break;

	case 'Canceled':
		// reversal cancelled
		// mark the payment as dispute cancelled		
		$paypal_ipn->error_out("Cancelled reversal", $em_headers);
	break;

	default:
		// order is not good
		$paypal_ipn->error_out("Unknown Payment Status - " . $paypal_ipn->get_payment_status(), $em_headers);
	break;

}
ipn_cls.php

Code:

Code: Select all

class paypal_ipn
{
	var $paypal_post_vars;
	var $paypal_response;
	var $timeout;

	var $error_email;
	
	function paypal_ipn($paypal_post_vars) {
		$this->paypal_post_vars = $paypal_post_vars;
		$this->timeout = 120;
	}

	function send_response()
	{
		$fp = @fsockopen( "www.eliteweaver.co.uk", 80, &$errno, &$errstr, 120 ); 

		if (!$fp) { 
			$this->error_out("PHP fsockopen() error: " . $errstr , "");
		} else {
			foreach($this->paypal_post_vars AS $key => $value) {
				if (@get_magic_quotes_gpc()) {
					$value = stripslashes($value);
				}
				$values[] = "$key" . "=" . urlencode($value);
			}

			$response = @implode("&", $values);
			$response .= "&cmd=_notify-validate";

			fputs( $fp, "POST /cgi-bin/webscr HTTP/1.0\r\n" ); 
			fputs( $fp, "Content-type: application/x-www-form-urlencoded\r\n" ); 
			fputs( $fp, "Content-length: " . strlen($response) . "\r\n\n" ); 
			fputs( $fp, "$response\n\r" ); 
			fputs( $fp, "\r\n" );

			$this->send_time = time();
			$this->paypal_response = ""; 

			// get response from paypal
			while (!feof($fp)) { 
				$this->paypal_response .= fgets( $fp, 1024 ); 

				if ($this->send_time < time() - $this->timeout) {
					$this->error_out("Timed out waiting for a response from PayPal. ($this->timeout seconds)" , "");
				}
			}

			fclose( $fp );

		}

	}
	
	function is_verified() {
		if( ereg("VERIFIED", $this->paypal_response) )
			return true;
		else
			return false;
	} 

	function get_payment_status() {
		return $this->paypal_post_vars['payment_status'];
	}

	function error_out($message, $em_headers)
	{

		$date = date("D M j G:i:s T Y", time());
		$message .= "\n\nThe following data was received from PayPal:\n\n";

		@reset($this->paypal_post_vars);
		while( @list($key,$value) = @each($this->paypal_post_vars)) {
			$message .= $key . ':' . " \t$value\n";
		}
		mail($this->error_email, "[$date] paypay_ipn notification", $message, $em_headers);

	}
}
When Paypal submits back to my site, all I recieve is a blank page! No updated database, no updated logs, no nothing. Does anyone know any code that works for IPN, or know how to fix this? I'm desperate .

Thanks!


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Aug 08, 2006 1:42 am
by RobertGonzalez
Show just the code for what happens when Paypal returns to your site. A blank page is usually a sign of a syntax error of some sort. It would be helpful if you could narrow down that portion of code. Thanks.

Posted: Tue Aug 08, 2006 3:16 pm
by arkdm
ipn_res.php is the code used when Paypal returns to my site. It includes the other file. Does that answer your question?

Posted: Tue Aug 08, 2006 3:42 pm
by RobertGonzalez
No, I want to see the code to see if there are syntax errors.

Posted: Wed Aug 09, 2006 12:54 am
by arkdm
I posted the code in the original post before. That's all of it. I marked the files accordingly. Hope that helps.

Posted: Wed Aug 09, 2006 12:59 am
by RobertGonzalez
In the first segment of code, replace this...

Code: Select all

if (!$paypal_ipn->is_verified()) {
        $paypal_ipn->error_out("Bad order (PayPal says it's invalid)" . $paypal_ipn->paypal_response , $em_headers);
        die();
}
with this...

Code: Select all

if (!$paypal_ipn->is_verified()) {
        $paypal_ipn->error_out("Bad order (PayPal says it's invalid)" . $paypal_ipn->paypal_response , $em_headers);
        die('<h1>There was a call to die inside of the is_verified method</h1>');
}
And run the script again. This is the only place I can see where the script would be halted by the script itself, barring syntax errors.

EDIT | Depending on your PHP version, you might also want to look into changing $HTTP_POST_VARS to $_POST.