[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PayPal IPN intermittent error

Posted on 2010-01-06
5
Medium Priority
?
527 Views
Last Modified: 2013-11-29
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

0
Comment
Question by:Resna
  • 3
  • 2
5 Comments
 
LVL 22

Expert Comment

by:neeraj523
ID: 26197268
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.
0
 

Author Comment

by:Resna
ID: 26197451
Thanks for your reply neeraj523

But no, these are not pending payment status.

Many thanks

Resna
0
 
LVL 22

Expert Comment

by:neeraj523
ID: 26197559
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..
0
 

Author Comment

by:Resna
ID: 26201364
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

0
 
LVL 22

Accepted Solution

by:
neeraj523 earned 1000 total points
ID: 26202644
Not like this. You need to handle pending status separately in your db where admin should have to go manually to update its status later once paypal start showing final status. Please note that final status may be success or reject tooo.. so dont assume that pending means success only
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Read about how to choose the best possible content marketing agency to suit your needs. Content marketing has become an integral part of running a successful tech business, so it is wise to be informed.
The online market is growing at an unprecedented rate and retail eCommerce sales are expected to reach $4 trillion by 2020. Yet, the profit is not just there for the taking because you have to set yourself apart from the competition.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
Suggested Courses

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question