PayPal IPN消息

时间:2014-11-23 21:35:01

标签: php paypal

我有一个PHP页面来收听PayPal IPN消息。

我想有第二个php监听器,只是为了获取买家的电子邮件地址和总金额,并将其添加到数据库表中。

是否可以设置多个侦听器?

这是我现在的倾听者......

    <?php

    status_header(200);

    $debug_log = "ipn_handle_debug.log"; // Debug log file name

    class paypal_ipn_handler {

    var $last_error;                 // holds the last error encountered
    var $ipn_log;                    // bool: log IPN results to text file?
    var $ipn_log_file;               // filename of the IPN log
    var $ipn_response;               // holds the IPN response from paypal
    var $ipn_data = array();         // array contains the POST values for IPN
    var $fields = array();           // array holds the fields to submit to paypal
    var $sandbox_mode = false;

    function paypal_ipn_handler()
    {
        $this->paypal_url = 'https://www.paypal.com/cgi-bin/webscr';
        $this->last_error = '';
        $this->ipn_log_file = WP_CART_PATH.'ipn_handle_debug.log';
        $this->ipn_response = '';
    }

    function validate_and_dispatch_product()
    {
        // Check Product Name , Price , Currency , Receivers email ,
        global $products,$currency,$paypal_email;
        $txn_id = $this->ipn_data['txn_id'];
        $transaction_type = $this->ipn_data['txn_type'];
        $payment_status = $this->ipn_data['payment_status'];
        $transaction_subject = $this->ipn_data['transaction_subject'];
        $custom_value_str = $this->ipn_data['custom'];
        //$this->debug_log('custom values from paypal: '.$custom_value_str,true);
        $first_name = $this->ipn_data['first_name'];
        $last_name = $this->ipn_data['last_name'];
        $buyer_email = $this->ipn_data['payer_email'];
        $street_address = $this->ipn_data['address_street'];
        $city = $this->ipn_data['address_city'];
        $state = $this->ipn_data['address_state'];
        $zip = $this->ipn_data['address_zip'];
        $country = $this->ipn_data['address_country'];
        $phone = $this->ipn_data['contact_phone'];
        $address = $street_address.", ".$city.", ".$state.", ".$zip.", ".$country;
        $custom_values = wp_cart_get_custom_var_array($custom_value_str);
        $this->debug_log('Payment Status: '.$payment_status,true);
        if($payment_status == "Completed" || $payment_status == "Processed" ){
            //We will process this notification
        }
        else{
            $this->debug_log('This is not a payment complete notification. This IPN will not be processed.',true);
            return true;
        }
        if ($transaction_type == "cart")
        {
            $this->debug_log('Transaction Type: Shopping Cart',true);
            // Cart Items
            $num_cart_items = $this->ipn_data['num_cart_items'];
            $this->debug_log('Number of Cart Items: '.$num_cart_items,true);

            $i = 1;
            $cart_items = array();
            while($i < $num_cart_items+1)
            {
                $item_number = $this->ipn_data['item_number' . $i];
                $item_name = $this->ipn_data['item_name' . $i];
                $quantity = $this->ipn_data['quantity' . $i];
                $mc_gross = $this->ipn_data['mc_gross_' . $i];
                $mc_currency = $this->ipn_data['mc_currency'];

                $current_item = array(
                    'item_number' => $item_number,
                    'item_name' => $item_name,
                    'quantity' => $quantity,
                    'mc_gross' => $mc_gross,
                    'mc_currency' => $mc_currency,
                );

                array_push($cart_items, $current_item);
                $i++;
            }
        }
        else
        {
            $cart_items = array();
            $this->debug_log('Transaction Type: Buy Now',true);
            $item_number = $this->ipn_data['item_number'];
            $item_name = $this->ipn_data['item_name'];
            $quantity = $this->ipn_data['quantity'];
            $mc_gross = $this->ipn_data['mc_gross'];
            $mc_currency = $this->ipn_data['mc_currency'];

            $current_item = array(
                'item_number' => $item_number,
                'item_name' => $item_name,
                'quantity' => $quantity,
                'mc_gross' => $mc_gross,
                'mc_currency' => $mc_currency,
            );
            array_push($cart_items, $current_item);
        }

        $product_id_array = Array();
        $product_name_array = Array();
        $product_price_array = Array();
        $attachments_array = Array();
        $download_link_array = Array();

        $payment_currency = get_option('cart_payment_currency');

        foreach ($cart_items as $current_cart_item)
        {
            $cart_item_data_num = $current_cart_item['item_number'];
            $cart_item_data_name = $current_cart_item['item_name'];
            $cart_item_data_quantity = $current_cart_item['quantity'];
            $cart_item_data_total = $current_cart_item['mc_gross'];
            $cart_item_data_currency = $current_cart_item['mc_currency'];

            $this->debug_log('Item Number: '.$cart_item_data_num,true);
            $this->debug_log('Item Name: '.$cart_item_data_name,true);
            $this->debug_log('Item Quantity: '.$cart_item_data_quantity,true);
            $this->debug_log('Item Total: '.$cart_item_data_total,true);
            $this->debug_log('Item Currency: '.$cart_item_data_currency,true);

            // Compare the values
            if ($payment_currency != $cart_item_data_currency)
            {
            $this->debug_log('Invalid Product Currency : '.$payment_currency,false);
            return false;
            }
        }        

        /*** Send notification email ***/
        //TODO
        $post_id = $custom_values['wp_cart_id'];
        $ip_address = $custom_values['ip'];
        $applied_coupon_code = $custom_values['coupon_code'];
        $currency_symbol = get_option('cart_currency_symbol');
        $this->debug_log('custom values',true);
        $this->debug_log_array($custom_values,true);
        //$this->debug_log('post id: '.$post_id,true);
        if($post_id)
        {           
            //security check
            if(!get_post_status($post_id))
            {
                $this->debug_log('Order ID '.$post_id.' does not exist in the database. This is not a Simple PayPal 

         Shopping Cart order', false);
                return;
            }

            if (get_option('wp_shopping_cart_strict_email_check') != '')
            {
                $seller_paypal_email = get_option('cart_paypal_email');
                if ($seller_paypal_email != $this->ipn_data['receiver_email']){
                    $error_msg .= 'Invalid Seller Paypal Email Address : '.$this->ipn_data['receiver_email'];
                    $this->debug_log($error_msg, false);
                    return;
                }
                else{
                    $this->debug_log('Seller Paypal Email Address is Valid: '.$this->ipn_data['receiver_email'],true);
                }
            }

            $transaction_id = get_post_meta( $post_id, 'wpsc_txn_id', true );
            if(!empty($transaction_id))
            {
                if($transaction_id == $txn_id)  //this transaction has been already processed once
                {
                    $this->debug_log('This transaction has been already processed once. Transaction ID: '.$transaction_id, false);
                    return;
                }
            }

            //End of security check

            $updated_wpsc_order = array(
                'ID'             => $post_id,
                'post_status'    => 'publish',
                'post_type'     => 'wpsc_cart_orders',
            );
            wp_update_post($updated_wpsc_order);

            update_post_meta( $post_id, 'wpsc_first_name', $first_name );
            update_post_meta( $post_id, 'wpsc_last_name', $last_name );
            update_post_meta( $post_id, 'wpsc_email_address', $buyer_email );
            update_post_meta( $post_id, 'wpsc_txn_id', $txn_id );
            $mc_gross = $this->ipn_data['mc_gross'];
            update_post_meta( $post_id, 'wpsc_total_amount', $mc_gross);
            update_post_meta( $post_id, 'wpsc_ipaddress', $ip_address );
            update_post_meta( $post_id, 'wpsc_address', $address );
            update_post_meta( $post_id, 'wpspsc_phone', $phone );
            $status = "Paid";
            update_post_meta( $post_id, 'wpsc_order_status', $status );
            update_post_meta( $post_id, 'wpsc_applied_coupon', $applied_coupon_code );
            $cart_items = get_post_meta( $post_id, 'wpsc_cart_items', true );
            $product_details = "";
            $item_counter = 1;
            $shipping = "";
            if($cart_items){
                foreach ($cart_items as $item){
                    if($item_counter != 1){
                        $product_details .= "\n";
                    }
                    $item_total = $item['price'] * $item['quantity'];
                    $product_details .= $item['name']." x ".$item['quantity']." - ".

$currency_symbol.wpspsc_number_format_price($item_total)."\n";
                    if($item['file_url']){
                        $file_url = base64_decode($item['file_url']);
                        $product_details .= "Download Link: ".$file_url."\n";
                    }
                    if(!empty($item['shipping'])){
                        $shipping += $item['shipping'] * $item['quantity'];
                    }
                    $item_counter++;
                }
            }
            if(empty($shipping)){
                $shipping = "0.00";
        }
            else{
                $baseShipping = get_option('cart_base_shipping_cost');
            $shipping = $shipping + $baseShipping;
                $shipping = wpspsc_number_format_price($shipping);
            }
            update_post_meta( $post_id, 'wpsc_shipping_amount', $shipping);
            $args = array();
            $args['product_details'] = $product_details;
            update_post_meta($post_id, 'wpspsc_items_ordered', $product_details);
            $from_email = get_option('wpspc_buyer_from_email');
            $subject = get_option('wpspc_buyer_email_subj');
            $body = get_option('wpspc_buyer_email_body');
            $args['email_body'] = $body;
            $body = wpspc_apply_dynamic_tags_on_email_body($this->ipn_data, $args);
            $headers = 'From: '.$from_email . "\r\n";
            if(!empty($buyer_email)){
                $args['payer_email'] = $buyer_email;
                if(get_option('wpspc_send_buyer_email'))
            {
                    wp_mail($buyer_email, $subject, $body, $headers);
                    $this->debug_log('Product Email successfully sent to '.$buyer_email,true);
                    update_post_meta( $post_id, 'wpsc_buyer_email_sent', 'Email sent to: '.$buyer_email);                               
                }
            }
            $notify_email = get_option('wpspc_notify_email_address');
            $seller_email_subject = get_option('wpspc_seller_email_subj');
            $seller_email_body = get_option('wpspc_seller_email_body');
            $args['email_body'] = $seller_email_body;
            $args['order_id'] = $post_id;
            $seller_email_body = wpspc_apply_dynamic_tags_on_email_body($this->ipn_data, $args);
            if(!empty($notify_email)){
                if(get_option('wpspc_send_seller_email'))
            {
                    wp_mail($notify_email, $seller_email_subject, $seller_email_body, $headers);
                    $this->debug_log('Notify Email successfully sent to '.$notify_email,true);
                }
            }
        }

        /**** Affiliate plugin integratin ****/
        $this->debug_log('Updating Affiliate Database Table with Sales Data if Using the WP Affiliate Platform 

Plugin.',true);       
        if (function_exists('wp_aff_platform_install'))
        {
            $this->debug_log('WP Affiliate Platform is installed, registering sale...',true);
            $referrer = $custom_values['ap_id'];
            $sale_amount = $this->ipn_data['mc_gross'];
            if (!empty($referrer))
            {   
                do_action('wp_affiliate_process_cart_commission', array("referrer" => $referrer, "sale_amt" => 

$sale_amount, "txn_id" => $txn_id, "buyer_email" => $buyer_email));

                $message = 'The sale has been registered in the WP Affiliates Platform Database for referrer: '.

$referrer.' for sale amount: '.$sale_amount;
                $this->debug_log($message,true);                
            }
            else
            {
                $this->debug_log('No Referrer Found. This is not an affiliate sale',true);
            }           
        }
        else
        {
            $this->debug_log('Not Using the WP Affiliate Platform Plugin.',true);
        }

        do_action('wpspc_paypal_ipn_processed',$this->ipn_data);

        return true;
    }

   function validate_ipn() 
   {
      // parse the paypal URL
      $url_parsed=parse_url($this->paypal_url);

      // generate the post string from the _POST vars aswell as load the _POST vars into an arry
      $post_string = '';
      foreach ($_POST as $field=>$value) {
         $this->ipn_data["$field"] = $value;
         $post_string .= $field.'='.urlencode(stripslashes($value)).'&';
      }

      $this->post_string = $post_string;
      $this->debug_log('Post string : '. $this->post_string,true);

      $post_string.="cmd=_notify-validate"; // append ipn command

      // open the connection to paypal
      if($this->sandbox_mode){//connect to PayPal sandbox
          $uri = 'ssl://'.$url_parsed['host'];
          $port = '443';            
          $fp = fsockopen($uri,$port,$err_num,$err_str,30);
      }
      else{//connect to live PayPal site using standard approach
        $fp = fsockopen($url_parsed['host'],"80",$err_num,$err_str,30);
      }

      if(!$fp)
      {
         // could not open the connection.  If loggin is on, the error message
         // will be in the log.
         $this->debug_log('Connection to '.$url_parsed['host']." failed. fsockopen error no. $errnum: $errstr",false);
         return false;

      }
      else
      {
         // Post the data back to paypal
         fputs($fp, "POST $url_parsed[path] HTTP/1.1\r\n");
         fputs($fp, "Host: $url_parsed[host]\r\n");
         fputs($fp, "User-Agent: Simple PayPal Shopping Cart Plugin\r\n" );
         fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
         fputs($fp, "Content-length: ".strlen($post_string)."\r\n");
         fputs($fp, "Connection: close\r\n\r\n");
         fputs($fp, $post_string . "\r\n\r\n");

         // loop through the response from the server and append to variable
         while(!feof($fp)) {
            $this->ipn_response .= fgets($fp, 1024);
         }

         fclose($fp); // close connection

         $this->debug_log('Connection to '.$url_parsed['host'].' successfuly completed.',true);
      }

      //if (eregi("VERIFIED",$this->ipn_response))
      if (strpos($this->ipn_response, "VERIFIED") !== false)// Valid IPN transaction.
      {
         $this->debug_log('IPN successfully verified.',true);
         return true;
      }
      else
      {
         // Invalid IPN transaction. Check the log for details.
         $this->debug_log('IPN validation failed.',false);
         return false;
      }
   }

   function log_ipn_results($success)
   {
      if (!$this->ipn_log) return;  // is logging turned off?

      // Timestamp
      $text = '['.date('m/d/Y g:i A').'] - ';

      // Success or failure being logged?
      if ($success) $text .= "SUCCESS!\n";
      else $text .= 'FAIL: '.$this->last_error."\n";

      // Log the POST variables
      $text .= "IPN POST Vars from Paypal:\n";
      foreach ($this->ipn_data as $key=>$value) {
         $text .= "$key=$value, ";
      }

      // Log the response from the paypal server
      $text .= "\nIPN Response from Paypal Server:\n ".$this->ipn_response;

      // Write to log
      $fp=fopen($this->ipn_log_file,'a');
      fwrite($fp, $text . "\n\n");

      fclose($fp);  // close file
   }

   function debug_log($message,$success,$end=false)
   {

      if (!$this->ipn_log) return;  // is logging turned off?

      // Timestamp
      $text = '['.date('m/d/Y g:i A').'] - '.(($success)?'SUCCESS :':'FAILURE :').$message. "\n";

      if ($end) {
        $text .= "\n------------------------------------------------------------------\n\n";
      }

      // Write to log
      $fp=fopen($this->ipn_log_file,'a');
      fwrite($fp, $text );
      fclose($fp);  // close file
   }

    function debug_log_array($array_to_write,$success,$end=false)
    {
        if (!$this->ipn_log) return;  // is logging turned off?
        $text = '['.date('m/d/Y g:i A').'] - '.(($success)?'SUCCESS :':'FAILURE :'). "\n";
        ob_start(); 
        print_r($array_to_write); 
        $var = ob_get_contents(); 
        ob_end_clean();     
        $text .= $var;

        if ($end) 
        {
            $text .= "\n------------------------------------------------------------------\n\n";
        }
        // Write to log
        $fp=fopen($this->ipn_log_file,'a');
        fwrite($fp, $text );
        fclose($fp);  // close filee
    }
}

// Start of IPN handling (script execution)
function wpc_handle_paypal_ipn()
{
    $debug_log = "ipn_handle_debug.log"; // Debug log file name    
    $ipn_handler_instance = new paypal_ipn_handler();

    $debug_enabled = false;
    $debug = get_option('wp_shopping_cart_enable_debug');
    if ($debug){
        $debug_enabled = true;
    }

    if ($debug_enabled)
    {
        echo 'Debug is enabled. Check the '.$debug_log.' file for debug output.';
        $ipn_handler_instance->ipn_log = true;
        //$ipn_handler_instance->ipn_log_file = realpath(dirname(__FILE__)).'/'.$debug_log;
    }
    $sandbox = get_option('wp_shopping_cart_enable_sandbox');
    if ($sandbox) // Enable sandbox testing
    {
        $ipn_handler_instance->paypal_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
        $ipn_handler_instance->sandbox_mode = true;
    }
    $ipn_handler_instance->debug_log('Paypal Class Initiated by '.$_SERVER['REMOTE_ADDR'],true);
    // Validate the IPN
    if ($ipn_handler_instance->validate_ipn())
    {
        $ipn_handler_instance->debug_log('Creating product Information to send.',true);
        if(!$ipn_handler_instance->validate_and_dispatch_product())
        {
            $ipn_handler_instance->debug_log('IPN product validation failed.',false);
        }
    }
    $ipn_handler_instance->debug_log('Paypal class finished.',true,true);
}

所以我的更新代码应该看起来有点像这样,在'ipn-listener.php'上我只需要阅读帖子数据? ...

    <?php

    status_header(200);

    $debug_log = "ipn_handle_debug.log"; // Debug log file name


        class paypal_ipn_handler {
    var $last_error;                 // holds the last error encountered
    var $ipn_log;                    // bool: log IPN results to text file?
    var $ipn_log_file;               // filename of the IPN log
    var $ipn_response;               // holds the IPN response from paypal
    var $ipn_data = array();         // array contains the POST values for IPN
    var $fields = array();           // array holds the fields to submit to paypal
    var $sandbox_mode = false;

    function paypal_ipn_handler()
    {
        $this->paypal_url = 'https://www.paypal.com/cgi-bin/webscr';
        $this->last_error = '';
        $this->ipn_log_file = WP_CART_PATH.'ipn_handle_debug.log';
        $this->ipn_response = '';
}
function curl_request($url, $req, $ssl)
{
     $curl_result=$curl_err='';

     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL,$url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
     curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded", "Content-Length: ".strlen($req)));
     curl_setopt($ch, CURLOPT_HEADER , 0);   
     curl_setopt($ch, CURLOPT_VERBOSE, 1);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $ssl);
     curl_setopt($ch, CURLOPT_TIMEOUT, 90);

     $curl_result = curl_exec($ch);
     $curl_err = curl_error($ch);
     curl_close($ch);

     return $curl_result;     
}

    } ////// class paypal_ipn_handler ends here






////////// A snippet of my coding ////////////////

    function send_email()
    {       

        /*** Send notification email -Sends an email and forwards the request on to the IPN listener ***/

                    wp_mail($buyer_email, $subject, $body, $headers);
                    $this->debug_log('Product Email successfully sent to '.$buyer_email,true);
                    update_post_meta( $post_id, 'wpsc_buyer_email_sent', 'Email sent to: '.$buyer_email);

                curl_request('http://www.domain.com/paypal/ipn/ipn-listener.php', $req, $ssl);

        return true;
    }
?>

****更新编码****所以我现在已经更新了我的第一个IPN监听器,在发送电子邮件时转发给我的第二个监听器,这看起来还不错吗?

现在在我的第二个IPN阅读器上,我只想阅读买家的电子邮件和费用,我可以做一些简单的事情,如$ buyeremail = $ _GET ['payer_email']?

<?php

 status_header(200);

 $debug_log = "ipn_handle_debug.log"; // Debug log file name

 class paypal_ipn_handler {

var $last_error;                 // holds the last error encountered
var $ipn_log;                    // bool: log IPN results to text file?
var $ipn_log_file;               // filename of the IPN log
var $ipn_response;               // holds the IPN response from paypal
var $ipn_data = array();         // array contains the POST values for IPN
var $fields = array();           // array holds the fields to submit to paypal
var $sandbox_mode = false;

function paypal_ipn_handler()
{
    $this->paypal_url = 'https://www.paypal.com/cgi-bin/webscr';
    $this->last_error = '';
    $this->ipn_log_file = WP_CART_PATH.'ipn_handle_debug.log';
    $this->ipn_response = '';
}

 // Curl function to send POST data to a server.
 function curl_request($url, $req, $ssl)
 {
 $curl_result=$curl_err='';

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL,$url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded", "Content-Length: ".strlen($req)));
 curl_setopt($ch, CURLOPT_HEADER , 0);   
 curl_setopt($ch, CURLOPT_VERBOSE, 1);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $ssl);
 curl_setopt($ch, CURLOPT_TIMEOUT, 90);

 $curl_result = curl_exec($ch);
 $curl_err = curl_error($ch);
 curl_close($ch);

 return $curl_result;     
 }

 / Set config fields
 $sandbox = isset($_POST['test_ipn']) ? true : false;
 $ppHost = $sandbox ? 'www.sandbox.paypal.com' : 'www.paypal.com';
 $ssl = $_SERVER['SERVER_PORT'] == '443' ? true : false;

 // Loop through POST data and generate NVP string to return back to PayPal (or forward on to secondary listeners)
 $req = '';
 foreach ($_POST as $key => $value)   
 $req .= "&$key.=".urlencode(stripslashes($value));



////////// A snippet of my coding ////////////////

    function send_email()
    {       

        /*** Send notification email -Sends an email and forwards the request on to the IPN listener ***/

                    wp_mail($buyer_email, $subject, $body, $headers);
                    $this->debug_log('Product Email successfully sent to '.$buyer_email,true);
                    update_post_meta( $post_id, 'wpsc_buyer_email_sent', 'Email sent to: '.$buyer_email);

                curl_request('http://www.domain.com/paypal/ipn/second-listener.php', $req, $ssl);

        return true;
    }
?>

2 个答案:

答案 0 :(得分:0)

这听起来像是你可以在单个IPN脚本中处理的东西,但为了回答你的问题,你可以做的是daisy-chain multiple IPN's scripts together

您基本上需要将教程中提供的curl_request()函数添加到您的IPN类中并相应地进行调整。

function curl_request($url, $req, $ssl)
{
     $curl_result=$curl_err='';

     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL,$url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
     curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded", "Content-Length: ".strlen($req)));
     curl_setopt($ch, CURLOPT_HEADER , 0);   
     curl_setopt($ch, CURLOPT_VERBOSE, 1);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $ssl);
     curl_setopt($ch, CURLOPT_TIMEOUT, 90);

     $curl_result = curl_exec($ch);
     $curl_err = curl_error($ch);
     curl_close($ch);

     return $curl_result;     
}

然后,您将从您知道将始终运行的任何其他函数中调用它,如下所示:

curl_request('http://www.domain.com/paypal/ipn/ipn-listener.php', $req, $ssl);

答案 1 :(得分:0)

是的,你可以 试试这种方式

$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]);
        }
        // read the IPN message sent from PayPal and prepend 'cmd=_notify-validate'
        $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.sandbox.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);
            common::activity_log("CURL EXECUTION", "CURL IS NOT EXECUTED");
        }
        curl_close($ch);
        // inspect IPN validation result and act accordingly
        if (strcmp($res, "VERIFIED") == 0)
        {
            // The IPN is verified, process it
            $this->load->model('order_model', 'order');
            $orderInfo = $this->order->get_record_for_field('order_key', $myPost['invoice']);
            // The IPN is verified, process it
            if (!empty($orderInfo))
            {
                $this->order->order_id   = $orderInfo['0']->order_id;
                $this->order->ipn_log    = json_encode($myPost);
                $this->order->save();
                # Log the email in database too.
                $this->load->model('Email_Log_Model', 'emails');
                $this->emails->to_email      = 'admin@gmail.com';
                $this->emails->from_email    = 'noreply@gmail.com';
                $this->emails->subject       = 'paypal valid invalid';
                $this->emails->body          = json_encode($myPost);
                $this->emails->status        = 0;
                $this->emails->save();
            }
            else
            {
                common::activity_log("INVALID ORDER KEY", "order key is not valid");
            }
        }
        else if (strcmp($res, "INVALID") == 0)
        {
            // IPN invalid, log for manual investigation
            common::activity_log("PAYPAL RESPONSE", "paypal didnt send this info");
        }