<?php
/*
  $Id: paypalipn.php,v 1.2 2002/12/01 18:25:09 wilt Exp $

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

  Paypal IPN v0.96
  Copyright (c) 2002 Pablo Pasqualino
  http://www.osmosisdc.com

  Released under the GNU General Public License
*/

  class paypalipn {
    var $code, $title, $description, $enabled, $notify_url, $curl, $completed_txn_status_id, $add_shipping_to_amount, $add_tax_to_amount, $update_stock_before_payment, $allowed_currencies, $default_currency;

// class constructor
    function paypalipn() {
      $this->code = 'paypalipn';
      $this->title = MODULE_PAYMENT_PAYPALIPN_TEXT_TITLE;
      $this->description = MODULE_PAYMENT_PAYPALIPN_TEXT_DESCRIPTION;
      $this->enabled = ((MODULE_PAYMENT_PAYPALIPN_STATUS == 'True') ? true : false);
      $this->notify_url = MODULE_PAYMENT_PAYPALIPN_NOTIFY_URL;
      $this->curl = ((MODULE_PAYMENT_PAYPALIPN_CURL == 'True') ? true : false);
      $this->completed_txn_status_id = MODULE_PAYMENT_PAYPALIPN_COMPLETED_TXN_STATUS_ID;
      $this->add_shipping_to_amount = ((MODULE_PAYMENT_PAYPALIPN_ADD_SHIPPING_TO_AMOUNT == 'True') ? true : false);
      $this->add_tax_to_amount = ((MODULE_PAYMENT_PAYPALIPN_ADD_TAX_TO_AMOUNT == 'True') ? true : false);
      $this->update_stock_before_payment = ((MODULE_PAYMENT_PAYPALIPN_UPDATE_STOCK_BEFORE_PAYMENT == 'True') ? true : false);
      $this->allowed_currencies = MODULE_PAYMENT_PAYPALIPN_ALLOWED_CURRENCIES;
      $this->default_currency = MODULE_PAYMENT_PAYPALIPN_DEFAULT_CURRENCY;

      $this->form_action_url = tep_href_link(FILENAME_CHECKOUT_PAYPALIPN,'','SSL');
    }

// class methods
    function javascript_validation() {
      return false;
    }

    function selection() {
      return array('id' => $this->code,
                   'module' => $this->title);
    }

    function pre_confirmation_check() {
      return false;
    }

    function confirmation() {
      return false;
    }

    function process_button() {
	return false;
    }

    function before_process() {
      return false;
    }

    function after_process() {
	  return false;
    }

    function output_error() {
      return false;
    }

    function check() {
      if (!isset($this->_check)) {
        $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_PAYPALIPN_STATUS'");
        $this->_check = tep_db_num_rows($check_query);
      }
      return $this->_check;
    }

    function install() {
      global $language;
      if (function_exists('curl_exec')) {
        $curl_detected = 'True';
        $curl_message = '<br>cURL has been <b>DETECTED</b> in your system';
      } else {
        $curl_detected = 'False';
        $curl_message = '<br>cURL has <b>NOT</b> been <b>DETECTED</b> in your system';
      };

      $orders_status_query_raw = "select o.orders_status_id,o.orders_status_name from " . TABLE_ORDERS_STATUS . " o," . TABLE_LANGUAGES . " l where l.directory = '" . $language . "' AND l.languages_id=o.language_id AND o.orders_status_id!='99999' order by o.orders_status_id";
      $orders_status_query = tep_db_query($orders_status_query_raw);
      while ($orders_status = tep_db_fetch_array($orders_status_query)) {
        if ($orders_status[orders_status_id]==DEFAULT_ORDERS_STATUS_ID) { $if_default = ' (default)'; } else { $if_default = ''; };
        $orders_status_available .= '<b>'.$orders_status[orders_status_id].'</b>- '.$orders_status[orders_status_name].$if_default.'<br>';
      };

			$paypal_supported_currencies = "'USD','EUR','GBP','CAD','JPY'";

      $available_currencies_query = tep_db_query("select title,code,symbol_left,symbol_right from " . TABLE_CURRENCIES . " where code IN($paypal_supported_currencies) order by currencies_id");
			if (tep_db_num_rows($available_currencies_query)) {
	      while ($available_currencies = tep_db_fetch_array($available_currencies_query)) {
					$osc_allowed_currencies .= $available_currencies[code].',';
				};
				$osc_allowed_currencies = substr($osc_allowed_currencies,0,strlen($osc_allowed_currencies)-1);
			} else {
				$osc_allowed_currencies = 'USD';
			};

      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Allow PayPal IPN', 'MODULE_PAYMENT_PAYPALIPN_STATUS', 'True', 'Do you want to accept PayPal IPN payments and notifications?', '6', '1', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('PayPal ID', 'MODULE_PAYMENT_PAYPALIPN_ID', 'you@yourbusiness.com', 'Your business ID at PayPal.  Usually the email address you signed up with.  You can create a free PayPal account at <a href=\"http://www.paypal.com/\" target=\"_blank\">http://www.paypal.com</a>.', '6', '2', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Notify URL', 'MODULE_PAYMENT_PAYPALIPN_NOTIFY_URL','".HTTP_CATALOG_SERVER . DIR_WS_CATALOG."paypal_notify.php', 'Exact location in which your paypal_notify.php file resides.', '6', '3', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Use cURL', 'MODULE_PAYMENT_PAYPALIPN_CURL','" . $curl_detected . "', 'Use cURL to communicate with PayPal?" . $curl_message . "', '6', '4', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Add Shipping to Amount', 'MODULE_PAYMENT_PAYPALIPN_ADD_SHIPPING_TO_AMOUNT','False', 'Add shipping amount to order amount? (will set shipping amount to $0 in PayPal)', '6', '5', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Add Tax to Amount', 'MODULE_PAYMENT_PAYPALIPN_ADD_TAX_TO_AMOUNT','False', 'Add tax amount to order amount? (will set tax amount to $0 in PayPal)', '6', '5', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Update Stock Before Payment', 'MODULE_PAYMENT_PAYPALIPN_UPDATE_STOCK_BEFORE_PAYMENT','False', 'Should Products Stock be updated even when the payment is not yet COMPLETED?', '6', '6', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Order Status for COMPLETED Transactions', 'MODULE_PAYMENT_PAYPALIPN_COMPLETED_TXN_STATUS_ID','".DEFAULT_ORDERS_STATUS_ID."', 'Order Status ID the order will have when the Payment gets a COMPLETED status.<br>Available order_status_id:<br>".$orders_status_available."', '6', '7', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Allowed Currencies', 'MODULE_PAYMENT_PAYPALIPN_ALLOWED_CURRENCIES','$osc_allowed_currencies', 'Allowed currencies in which customers can pay.<br>Allowed by PayPal: " . str_replace('\'','',$paypal_supported_currencies) . "<br>Allowed in your shop: $osc_allowed_currencies<br>To add more currencies to your shop go to Localization->Currencies.', '6', '9', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Default Currency', 'MODULE_PAYMENT_PAYPALIPN_DEFAULT_CURRENCY','USD', 'Default currency to use when customer try to pay in a NON allowed (because of PayPal or you) currency', '6', '10', 'tep_cfg_select_option(array(\'" . str_replace(',',"\', \'",$osc_allowed_currencies) . "\'), ', now())");
    }

    function remove() {
      $keys = '';
      $keys_array = $this->keys();
      for ($i=0; $i<sizeof($keys_array); $i++) {
        $keys .= "'" . $keys_array[$i] . "',";
      }
      $keys = substr($keys, 0, -1);

      tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in (" . $keys . ")");
    }

    function keys() {
      return array('MODULE_PAYMENT_PAYPALIPN_STATUS', 'MODULE_PAYMENT_PAYPALIPN_ID', 'MODULE_PAYMENT_PAYPALIPN_NOTIFY_URL', 'MODULE_PAYMENT_PAYPALIPN_CURL', 'MODULE_PAYMENT_PAYPALIPN_ADD_SHIPPING_TO_AMOUNT', 'MODULE_PAYMENT_PAYPALIPN_ADD_TAX_TO_AMOUNT', 'MODULE_PAYMENT_PAYPALIPN_COMPLETED_TXN_STATUS_ID', 'MODULE_PAYMENT_PAYPALIPN_UPDATE_STOCK_BEFORE_PAYMENT', 'MODULE_PAYMENT_PAYPALIPN_ALLOWED_CURRENCIES', 'MODULE_PAYMENT_PAYPALIPN_DEFAULT_CURRENCY');
    }
  }
?>