Link to home
Start Free TrialLog in
Avatar of Resna
Resna

asked on

PayPal IPN intermittent error

Hi guys

I'm having problems with PayPals IPN.  It seems very temperamental, some times it works and other times not.

Basically all the script does is updates my database and sends an email once the payment is taken.  I'm using Fasthost to host the IPN script.

I copied the IPN code from another site, and entered my own code after the #### Payment complete enter code here #### comment and before the #### END #### comment.

What makes it hard to trouble shot is the fact it's a intermittent error.

Is there a more reliable way to code this script?

Mant thanks Resna

Code below:

<?php 
$req = 'cmd=_notify-validate'; 
foreach($_POST as $key => $value) 
{ 
    $value = urlencode(stripslashes($value)); 
    $req .= "&$key=$value"; 
} 

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

//Change the URL to www.sandbox.paypal.com when you are testing using the sandbox 
//$fp = @fsockopen("www.sandbox.paypal.com", 80, $errno, $errstr, 30); 
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);

if (!$fp)  
{ 
    echo "$errstr ($errno)"; 
    //could not connect to PayPal. Log this error. 
    //IT NEEDS TO BE FIXED ASAP!! 
} 
else  
{ 
    fputs($fp, $header . $req); 
    while (!feof($fp))  
    { 
        //Loop untill we reach the last line (which is where the treasure lies ;)) 
        $tx_status = fgets($fp); 
    }     
        
    //Values sent by paypal 
    $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']; 
    $first_name       = $_POST['first_name'];

    if (strcmp($tx_status, "VERIFIED") == 0)  
    { 
        if (strcmp($payment_status, "Completed") == 0)  
        { 
#### Payment complete enter code here ####

$connection = mysql_connect("***", "***", "***");
mysql_select_db("***", $connection);

$result = mysql_query("SELECT * FROM users WHERE email='$payer_email'", $connection) or die("error querying database");
$display = mysql_fetch_assoc($result);

$email = $display['email'];
	
$to      = $email;
$subject = "Welcome to Roissy People";
$message = "Dear " . $display['first_name'] .",

Welcome to Roissy People.

Thank you for choosing Roissy People as your online training provider. Your new online training account has been activated and is ready to use. Follow the steps below to get your training started right away.

Use the details below to access your new account.

Remember to keep this information safe.

Visit the http://roissyonlinetraining.com/ home page and click Login.
Enter your username and password:

Username: " . $display['user'] . "\n
Password: " . $display['password_temp'] . "


GET STARTED NOW  Follow our quick-start guide

Quick-Start Guide:

1. Visit the http://www.roissyonlinetraining.com/ and click the Login button

2. At the Login page enter your username and password (see above)

3. Follow the on-screen guide to access you course content.

You are will be able to access the course training content and, if applicable, any end of course examinations for 90 days from the date you first enrolled. During this time you can access the course training content as many times as you wish until you have understood and memorized the content.


End of course examinations - if applicable:

1.	You can access to the end of course examination at any time. However, you will not be able to return any of the course content once you start the examination.

2.	We recommend you do the examination in a quite area away from any other distractions.

3.	Each examination is made of a series of multiple-choice questions, to pass it you must achieve 75 percent.

4.	Upon passing you will automatically be given access to your unique certificate, which you can print off once only. Please ensure you keep your certificate in a safe place, whilst duplicates can be issued there is an additional administration fee for this duplicate certificate service.

5.	Should you fail, you can retake the examination as many times as you need to at no additional cost within 90 days from the date you first enrolled.

6.	If you have to retake the examination you will still have access to the course content prior to the examination for as long as you need to enable you to refine your knowledge. However, as before, once you begin the examination you will not have access to the course content.



Remember, if you need any additional help, our UK-based experts are on hand.

We are committed to providing first-class customer service and feature-rich solutions to help you get the most from your business and your training. We always welcome feedback; so if you have any comments or suggestions at any time, please feel free to contact us.


Best regards,

The Roissy People Team


";
mysql_query("UPDATE `users` SET password_temp='none', op='$item_number' WHERE email='$email'");
// Headers
$from    = "no_reply@roissyonlinetraining.com";
$headers = "From: $from";

mail($to, $subject, $message, $headers, "-f". $from);


#### END ####
        } 
        else  
        { 
            //Payment is PENDING. Funds will be released later. 
        } 
    } 
    elseif (strcmp($tx_status, "INVALID") == 0)   
    {         
        //Payment Failed!! 
        //This is most probably a hacking attempt. 
        //Send an email to yourself so you can investigate. 
    } 

} 

fclose($fp); 
?>

Open in new window

Avatar of neeraj523
neeraj523
Flag of India image

Are you handling situation where payment is still in pending mode status from paypal ??

You need to code a silent post page to accept subsequent statuses for the pending orders from paypal.
Avatar of Resna
Resna

ASKER

Thanks for your reply neeraj523

But no, these are not pending payment status.

Many thanks

Resna
Hello Resna

Pending status may be persisting for few seconds and when you open and see paypal account, by that time that order might get completed..

Set a notification if order status returned is pending in your code to understand this issue..

else  
        {
            //Payment is PENDING. Funds will be released later.
        }

in this else condition, you fire an email notification or someother logging to understand the issue correctly..
Avatar of Resna

ASKER

Thanks again neeraj523

Ah I see, so for a few split seconds the payment is pending so the script runs the 'Payment is PENDING' condition instead of a successful payment?

Can I just remove the below code to fix the error:

Many thnaks

Resna
else   
        {  
            //Payment is PENDING. Funds will be released later.  
        }  
    }  
    elseif (strcmp($tx_status, "INVALID") == 0)    
    {          
        //Payment Failed!!  
        //This is most probably a hacking attempt.  
        //Send an email to yourself so you can investigate.  
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of neeraj523
neeraj523
Flag of India 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