<?php
ob_start();
session_start();
include("include/db.inc.php");
include("include/bit_functions.inc.php");
include("include/mail.php");
$db1=new db;
$db1->open();


//************google checkout*****************//

 require_once('library/googleresponse.php');
 require_once('library/googlemerchantcalculations.php');
 require_once('library/googleresult.php');
 require_once('library/googlerequest.php');

//*********************************************//

  define('RESPONSE_HANDLER_ERROR_LOG_FILE', 'googleerror.log');
  define('RESPONSE_HANDLER_LOG_FILE', 'googlemessage.log');



  $google_pay = mysql_fetch_array(mysql_query("select google_pay_id,google_key from admin where type='superadmin'"));
  $google_pay_id = $google_pay['google_pay_id'];
  $google_pay_key = $google_pay['google_key'];

  $merchant_id = $google_pay_id;  // Your Merchant ID
  $merchant_key = $google_pay_key;  // Your Merchant Key
  $server_type = "Production";  // change this to go live

 // $merchant_id = "838300569379539";  // Your Merchant ID
  //$merchant_key = "ATvXTWlBco72cEKUGWrOng";  // Your Merchant Key
  //$server_type = "sandbox";

  $currency = 'USD';  // set to GBP if in the UK

  $Gresponse = new GoogleResponse($merchant_id, $merchant_key);

  $Grequest = new GoogleRequest($merchant_id, $merchant_key, $server_type, $currency);

  //Setup the log file
  $Gresponse->SetLogFiles(RESPONSE_HANDLER_ERROR_LOG_FILE, RESPONSE_HANDLER_LOG_FILE, L_ALL);

  // Retrieve the XML sent in the HTTP POST request to the ResponseHandler
  $xml_response = isset($HTTP_RAW_POST_DATA)?$HTTP_RAW_POST_DATA:file_get_contents("php://input");



  if (get_magic_quotes_gpc()) {
    $xml_response = stripslashes($xml_response);
  }

  list($root, $data) = $Gresponse->GetParsedXML($xml_response);



  $Gresponse->SetMerchantAuthentication($merchant_id, $merchant_key);

  $status = $Gresponse->HttpAuthentication();
  if(! $status) {

    die('authentication failed');
  }

  $root=trim(strtolower($root));
  switch ($root)
  {
     case "new-order-notification":

		 $order_item=get_arr_result($data[$root]['shopping-cart']['items']['item']);
		 foreach($order_item as $itm_val)
		 {
				$item_name=$itm_val['item-name']['VALUE'];
				$order_id=$itm_val['merchant-item-id']['VALUE'];
				$order_item_sku=$itm_val['item-description']['VALUE'];
				$product_quantity=$itm_val['quantity']['VALUE'];
				$product_item_price=$itm_val['unit-price']['VALUE'];

                if($order_id<>"")
                {
						if($item_name == "Product Payment")
						{
							$orderIdArr = explode("_",$order_id);
							for($i=0;$i<count($orderIdArr);$i++) {
								  $sql_qry="UPDATE t123go_product_sold SET payment='1' WHERE id='".$orderIdArr[$i]."'";
								  $myrwd=mysql_query($sql_qry);
							}

						} else {

							$bidres=mysql_query("select uid,total_bid from t123go_bidpackage p inner join t123go_memberpack m on p.pkgid=m.package_id where  m.id='".$order_id."' and payment='f'");
							if(mysql_num_rows($bidres)>0) {
								  $bidrec= mysql_fetch_array($bidres);
								  mysql_query("update user set no_bids=no_bids+$bidrec[total_bid] where id='$bidrec[uid]'");

								  $sql_qry="update t123go_memberpack set payment='c' where id='".$order_id."'";
								  $myrwd=mysql_query($sql_qry);

								 
							}
						}
				}
		  }

   break;

   case "risk-information-notification":


   break;
  default:
     //  mail("smith@123789.org","Response Handle","Default Case","","");
  break;

 }
  /* In case the XML API contains multiple open tags
     with the same value, then invoke this function and
     perform a foreach on the resultant array.
     This takes care of cases when there is only one unique tag
     or multiple tags.
     Examples of this are "anonymous-address", "merchant-code-string"
     from the merchant-calculations-callback API
  */
  function get_arr_result($child_node) {
    $result = array();
    if(isset($child_node)) {
      if(is_associative_array($child_node)) {
        $result[] = $child_node;
      }
      else {
        foreach($child_node as $curr_node){
          $result[] = $curr_node;
        }
      }
    }
    return $result;
  }

  /* Returns true if a given variable represents an associative array */
  function is_associative_array( $var ) {
    return is_array( $var ) && !is_numeric( implode( '', array_keys( $var ) ) );
  }
  function sendmail_smith($error)
  {
   //mail("smith@123789.org","Google Response Handle",$error,"","");
  }

?>