I am having problems installing PayPal IPN. It returns INVALID every time I process it. Is there something wrong with my code
Code:

<?php

    $db_host = 'localhost'; 
    $db_user = ''; 
    $db_password = ''; 
    $db_database = ''; 

    // Connect and select database
    $connect = mysql_connect($db_host, $db_user, $db_password);
    $select_db = mysql_select_db($db_database);

    // require_once('../includes/main.php');
    // Notify command
    $request = "cmd=_notify-validate";
    // Prepare the URL to send via cURL
    foreach ($_POST as $key => $value){
        if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()){  
            $value = urlencode(stripslashes($value)); 
        }else{ 
            $value = urlencode($value); 
        } 
        $request .= "&$key=$value"; 
    } 
    // Initial cURL
    $ch = curl_init();
    // Set opt
    curl_setopt($ch,CURLOPT_URL,"https://www.sandbox.paypal.com/cgi-bin/webscr");
    //curl_setopt($ch,CURLOPT_URL,"https://www.paypal.com");
    curl_setopt($ch,CURLOPT_POST,true);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$request);
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,false);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    // Return result
    $result = curl_exec($ch);
    // Close cURL connection
    curl_close($ch);
    // If condition
    if($result == 'VERIFIED'){
        mysql_query("INSERT INTO paypal_ipn (verification) VALUES ('{$result}')");
    }else{
        mysql_query("INSERT INTO paypal_ipn (verification) VALUES ('{$result}')");
    }
?> 

Recommended Answers

All 3 Replies

The issue must be in the values string you set up. If you're getting 'invalid' back you can be sure you're hitting the right URL and your cURL is working but you're failing the verification.
So you're need to match your query string against the API docs and any examples to see what you're doing wrong.

As an aside you're opening database connection too early. Don't open it until the curl response comes back to minimise the time you're holding a database connection open. Probably not a big deal but a good habit to get into.

Hi hericles, Thank you. How can I match the two strings. The one from my side and the one from PayPal side? I want to see if they exactly match, or see what is the problem.

I have solved the problem. It was the payment_date that was causing the problem. It was set to: Sun May 22 2016 10:15:18 GMT+0300 (Arab Standard Time). After I changed it to: Sun May 22 2016 10:15:18 GMT It worked perfectly.

But I have another question: How can I make it work with and without payment_date?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.