<?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: paypal_ipn.class.php,v 1.1.1.1 2004/09/01 04:47:18 shidairiki Exp $
//

  class paypal_ipn {
    var $info, $txn, $customer;

    function paypal_ipn($paypal_ipn_id,$languages_ids_id) {
      $this->info = array();
      $this->txn = array();
      $this->customer = array();
      $this->query($paypal_ipn_id,$languages_ids_id);
    }

    function query($paypal_ipn_id,$languages_id) {
      global $db;
      $ipn = $db->Execute("select * from " . TABLE_PAYPAL_IPN . " where paypal_ipn_id = '" . (int)$paypal_ipn_id . "'");
      $this->info = array(
        'txn_type'            => $this->get_name('txn_type_name','txn_type_id',$ipn->fields['txn_type'],$languages_id,TABLE_PAYPAL_IPN_TXN_TYPE),
        'reason_code'         => $this->get_name('reason_code_name','reason_code_id',$ipn->fields['reason_code'],$languages_id,TABLE_PAYPAL_IPN_REASON_CODE),
        'payment_type'        => $this->get_name('payment_type_name','payment_type_id',$ipn->fields['payment_type'],$languages_id,TABLE_PAYPAL_IPN_PAYMENT_TYPE),
        'payment_status'      => $this->get_name('payment_status_name','payment_status_id',$ipn->fields['payment_status'],$languages_id,TABLE_PAYPAL_IPN_PAYMENT_STATUS),
        'pending_reason'      => $this->get_name('pending_reason_name','pending_reason_id',$ipn->fields['pending_reason'],$languages_id,TABLE_PAYPAL_IPN_PAYMENT_PENDING_REASON),
        'invoice'             => $ipn->fields['invoice'],
        'mc_currency'         => $this->get_name('mc_currency_name','mc_currency_id',$ipn->fields['mc_currency'],$languages_id,TABLE_PAYPAL_IPN_MC_CURRENCY),
        'payment_date'        => $ipn->fields['payment_date'],
        'business'            => $ipn->fields['business'],
        'receiver_email'      => $ipn->fields['receiver_email'],
        'receiver_id'         => $ipn->fields['receiver_id'],
        'paypal_address_id'   => $ipn->fields['papal_address_id'],
        'txn_id'              => $ipn->fields['txn_id'],
        'notify_version'      => $ipn->fields['notify_version'],
        'verify_sign'         => $ipn->fields['verify_sign'],
        'date_added'          => $ipn->fields['date_added']);

      $ipn_orders = $db->Execute("select * from " . TABLE_PAYPAL_IPN_ORDERS . " where paypal_ipn_id = '" . (int)$paypal_ipn_id . "'");

      $ipn_memo = $db->Execute("select * from " . TABLE_PAYPAL_IPN_ORDERS_MEMO . " where paypal_ipn_id = '" . (int)$paypal_ipn_id . "'");

      $this->txn = array(
        'num_cart_items'      => $ipn_orders->fields['num_cart_items'],
        'mc_gross'            => $ipn_orders->fields['mc_gross'],
        'mc_fee'              => $ipn_orders->fields['mc_fee'],
        'payment_gross'       => $ipn_orders->fields['payment_gross'],
        'payment_fee'         => $ipn_orders->fields['payment_fee'],
        'settle_amount'       => $ipn_orders->fields['settle_amount'],
        'settle_currency'     => $ipn_orders->fields['settle_currency'],
        'exchange_rate'       => $ipn_orders->fields['exchange_rate']);

      $this->customer = array('first_name'          => $ipn->fields['first_name'],
                              'last_name'           => $ipn->fields['last_name'],
                              'payer_business_name' => $ipn->fields['payer_business_name'],
                              'address_name'        => $ipn->fields['address_name'],
                              'address_street'      => $ipn->fields['address_street'],
                              'address_city'        => $ipn->fields['address_city'],
                              'address_state'       => $ipn->fields['address_state'],
                              'address_zip'         => $ipn->fields['address_zip'],
                              'address_country'     => $ipn->fields['address_country'],
                              'address_status'      => $this->get_name('address_status_name','address_status_id',$ipn->fields['address_status'],$languages_id,TABLE_PAYPAL_IPN_PAYMENT_ADDRESS_STATUS),
                              'address_owner'       => $ipn->fields['address_owner'],
                              'payer_email'         => $ipn->fields['payer_email'],
                              'ebay_address_id'     => $ipn->fields['ebay_address_id'],
                              'payer_id'            => $ipn->fields['payer_id'],
                              'payer_status'        => $ipn->fields['payer_status'],
                              'memo'                => $ipn_memo->fields['memo']);
    }

  function get_name($column_name,$column_id,$column_value,$languages_id,$table_name) {
    global $db;
    $sql_query = $db->Execute("select ".$column_name." from " . $table_name . " where ".$column_id." = '" . (int)$column_value . " and language_id=".$languages_id."'");
    if ($sql_query->RecordCount() < 1) {
      return '';
    } else {
      return $sql_query->fields[$column_name];
    }
  }

  }//end class
?>