osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2014 osCommerce

  Released under the GNU General Public License
*/
chdir('../../../../');
require 'includes/application_top.php';
if (!defined('MODULE_PAYMENT_PAYPAL_STANDARD_STATUS') || MODULE_PAYMENT_PAYPAL_STANDARD_STATUS != 'True') {
    exit;
}
require DIR_WS_LANGUAGES . $language . '/modules/payment/paypal_standard.php';
require 'includes/modules/payment/paypal_standard.php';
$result = false;
if (isset($HTTP_POST_VARS['receiver_email']) && ($HTTP_POST_VARS['receiver_email'] == MODULE_PAYMENT_PAYPAL_STANDARD_ID || defined('MODULE_PAYMENT_PAYPAL_STANDARD_PRIMARY_ID') && tep_not_null(MODULE_PAYMENT_PAYPAL_STANDARD_PRIMARY_ID) && $HTTP_POST_VARS['receiver_email'] == MODULE_PAYMENT_PAYPAL_STANDARD_PRIMARY_ID)) {
    $paypal_standard = new paypal_standard();
    $parameters = 'cmd=_notify-validate';
    foreach ($HTTP_POST_VARS as $key => $value) {
        $parameters .= '&' . $key . '=' . urlencode(stripslashes($value));
    }
    $result = $paypal_standard->sendTransactionToGateway($paypal_standard->form_action_url, $parameters);
}
if ($result == 'VERIFIED') {
    $paypal_standard->verifyTransaction(true);
} else {
    $paypal_standard->sendDebugEmail($result, true);
}
tep_session_destroy();
require 'includes/application_bottom.php';
    foreach ($_POST as $key => $value) {
        if ($key != 'cmd') {
            $parameters .= $key . '=' . urlencode(stripslashes($value)) . '&';
        }
    }
    $parameters = substr($parameters, 0, -1);
    $result = $paypal_standard->_app->makeApiCall($paypal_standard->form_action_url, $parameters);
}
$log_params = $_POST;
$log_params['cmd'] = '_notify-validate';
foreach ($_GET as $key => $value) {
    $log_params['GET ' . $key] = $value;
}
$paypal_standard->_app->log('PS', '_notify-validate', $result == 'VERIFIED' ? 1 : -1, $log_params, $result, OSCOM_APP_PAYPAL_PS_STATUS == '1' ? 'live' : 'sandbox', true);
if ($result == 'VERIFIED') {
    $paypal_standard->verifyTransaction($_POST, true);
    $order_id = (int) $_POST['invoice'];
    $customer_id = (int) $_POST['custom'];
    $check_query = tep_db_query("select orders_status from orders where orders_id = '" . (int) $order_id . "' and customers_id = '" . (int) $customer_id . "'");
    if (tep_db_num_rows($check_query)) {
        $check = tep_db_fetch_array($check_query);
        if ($check['orders_status'] == OSCOM_APP_PAYPAL_PS_PREPARE_ORDER_STATUS_ID) {
            $new_order_status = DEFAULT_ORDERS_STATUS_ID;
            if (OSCOM_APP_PAYPAL_PS_ORDER_STATUS_ID > 0) {
                $new_order_status = OSCOM_APP_PAYPAL_PS_ORDER_STATUS_ID;
            }
            tep_db_query("update orders set orders_status = '" . (int) $new_order_status . "', last_modified = now() where orders_id = '" . (int) $order_id . "'");
            $sql_data_array = array('orders_id' => $order_id, 'orders_status_id' => (int) $new_order_status, 'date_added' => 'now()', 'customer_notified' => SEND_EMAILS == 'true' ? '1' : '0', 'comments' => '');
            tep_db_perform('orders_status_history', $sql_data_array);
            include 'includes/classes/order.php';
            $order = new order($order_id);