cancel
Showing results for 
Search instead for 
Did you mean: 

IPN listener always has "Completed" status

Options
cockrachecommx
Contributor
Posted on

Here is the problem, I have a listener that goes as follows: 

 

 $raw_post_data = file_get_contents('php://input');
    $raw_post_array = explode('&',$raw_post_data);
    $myPost = array();
    foreach($raw_post_array as $keyval){
        $keyval = explode("=",$keyval);
        if(count($keyval) == 2)
            $myPost[$keyval[0]] = urldecode($keyval[1]);
    }
    $req = 'cmd=_notify-validate';
    if(function_exists('get_magic_quotes_gpc')){
        $get_magic_quotes_exists = true;
    }
    foreach($myPost as $key => $value){
       if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
            $value = urlencode(stripslashes($value));
        } else {
            $value = urlencode($value);
        }
        $req .= "&$key=$value";
    }
    
    $ch = curl_init('https://www.paypal.com/cgi-bin/webscr');  
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));

        if( !($res = curl_exec($ch)) ) {
            curl_close($ch);
            exit;
        }
        curl_close($ch);

        if (strcmp ($res, "VERIFIED") == 0) {
                     
            if($_POST["payment_status"]==="Completed"){
                  /// here I have some code to do an insert in my db
            }
                   [... continues ...]

The problem is that my IPN is inserting like 5 or more regirsters in my data base, every time I do a test payment with a dollar...

I was given the advice to check if $_POST["payment_status"] was "Completed" to do my insert, and also check $_POST["txn_id"] to check if it was unique.

 

I haven't done the last option, I see that the 5 problematic inserts have the same txn_id, so I'm pretty sure the "payment_status" value is being "Completed" every time.

 

So... How could I prevent my code to insert multiple transactions with the same "txn_id"... and instead insert only ONE, but allowing me to know if the transaction was valid or not.

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.