* @package initSystem
 * @copyright Copyright 2003-2009 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: init_paypal_ipn_sessions.php 14422 2009-09-13 04:42:03Z drbyte $
 */
if (!defined('IS_ADMIN_FLAG')) {
    die('Illegal Access');
}
/**
 * Begin processing.
 * Add notice to log if logging enabled.
 */
ipn_debug_email('IPN PROCESSING INITIATED. ' . "\n" . '*** Originating IP: ' . $_SERVER['REMOTE_ADDR'] . '  ' . (SESSION_IP_TO_HOST_ADDRESS == 'true' ? @gethostbyaddr($_SERVER['REMOTE_ADDR']) : '') . ($_SERVER['HTTP_USER_AGENT'] == '' ? '' : "\n" . '*** Browser/User Agent: ' . $_SERVER['HTTP_USER_AGENT']));
// need to see if we are in test mode. If so then the data is going to come in as a GET string
if (defined('MODULE_PAYMENT_PAYPAL_TESTING') && MODULE_PAYMENT_PAYPAL_TESTING == 'Test') {
    foreach ($_GET as $key => $value) {
        $_POST[$key] = $value;
    }
}
if (!$_POST) {
    ipn_debug_email('IPN FATAL ERROR :: No POST data available -- Most likely initiated by browser and not PayPal.' . "\n\n\n" . '     *** The rest of this log report can most likely be ignored !! ***' . "\n\n\n\n");
    // if ($show_all_errors) echo 'No POST data. This is not a real IPN transaction. Any "Undefined" errors below can be ignored ...<br />';
}
$session_post = isset($_POST['custom']) ? $_POST['custom'] : '=';
$session_stuff = explode('=', $session_post);
$ipnFoundSession = true;
if (!$isECtransaction && !isset($_POST['parent_txn_id']) && ipn_get_stored_session($session_stuff) === false) {
    ipn_debug_email('IPN ERROR :: No saved Website Payments Standard session data available. Must be an Express Checkout or Direct Pay transaction.' . "\n" . 'Could be a test notification, or the incoming IPN notification is not actually a bonafide PayPal transaction.' . "\n" . 'NOTE: It is likely that all the following log content is meaningless or irrelevant.');
    $ipnFoundSession = false;
}
 ipn_debug_email('Breakpoint: 3 - Communication method verified');
 /**
  * Lookup transaction history information in preparation for matching and relevant updates
  */
 $lookupData = ipn_lookup_transaction($_POST);
 $ordersID = $lookupData['order_id'];
 $paypalipnID = $lookupData['paypal_ipn_id'];
 $txn_type = $lookupData['txn_type'];
 $parentLookup = $txn_type;
 ipn_debug_email('Breakpoint: 4 - ' . 'Details:  txn_type=' . $txn_type . '    ordersID = ' . $ordersID . '  IPN_id=' . $paypalipnID . "\n\n" . '   Relevant data from POST:' . "\n     " . 'txn_type = ' . $txn_type . "\n     " . 'parent_txn_id = ' . ($_POST['parent_txn_id'] == '' ? 'None' : $_POST['parent_txn_id']) . "\n     " . 'txn_id = ' . $_POST['txn_id']);
 if (!$isECtransaction && !isset($_POST['parent_txn_id']) && $txn_type != 'cleared-echeck') {
     if (defined('MODULE_PAYMENT_PAYPAL_PDTTOKEN') && MODULE_PAYMENT_PAYPAL_PDTTOKEN != '') {
         ipn_debug_email('IPN NOTICE :: IPN pausing: waiting for PDT to process. Sleeping 10 seconds ...');
         sleep(10);
     }
     if (ipn_get_stored_session($session_stuff) === false) {
         ipn_debug_email('IPN ERROR :: No pending Website Payments Standard session data available.  Might be a duplicate transaction already entered via PDT.');
         $ipnFoundSession = false;
     }
 }
 if ($ipnFoundSession == FALSE && !$isECtransaction && !$isDPtransaction && $txn_type != 'cleared-echeck') {
     ipn_debug_email('NOTICE: IPN Processing Aborted due to missing matching transaction data, as per earlier debug message. Perhaps this transaction was already entered via PDT? Thus there is no need to process this incoming IPN notification.');
     die;
 }
 // this is used to determine whether a record needs insertion. ie: original echeck notice failed, but now we have cleared, so need parent record established:
 $new_record_needed = $txn_type == 'unique' ? true : false;
 /**
  * evaluate what type of transaction we're processing
  */
 $txn_type = ipn_determine_txn_type($_POST, $txn_type);
 ipn_debug_email('Breakpoint: 5 - Transaction type (txn_type) = ' . $txn_type . '   [parentLookup=' . $parentLookup . ']');