<?php
//
// +----------------------------------------------------------------------+
// |zen-cart Open Source E-commerce                                       |
// +----------------------------------------------------------------------+
// | Copyright (c) 2004 The zen-cart developers                           |
// |                                                                      |
// | http://www.zen-cart.com/index.php                                    |
// |                                                                      |
// | Portions Copyright (c) 2003 osCommerce                               |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the GPL license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at the following url:           |
// | http://www.zen-cart.com/license/2_0.txt.                             |
// | If you did not receive a copy of the zen-cart license and are unable |
// | to obtain it through the world-wide-web, please send a note to       |
// | license@zen-cart.com so we can mail you a copy immediately.          |
// +----------------------------------------------------------------------+
// $Id: ipn.php,v 1.1.1.1 2004/09/01 04:46:54 shidairiki Exp $
//

  unset($_GET,$HTTP_GET_VARS,$HTTP_POST_VARS);
  if (!isset($_GET['main_page'])) $_GET['main_page'] = '';

  //Continue the session
  $_POST['zenid'] = $_POST['custom'];
  //Start up osC
  require ('includes/application_top.php');

  require_once(DIR_WS_CLASSES . 'paypal/ipn.class.php');
  require_once(DIR_WS_LANGUAGES . $_SESSION['language'] . '/modules/payment/paypal.php');
  require_once(DIR_WS_LANGUAGES . $_SESSION['language'] . '/checkout_process.php');

  $ipn = new paypal_ipn(MODULE_PAYMENT_PAYPAL_IPN_DEBUG_EMAIL,$_POST,zen_session_name(),MODULE_PAYMENT_PAYPAL_IPN_DEBUG);
  unset($_POST,$HTTP_POST_VARS);


  //post back to PayPal system to validate
  //For a sample VALID IPN view PayPal's source page prior to returning checkout_success.php
  //If the IPN is not valid then don't do anything, set 'paypal_debug' to non-zero if
  //you want to investigate these occurances.
  if(!$ipn->authenticate('www.paypal.com') && !MODULE_PAYMENT_PAYPAL_IPN_DEBUG_TEST) exit;

  //Check that the receiver_email is your Primary PayPal email
  if (!$ipn->validate_receiver_email(MODULE_PAYMENT_PAYPAL_ID,MODULE_PAYMENT_PAYPAL_BUSINESS_ID) && !MODULE_PAYMENT_PAYPAL_IPN_DEBUG_TEST) exit;

  //Check the payment_status is Completed
  if ( $ipn->key['payment_status'] != 'Completed' && !MODULE_PAYMENT_PAYPAL_IPN_DEBUG_TEST) exit;


  //Check that txn_id has not been previously processed
  //NOTE: This disable notifications on refunds/reversals(?) with same transaction id's
  if (!$ipn->unique_txn_id() && !MODULE_PAYMENT_PAYPAL_IPN_DEBUG_TEST) exit;


  //Process payment
  if ( $ipn->txn_type() == 'web_accept' || $ipn->txn_type() == 'cart') {


    require_once DIR_WS_CLASSES . 'paypal/cart.class.php';
    $ipn_cart = new paypal_ipn_cart();
    //check that payment_amount/payment_currency are correct
    if ($ipn_cart->valid_payment($ipn,$cart,$currency) ) { //needs work
         $ipn->send_email(PAYMENT_CART_DESCRIPTION,' validate point 8');
      $description = sprintf(PAYMENT_CART_DESCRIPTION_MSG,number_format($ipn->key['mc_gross'],2),$ipn->key['mc_currency'],$ipn->key['first_name'],$ipn->key['last_name']);
      if (MODULE_PAYMENT_PAYPAL_IPN_DEBUG > 1) $ipn->send_email(PAYMENT_CART_DESCRIPTION, $description);
      unset($description);
      $ipn->insert_ipn_txn();
///////////////////////////////////////////////////////////////////////////////////////////////////
//                            BEGIN CHECKOUT_PROCESS                                             //
///////////////////////////////////////////////////////////////////////////////////////////////////
$full_ipn = true;
require(DIR_WS_MODULES . 'checkout_process.php');
///////////////////////////////////////////////////////////////////////////////////////////////////
//                            END CHECKOUT_PROCESS                                               //
///////////////////////////////////////////////////////////////////////////////////////////////////
    } else {
        if (MODULE_PAYMENT_PAYPAL_IPN_DEBUG) {
          $description = sprintf(PAYMENT_CART_DESCRIPTION_MSG,number_format($ipn->key['mc_gross'],2),$ipn->key['mc_currency'],$ipn->key['first_name'],$ipn->key['last_name']);
          $ipn->send_email(PAYMENT_CART_DESCRIPTION,$description);
          unset($description);
        }
    }
  } else if ($ipn->txn_type() == 'send_money') {
    $description = sprintf(PAYMENT_SEND_MONEY_DESCRIPTION_MSG,number_format($ipn->key['mc_gross'],2),$ipn->key['mc_currency']);
    if (MODULE_PAYMENT_PAYPAL_IPN_DEBUG) $ipn->send_email(PAYMENT_SEND_MONEY_DESCRIPTION,$description);
    unset($description);
  } else if ($ipn->txn_type() == 'reversal') {
    $description = sprintf(PAYMENT_REVERSAL_DESCRIPTION_MSG,number_format($ipn->key['mc_gross'],2),$ipn->key['mc_currency']);
    if (MODULE_PAYMENT_PAYPAL_IPN_DEBUG) $ipn->send_email(PAYMENT_REVERSAL_DESCRIPTION,$description);
    unset($description);
  } else {
    if (MODULE_PAYMENT_PAYPAL_IPN_DEBUG) $ipn->send_email(PAYMENT_UNKNOWN_DESCRIPTION,PAYMENT_UNKNOWN_DESCRIPTION);
  }
  if (MODULE_PAYMENT_PAYPAL_IPN_DEBUG_TEST) {
      echo $ipn->debug_info();
  }
  require(DIR_WS_INCLUDES . 'application_bottom.php');
?>