Jump to content

PayPal IPN Processor Problems - Help deseperately needed!


nevynev

Recommended Posts

Hi guys,

 

This is an IPN script which I have customised but it is not really working for me. The log file shows some progress but the entries aren't entered into the database - could it be because the sql statements are full of errors? If i just run the script normally everything seems fine but when paypal interacts with it the database queries are not carried out...

 

THESE ARE THE $_POST VALUES OUTPUT WHEN USING THE LINE "foreach ($_POST as $key => $value) {fwrite($log, "$key = $value \n");}"

 

mc_gross = 0.01

payer_id = XX37NA8XX8U8Q

tax = 0.00

payment_date = 12:16:49 Sep 20, 2007 PDT

payment_status = Completed

charset = windows-1252

first_name = THOMAS

option_selection1 = 1015

option_selection2 = 0GH3ZDOMK3BZ

mc_fee = 0.01

notify_version = 2.4

custom =

payer_status = verified

business = payment@emailaddress.com

quantity = 1

verify_sign = AwD4sJJmdrzDKNGw7KMAMuZSx1AHAxmHJRcrAyjTFoiLThiHcYXmuBfJ

payer_email = myemail@address.com

option_name1 = Member ID

option_name2 = Ref Code

txn_id = 8HU695068V7262017

payment_type = instant

payer_business_name = Key Global Online

last_name = NEVILLE

receiver_email = receiveremail@address.com

payment_fee =

receiver_id = QLJGPPPZKY5Z8

txn_type = web_accept

item_name = Fashion Platform Full Membership

mc_currency = GBP

item_number =

residence_country = GB

payment_gross =

shipping = 0.00

 

So that shows what I'm working with. Now heres the code. It may look long but most of it is a copied sql query. I'm finding it very hard to see what's going wrong as obviously Im not accessing the script, paypal is. Can you see where I'm going wrong - this is really getting to me!!

 

<?php 
include("configs/connectdb.php");
connectdb(); // include your mysql connection file

//------------------------------------------------------------------
// Open log file (in append mode) and write the current time into it.
// Open the DB Connection. Open the actual database.
//-------------------------------------------------------------------
$log = fopen("ipn.log", "a");
fwrite($log, "\n\nipn - " . gmstrftime ("%b %d %Y %H:%M:%S", time()) . "\n");
#THIS WORKS AND SHOWS THE SCRIPT IS ACCESSED BY PAYPAL.

###GET PAYMENT AMOUNT
$getsetting= "SELECT setting FROM settings WHERE name='full_membership_cost'";
$getsettingres = mysql_query($getsetting,$conn) or die(mysql_error());
$row2=mysql_fetch_array($getsettingres);
$fee = $row2['setting'];


fwrite($log, "FOREACH KEY AND VALUE: \n");

foreach ($_POST as $key => $value) {
fwrite($log, "$key = $value \n");
}

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

//---------------------------------------------
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
extract($_POST, EXTR_OVERWRITE);

if (!$fp) {
echo "$errstr ($errno)";
fwrite($log, "Failed to open HTTP connection!");
$res = "FAILED";
}


else {
fputs ($fp, $header . $req);

fwrite($log, "HTTP connection OK");

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

if (($payment_status != "Completed")
# or ($receiver_email != "payments@fashionplatform.com") or ($payment_amount != "$fee")
###REINSTATE THIS LATER!!!!!!!!!!!!!!!!!
)
{
//code to update your users data in mysql to payment status = not paid

fwrite($log, "\n NOT PAID \n");

###SEND EMAIL TO USER
	include("emails/emails.php");
	$to = $payer_email;
	$subject = $upgrade_failure_email_subject;
	$from = $upgrade_failure_email_from;
	$themessage = $upgrade_failure_email;
	mail($to, $subject, $themessage, $from);

##ADD THE DATA TO THE PAYPAL TABLE
$qry = "INSERT into 
paypal_sales 
(
invoice, 
receiver_email, 
item_name, 
item_number,
quantity,
payment_status,
pending_reason,
payment_date,
payment_gross,
payment_fee,
txn_id,
txn_type,
first_name,
last_name,
address_street,
address_city,
address_state,
address_zip,
address_country,
address_status,
payer_email,
payer_status,
payment_type,
notify_version,
verify_sign )
VALUES
(
'$invoice',
'$receiver_email',
'$item_name',
'$item_number',
'$quantity',
'$payment_status',
'$pending_reason',
'$payment_date',
'$payment_gross',
'$payment_fee',
'$txn_id',
'$txn_type',
'$first_name',
'$last_name',
'$address_street',
'$address_city',
'$address_state',
'$address_zip',
'$address_country',
'$address_status', 
'$payer_email', 
'$payer_status', 
'$payment_type', 
'$notify_version', 
'$verify_sign'
)";
$result = mysql_query($qry,$conn);
}
else
{
//code to update your users data in mysql to payment status = paid and email them etc.
##ADD THE DATA TO THE PAYPAL TABLE
fwrite($log, "\n PAID SUCCESS \n");

$qry = "INSERT into 
paypal_sales 
(
invoice, 
receiver_email, 
item_name, 
item_number,
quantity,
payment_status,
pending_reason,
payment_date,
payment_gross,
payment_fee,
txn_id,
txn_type,
first_name,
last_name,
address_street,
address_city,
address_state,
address_zip,
address_country,
address_status,
payer_email,
payer_status,
payment_type,
notify_version,
verify_sign )
VALUES
(
'$invoice',
'$receiver_email',
'$item_name',
'$item_number',
'$quantity',
'$payment_status',
'$pending_reason',
'$payment_date',
'$payment_gross',
'$payment_fee',
'$txn_id',
'$txn_type',
'$first_name',
'$last_name',
'$address_street',
'$address_city',
'$address_state',
'$address_zip',
'$address_country',
'$address_status', 
'$payer_email', 
'$payer_status', 
'$payment_type', 
'$notify_version', 
'$verify_sign'
)";

$result = mysql_query($qry,$conn);

###GET PAYMENT REF AND EMAIL ADDRESS
$getuser= "SELECT email, payment_reference FROM users WHERE id='$os0'";
$getuserres = mysql_query($getuser,$conn) or die(mysql_error());
$row=mysql_fetch_array($getuserres);
$payment_reference = $row['payment_reference'];
$email = $row['email'];

if($payment_reference == $option_selection2)
	{
	fwrite($log, "\n REFERENCES MATCH \n");
	###IF PAYMENT REFERENCE MATCHES THE ONE IN THE DATABASE UPDATE USER STATUS
	$updateuser = 
			"
			UPDATE 
			users 
			SET 
			payment = 'Upgrade Completed',
			userlevel = '2'
			WHERE 
			id='$os0'
			";
		$updateuserres =  mysql_query($updateuser);
	###SEND EMAIL TO USER
	include("emails/emails.php");
	$to = $email;
	$subject = $upgrade_success_email_subject;
	$from = $upgrade_success_email_from;
	$themessage = $upgrade_success_email;
	mail($to, $subject, $themessage, $from);

	}
else
	{
	###IF PAYMENT REFERENCE DOES NOT MATCH

	$updateuser = 
			"
			UPDATE 
			users 
			SET 
			payment = 'Upgrade Failed - Reference Did Not Match'
			WHERE 
			id='$os0'
			";
		$updateuserres =  mysql_query($updateuser);

	###SEND EMAIL TO USER
	include("emails/emails.php");
	$to = $email;
	$subject = $upgrade_failure_email_subject;
	$from = $upgrade_failure_email_from;
	$themessage = $upgrade_failure_email;
	mail($to, $subject, $themessage, $from);
	}
}
}
else if (strcmp ($res, "INVALID") == 0) {
//insert into DB in a table for bad payments for you to process later
fwrite($log, "\n BAD PAYMENT \n");
###SEND EMAIL TO USER
include("emails/emails.php");
$to = $payer_email;
$subject = $upgrade_failure_email_subject;
$from = $upgrade_failure_email_from;
$themessage = $upgrade_failure_email;
mail($to, $subject, $themessage, $from);

##ADD THE DATA TO THE PAYPAL TABLE
$qry = "INSERT into 
paypal_sales 
(
invoice, 
receiver_email, 
item_name, 
item_number,
quantity,
payment_status,
pending_reason,
payment_date,
payment_gross,
payment_fee,
txn_id,
txn_type,
first_name,
last_name,
address_street,
address_city,
address_state,
address_zip,
address_country,
address_status,
payer_email,
payer_status,
payment_type,
notify_version,
verify_sign )
VALUES
(
'$invoice',
'$receiver_email',
'$item_name',
'$item_number',
'$quantity',
'$payment_status',
'$pending_reason',
'$payment_date',
'$payment_gross',
'$payment_fee',
'$txn_id',
'$txn_type',
'$first_name',
'$last_name',
'$address_street',
'$address_city',
'$address_state',
'$address_zip',
'$address_country',
'$address_status', 
'$payer_email', 
'$payer_status', 
'$payment_type', 
'$notify_version', 
'$verify_sign'
)";
$result = mysql_query($qry,$conn);

}
}
fclose ($fp);
} 
?>

 

Thanks for ANY help!

 

NevyNev

Link to comment
Share on other sites

<?php
$qry = mysql_query("INSERT INTPO
paypal_sales 
(
invoice, 
receiver_email, 
item_name, 
item_number,
quantity,
payment_status,
pending_reason,
payment_date,
payment_gross,
payment_fee,
txn_id,
txn_type,
first_name,
last_name,
address_street,
address_city,
address_state,
address_zip,
address_country,
address_status,
payer_email,
payer_status,
payment_type,
notify_version,
verify_sign )
VALUES
(
'$invoice',
'$receiver_email',
'$item_name',
'$item_number',
'$quantity',
'$payment_status',
'$pending_reason',
'$payment_date',
'$payment_gross',
'$payment_fee',
'$txn_id',
'$txn_type',
'$first_name',
'$last_name',
'$address_street',
'$address_city',
'$address_state',
'$address_zip',
'$address_country',
'$address_status', 
'$payer_email', 
'$payer_status', 
'$payment_type', 
'$notify_version', 
'$verify_sign'
)");
?>

 

you need to do all your other queries like this

Link to comment
Share on other sites

Hi,

 

Thanks for the help so far. Have now gone through and tested mysql statements. I'm now getting wierd results in the log when I run the script myself (i.e. viewing it, not using paypal). They are as follows:

 

 

ipn - Sep 21 2007 10:09:58

FOREACH KEY AND VALUE:

 

HTTP connection OK

 

RES is HTTP/1.1 302 Found

 

 

LINE 49 SUCCESS

 

RES is Date: Fri, 21 Sep 2007 10:11:59 GMT

 

 

LINE 49 SUCCESS

 

RES is Server: Apache/1.3.33 (Unix) mod_fastcgi/2.4.2 mod_gzip/1.3.26.1a mod_ssl/2.8.22 OpenSSL/0.9.7e

 

 

LINE 49 SUCCESS

 

RES is Content-Length: 0

 

 

LINE 49 SUCCESS

 

RES is Location: https://www.paypal.com/cgi-bin/webscr

 

 

LINE 49 SUCCESS

 

RES is Connection: close

 

 

LINE 49 SUCCESS

 

RES is Content-Type: text/html; charset=iso-8859-1

 

 

LINE 49 SUCCESS

 

RES is

 

 

LINE 49 SUCCESS

 

RES is <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

 

 

LINE 49 SUCCESS

 

RES is <HTML><HEAD>

 

 

LINE 49 SUCCESS

 

RES is <TITLE>302 Found</TITLE>

 

 

LINE 49 SUCCESS

 

RES is </HEAD><BODY>

 

LINE 49 SUCCESS

 

RES is <H1>Found</H1>

 

LINE 49 SUCCESS

 

RES is The document has moved <A HREF="https://www.paypal.com/cgi-bin/webscr">here</A>.<P>

 

LINE 49 SUCCESS

 

RES is <HR>

 

LINE 49 SUCCESS

 

RES is <ADDRESS>Apache/1.3.33 Server at <A HREF="mailto:webmaster@paypal.com">www.paypal.com</A> Port 80</ADDRESS>

 

LINE 49 SUCCESS

 

RES is </BODY></HTML>

 

LINE 49 SUCCESS

 

Now I am assuming this is something to do with line 49 - will this work ok (i.e. these logs not happen) when paypal ipn accesses the script?

My only other suggestion is my if statements - I've checked through myself but can't be certain if the all stack up ok - I had to comment out line 249 ("#}") as I was getting errors - but this may have been the wrong one to comment out. A fresh pair of eyes could probably work it out in no time.

 

I have also cleaned up and changed  the code so it is easier to follow the flow...

 

<?php 
include("configs/connectdb.php");
connectdb(); // include your mysql connection file
$log = fopen("ipn.log", "a");
fwrite($log, "\n\nipn - " . gmstrftime ("%b %d %Y %H:%M:%S", time()) . "\n");
#THIS WORKS AND SHOWS THE SCRIPT IS ACCESSED BY PAYPAL.

###GET PAYMENT AMOUNT
$getsetting= "SELECT setting FROM settings WHERE name='full_membership_cost'";
$getsettingres = mysql_query($getsetting,$conn) or die(mysql_error());
$row2=mysql_fetch_array($getsettingres);
$fee = $row2['setting'];


fwrite($log, "FOREACH KEY AND VALUE: \n");

foreach ($_POST as $key => $value)
{
fwrite($log, "$key = $value \n");
}

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

//---------------------------------------------
foreach ($_POST as $key => $value)
{
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}

extract($_POST, EXTR_OVERWRITE);

if (!$fp) 
{
echo "$errstr ($errno)";
fwrite($log, "Failed to open HTTP connection!");
$res = "FAILED";
fwrite($log, "\n RES is $res \n");
}
else 
{
fputs ($fp, $header . $req);
fwrite($log, "\n HTTP connection OK \n");
}

while (!feof($fp)) 
{
$res = fgets ($fp, 1024);
fwrite($log, "\n RES is $res \n");
fwrite($log, "\n LINE 49 SUCCESS \n");

if (strcmp ($res, "VERIFIED") == 0) 
	{

	if (($payment_status != "Completed")/* or ($receiver_email != "payments@fashionplatform.com") or ($payment_amount != "$fee")###REINSTATE THIS LATER!!!!!!!!!!!!!!!!!*/)
		{
		//code to update your users data in mysql to payment status = not paid
		fwrite($log, "\n NOT PAID \n");
		###SEND EMAIL TO USER
		include("emails/emails.php");
		$to = $payer_email;
		$subject = $upgrade_failure_email_subject;
		$from = $upgrade_failure_email_from;
		$themessage = $upgrade_failure_email;
		mail($to, $subject, $themessage, $from);

		##ADD THE DATA TO THE PAYPAL TABLE
		$qry = "INSERT INTO
		paypal_sales
		SET
		mc_gross = '$mc_gross', 
		payer_id = '$payer_id',
		tax = '$tax',
		payment_date = '$payment_date',
		payment_status = '$payment_status',
		first_name = '$first_name',
		option_selection1 = '$option_selection1',
		option_selection2 = '$option_selection2', 
		mc_fee = '$mc_fee', 
		notify_version = '$notify_version', 
		payer_status = '$payer_status', 
		business = '$business',
		quantity = '$quantity',
		verify_sign = '$verify_sign',
		payer_email = '$payer_email',
		option_name1 = '$option_name1',
		option_name2 = '$option_name2',
		txn_id = '$txn_id', 
		payment_type = '$payment_type', 
		payer_business_name = '$payer_business_name',
		last_name = '$last_name',
		receiver_email = '$receiver_email',
		receiver_id = '$receiver_id',
		txn_type = '$txn_type',
		item_name = '$item_name',
		mc_currency = '$mc_currency',
		residence_country = '$residence_country',
		shipping = '$shipping'
		";
		$result = mysql_query($qry,$conn);
		}
	else
		{
		//code to update your users data in mysql to payment status = paid and email them etc.
		##ADD THE DATA TO THE PAYPAL TABLE
		fwrite($log, "\n PAID SUCCESS \n");
		$qry = "INSERT INTO
		paypal_sales
		SET
		mc_gross = '$mc_gross', 
		payer_id = '$payer_id',
		tax = '$tax',
		payment_date = '$payment_date',
		payment_status = '$payment_status',
		first_name = '$first_name',
		option_selection1 = '$option_selection1',
		option_selection2 = '$option_selection2', 
		mc_fee = '$mc_fee', 
		notify_version = '$notify_version', 
		payer_status = '$payer_status', 
		business = '$business',
		quantity = '$quantity',
		verify_sign = '$verify_sign',
		payer_email = '$payer_email',
		option_name1 = '$option_name1',
		option_name2 = '$option_name2',
		txn_id = '$txn_id', 
		payment_type = '$payment_type', 
		payer_business_name = '$payer_business_name',
		last_name = '$last_name',
		receiver_email = '$receiver_email',
		receiver_id = '$receiver_id',
		txn_type = '$txn_type',
		item_name = '$item_name',
		mc_currency = '$mc_currency',
		residence_country = '$residence_country',
		shipping = '$shipping'
		";
		$result = mysql_query($qry,$conn);

		###GET PAYMENT REF AND EMAIL ADDRESS
		$getuser= "SELECT email, payment_reference FROM users WHERE id='$option_selection1'";
		$getuserres = mysql_query($getuser,$conn) or die(mysql_error());
		$row=mysql_fetch_array($getuserres);
		$payment_reference = $row['payment_reference'];
		$email = $row['email'];

		if($payment_reference == $option_selection2)
			{
			fwrite($log, "\n REFERENCES MATCH \n");
			###IF PAYMENT REFERENCE MATCHES THE ONE IN THE DATABASE UPDATE USER STATUS
			$updateuser = 
			"
			UPDATE 
			users 
			SET 
			payment = 'Upgrade Completed',
			userlevel = '2'
			WHERE 
			id='$option_selection1'
			";
			$updateuserres =  mysql_query($updateuser);
			###SEND EMAIL TO USER
			include("emails/emails.php");
			$to = $email;
			$subject = $upgrade_success_email_subject;
			$from = $upgrade_success_email_from;
			$themessage = $upgrade_success_email;
			mail($to, $subject, $themessage, $from);
			}
		else
			{
			###IF PAYMENT REFERENCE DOES NOT MATCH
			$updateuser = 
			"
			UPDATE 
			users 
			SET 
			payment = 'Upgrade Failed - Reference Did Not Match'
			WHERE 
			id='$option_selection1'
			";
			$updateuserres =  mysql_query($updateuser);

			fwrite($log, "\n REFERENCES DO NOT MATCH!!!! \n");
			###SEND EMAIL TO USER
			include("emails/emails.php");
			$to = $email;
			$subject = $upgrade_failure_email_subject;
			$from = $upgrade_failure_email_from;
			$themessage = $upgrade_failure_email;
			mail($to, $subject, $themessage, $from);
			}
		}
	}
else if (strcmp ($res, "INVALID") == 0) 
{
//insert into DB in a table for bad payments for you to process later
fwrite($log, "\n BAD PAYMENT \n");

###SEND EMAIL TO USER
include("emails/emails.php");
$to = $payer_email;
$subject = $upgrade_failure_email_subject;
$from = $upgrade_failure_email_from;
$themessage = $upgrade_failure_email;
mail($to, $subject, $themessage, $from);

##ADD THE DATA TO THE PAYPAL TABLE
$qry = "INSERT INTO
paypal_sales
SET
mc_gross = '$mc_gross', 
payer_id = '$payer_id',
tax = '$tax',
payment_date = '$payment_date',
payment_status = '$payment_status',
first_name = '$first_name',
option_selection1 = '$option_selection1',
option_selection2 = '$option_selection2', 
mc_fee = '$mc_fee', 
notify_version = '$notify_version', 
payer_status = '$payer_status', 
business = '$business',
quantity = '$quantity',
verify_sign = '$verify_sign',
payer_email = '$payer_email',
option_name1 = '$option_name1',
option_name2 = '$option_name2',
txn_id = '$txn_id', 
payment_type = '$payment_type', 
payer_business_name = '$payer_business_name',
last_name = '$last_name',
receiver_email = '$receiver_email',
receiver_id = '$receiver_id',
txn_type = '$txn_type',
item_name = '$item_name',
mc_currency = '$mc_currency',
residence_country = '$residence_country',
shipping = '$shipping'
";
$result = mysql_query($qry,$conn);
}
}
fclose ($fp);
#} 
?>

 

Once again any help is much appreciated

 

NevyNev

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.