<?php
if(function_exists('file_get_contents')){
	$postStr = file_get_contents("php://input");
}else{
	$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
}


//调试记录开始
$notifydatastr = print_r($notifydata,true)."----------2---------notifydata  \r\n";
$notifydatastr = $notifydatastr.print_r(xmlToArray($postStr),true)."---------poststr \r\n";
$notifydatastr = $notifydatastr.print_r($_GET,true)."---------GET \r\n";
$notifydatastr = $notifydatastr.print_r($_POST,true)."---------POST \r\n\r\n\r\n";
$notifydatastr = $notifydatastr.DISCUZ_PARTNER.'|'.DISCUZ_SECURITYCODE."---------常量 \r\n\r\n\r\n";;
$filename = "./wxpay.txt";
$fp = fopen("$filename", "a"); //打开文件指针，创建文件
fwrite($fp, $notifydatastr);
fclose($fp); //关闭指针
//调试记录结束



$post = xmlToArray($postStr);
if($post['result_code'] == 'SUCCESS'){
	$data = array();
	$data['paystate'] = 3;
	$data['orderid'] = $post['transaction_id'];
	$data['pay_time'] = $_G['timestamp'];
	$data['notify_time'] = $_G['timestamp'];
	DB::update("xj_eventpay_log",$data,"tradeno='".$post['out_trade_no']."'");
	$paylog = DB::fetch_first("SELECT applyid FROM ".DB::table('xj_eventpay_log')." WHERE tradeno='".$post['out_trade_no']."'");
	
	$data = array();
	$data['pay_state'] = 1;
	DB::update("xj_eventapply",$data,"applyid=".$paylog['applyid']);
	
	$return = array();
	$return['return_code'] = 'SUCCESS';  //SUCCESS/FAIL   SUCCESS表示商户接收通知成功并校验成功
	$return['return_msg'] = 'ok';  //返回信息，如非空，为错误原因
}else{
	$return = array();
	$return['return_code'] = 'FAIL';  //SUCCESS/FAIL   SUCCESS表示商户接收通知成功并校验成功
	$return['return_msg'] = 'ERROR';  //返回信息，如非空，为错误原因
}


echo arrayToXml($return);


/**
 * 	作用：array转xml
 */
function arrayToXml($arr)
{
	$xml = "<xml>";
	foreach ($arr as $key=>$val)
	{
		 if (is_numeric($val))
		 {
			$xml.="<".$key.">".$val."</".$key.">"; 

		 }
		 else
			$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";  
	}
	$xml.="</xml>";
	return $xml; 
}

/**
 * 	作用：将xml转为array
 */
function xmlToArray($xml)
{		
	//将XML转为array        
	$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);		
	return $array_data;
}
?>