Link to home
Start Free TrialLog in
Avatar of Sukhwinder Singh
Sukhwinder SinghFlag for India

asked on

Paypal IPN notification twice?

I have this strange problem that for every order the notification file is called twice.
 
The notify and failure etc all urls are all different.
 
I can confirm this because I check the existance of transaction id in table if it already exists then I log it.
 
I log successul as well as unsuccessful transactions (which happen when paypal transaction id alreay exists in database).
 
Here are few of the entries from log. There are exactly two enteries for same order.
 
Payment ok 285 20/01/2008 13:17:27
transaction with order 285 not ok 20/01/2008 13:17:43

 
Payment ok 286 22/01/2008 10:58:58
transaction with order 286 not ok 22/01/2008 10:59:22

Payment ok 287 22/01/2008 14:30:14
transaction with order 287 not ok 22/01/2008 14:30:26
 
You can see that number (285, 286, 287)  is the custom id I pass to paypal. After every successful transaction the notification file is again called at around 12-15 seconds difference as you can see from time differences between two calls.
 
And this doesn't happen when I try using sandbox. I am unable to figure what is wrong.
ASKER CERTIFIED SOLUTION
Avatar of Cornelia Yoder
Cornelia Yoder
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Marcus Bointon
Marcus Bointon
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sukhwinder Singh

ASKER

Thanks for your replies:

yodercm:
generating the same id twice is not possiblet possible because it is an autoincrement field. Which is sent to paypal for processing along with cart (total amount)

  <input type="hidden" name="cmd" value="_ext-enter">
  <input type="hidden" name="redirect_cmd" value="_xclick">
  <input type="hidden" name="item_name" value="Cart">
 <input type="hidden" name="custom" value="<?php echo $order_id; ?>">
  <input type="hidden" name="no_shipping" value="1">


File which is called for notification is like this:

$paypal_ipn = new PayPalIPN($_POST);
$paypal_ipn->setPayPalReceiverEmail(get_paypal_email());
$paypal_ipn->postResponse2PayPal(); //Post back our response to PayPal
$paypal_ipn->validateTransaction();

$order_id = $paypal_ipn->paypal_post_arr['custom'];

$fp = fopen(DUMMY_PATH. "paypal.txt", "a");
 
if ($paypal_ipn->isTransactionOk())
{      
            fwrite($fp, "Payment ok $order_id ". date("d/m/Y H:i:s"));
      //ok. process payment
         $paypal_ipn->logTransactions($order_id); //log valid & invalid transactions//
      //other updations
}
else
{
      $results = print_r($paypal_ipn->paypal_post_arr, true);
      @mail('myemail@example.com', "paymnet not ok", $results);
      fwrite($fp, "\ntransaction with order $order_id not ok ". date("d/m/Y H:i:s"));
}
fclose($fp);

Squinky:
Talking too long to post back. I don't have any control over it as it has to open a socket etc. Not sure how long paypal waits before trying again.

mmarth:
Not sure about that I think I receive same values both time. And it is sending cart values not subscrption etc. But when the payment happens next time I may have to dobule-check it.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am using class written by someone else:
Here is the function which does that post back.

function postResponse2PayPal()
{
    // post back to PayPal system to validate

    $this->paypal_post_vars_in_str = "cmd=_notify-validate" . $this->paypal_post_vars_in_str;
    $header_val = "POST /cgi-bin/webscr HTTP/1.0\r\n";
    $header_val .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $header_val .= 'Content-Length: ' . strlen($this->paypal_post_vars_in_str) . "\r\n\r\n";
    //suppress socket connection error by "@"...

    $fp = @fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);
    //$fp = fsockopen ('www.sandbox.paypal.com', 80, $errno, $errstr, 30);
       
    if (!$fp)
    {  // ERROR
        $this->paypal_response = "$errstr ($errno)";
    }
    else
    {
        //just post it...
        @fputs ($fp, $header_val.$this->paypal_post_vars_in_str);
        while(!feof($fp))
        {
           
            $resp = fgets($fp, 1024);
            if (strcmp($resp, "VERIFIED") == 0)
            {
                $this->paypal_response = "VERIFIED";
            }
            else if (strcmp($resp, "INVALID") == 0)
            {
                $this->paypal_response = "INVALID";
            }

        }

    }
}/*---postResponse2PayPal()-----*/
As I have not been able to find the reason for that twice notification yet, In next week or two I'll be using the same class again on some other site and see if that problem happens on that site as well (I'll only find out when the site goes live and someone makes the purdhase as problem doesn't happen with sandbox). The products sold on that site will be rare so it will take time for the first order to come.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes I could use the simple stuff. The problem is that class is really good one. It does lots of cheks like the exitstance of any previous matching transaction id etc.

Its code is simple enough to understand. It is not the class it is just the file being called twice (IPN notification being sent twice.

I couldn't test myself by paying becuase I don't use it paypal myself and don't have any funds etc in it.

Lets see how it behaves it on new host and a new site.
Project got delayed. Getting on to this one in next few days.
The project has been completed. But it is not live yet. But doing one test transaction it seemed to be okay. I feel it might have be the first server latency which made paypal to post twice but I am not sure.
It is the same class and code being used on other server.

As I keep getting reminders from EE to follow up I am going to split the points.