<?php
include 'common.php';


function InsertPurchaseLog($account_id, $login_key, $char_uid, $char_level, $item_uid, $item_count, $amount,$pay_type)
{
	$dbhandle = DB_Connect();
	//Select DB
	$db_selected = mysqli_select_db($dbhandle, DB_NAME)
	or die("Could not select db");
	
	
	//if(!CheckLoginKey($dbhandle, $account_id, $login_key))
	//{
	//	PrintLoginKeyError();
	//	return;
	//}
	
	$now = new DateTime();
	$now->setTimezone(new DateTimeZone('UTC'));
	$log_time =  date_format($now, "Y-m-d H:i:s");
	
	$insert_query = "INSERT INTO purchase_log (user_id, character_uid, character_level, item_uid, item_count,pay_amount , pay_type, log_time) VALUES ('".$account_id."',".$char_uid.",".$char_level.",".$item_uid.",".$item_count.",".$amount.",".$pay_type.",'".$log_time."')";

	//Query Result
	$result = @mysqli_query($dbhandle,$insert_query);
	$response = "PURCHASE_LOG_FAILED";
	$error = "";
	if($result)
	{
		$response = "PURCHASE_LOG_OK";
		// don't use for upate/insert query, because these query result is true/false, not mysqli_result object
		//mysqli_free_result($result); 
		
		//$error = $insert_query ;
	}
	else
	{
		
		$error = mysqli_error($dbhandle);
		//$error = $insert_query ;
	}
	
	 mysqli_close($dbhandle);
 
    $isXmlHttpRequest = (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) ?
    (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ? true : false: false;
    ($isXmlHttpRequest) ? header('Content-type: application/json') : header('Content-type: text/plain');
 
    echo json_encode(array('result'=>$response, 'error'=>$error));
}


$account_id = $_GET['account_id'];
$login_key = $_GET['login_key'];
$char_uid = $_GET['char_uid'];
$char_level = $_GET['char_level'];
$item_uid = $_GET['item_uid'];
$item_count = $_GET['item_count'];
$amount = $_GET['amount'];
$pay_type = $_GET['pay_type'];
if(CheckValidatedServerIP())
{
	
	InsertPurchaseLog($account_id, $login_key, $char_uid, $char_level, $item_uid, $item_count, $amount,$pay_type);
	

}
else
{
	$response = "INVALIDATED_SERVER";
	$error = "CheckValidatedServerIP ERROR";
	$isXmlHttpRequest = (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) ?
	(strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ? true : false: false;
	($isXmlHttpRequest) ? header('Content-type: application/json') : header('Content-type: text/plain'); 
 
	echo json_encode(array(	'result'=>$response,'error'=>$error	));
}

?>