0

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');
    }

?> 
1
Contributor
1
Reply
14
Views
1 Year
Discussion Span
Last Post by FarrisFahad
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.

Votes + Comments
thanks for sharing!
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.