Hello, I am trying to understand PayPal IPN proccess. I have built this simple script to test with the IPN simulator in the developer section of PayPal. I have tried different type of transaction status but it keeps returning VERIFIED. Can someone please explain to me how does this work? Thanks, Farris

<?php

    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'){
        update_record('users', "ad_space = '{$result}'", ' WHERE user_id = 1');
    }else{
        update_record('users', "ad_space = '{$result}'" , ' WHERE user_id = 1');
    }

?> 

For anybody who is still struggling ...
Here is what I have understood:

  1. The request type which is equal to Notify Validate, which means we want to validate that the notifaction came from PayPal.

    // Notify command
    $request = "cmd=_notify-validate";

  2. Prepare the URL to send back to PayPal to get a validation of the notification.

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

  3. Send the URL through cURL to connect to PayPal and get a response for either "VERIFIED" or "INVALID"

    // 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);

  4. If the response was Verified now you know that the notifaction was from PayPal and you can start processing.
    ** Note: All the POST variables are available just write $_POST['payment_status'] and you will get the value
    I didn't know that until now.

commented: thanks for sharing! +14
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.