<?php 
	session_start();
	require_once("db/db.php");	
	require_once("common/common.php");
	
	//获取接口数据，如果$_REQUEST拿不到数据，则使用file_get_contents函数获取
    $post = $_REQUEST;
    if ($post == null) {
        $post = file_get_contents("php://input");
    }
 
    if ($post == null) {
        $post = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
    }
 
    if (empty($post) || $post == null || $post == '') {
        //阻止微信接口反复回调接口  文档地址 https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=9_7&index=7，下面这句非常重要!!!
        $str='<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';  
        echo $str;
        exit('Notify 非法回调');
    }
 
    /*****************微信回调返回数据样例*******************
     $post = '<xml>
        <return_code><![CDATA[SUCCESS]]></return_code>
        <return_msg><![CDATA[OK]]></return_msg>
        <appid><![CDATA[wx2421b1c4370ec43b]]></appid>
        <mch_id><![CDATA[10000100]]></mch_id>
        <nonce_str><![CDATA[IITRi8Iabbblz1Jc]]></nonce_str>
        <sign><![CDATA[7921E432F65EB8ED0CE9755F0E86D72F]]></sign>
        <result_code><![CDATA[SUCCESS]]></result_code>
        <prepay_id><![CDATA[wx201411101639507cbf6ffd8b0779950874]]></prepay_id>
        <trade_type><![CDATA[APP]]></trade_type>
        </xml>';
     *************************微信回调返回*****************/
 
    libxml_disable_entity_loader(true); //禁止引用外部xml实体
 
    $xml = simplexml_load_string($post, 'SimpleXMLElement', LIBXML_NOCDATA);//XML转数组
    
    $post_data = (array)$xml;
    
    /** 解析出来的数组
        *Array
        * (
        * [appid] => wx1c870c0145984d30
        * [bank_type] => CFT
        * [cash_fee] => 100
        * [fee_type] => CNY
        * [is_subscribe] => N
        * [mch_id] => 1297210301
        * [nonce_str] => gkq1x5fxejqo5lz5eua50gg4c4la18vy
        * [openid] => olSGW5BBvfep9UhlU40VFIQlcvZ0
        * [out_trade_no] => fangchan_588796
        * [result_code] => SUCCESS
        * [return_code] => SUCCESS
        * [sign] => F6890323B0A6A3765510D152D9420EAC
        * [time_end] => 20180626170839
        * [total_fee] => 100
        * [trade_type] => JSAPI
        * [transaction_id] => 4200000134201806265483331660
        * )
    **/
    //订单号
    $out_trade_no = isset($post_data['out_trade_no']) && !empty($post_data['out_trade_no']) ? $post_data['out_trade_no'] : 0;
	//$out_total_fee = isset($post_data['total_fee']) && !empty($post_data['total_fee']) ? $post_data['total_fee'] : 0;
 
	echo $out_trade_no;
	
	//开始记录流水(设置为已支付成功，未入帐，入帐后为2
	$sql = sprintf("UPDATE WX_Pay_List SET DoFlag=5 Where WXOrderNo='%s' And DoFlag=0",$out_trade_no);
	$result = querySql($sql);
	

	//微信支付
	$orderno="";
	
	$sql1=sprintf("Select count(ListNo) as count from WX_Pay_List Where WXOrderNo='%s' And DoFlag=5",$out_trade_no);
	$result1 = querySql($sql1);
	$row1 = fetch_array($result1);
	$count = $row1['count'];
	
	$out_total_fee=11;
	
	freeResult($result1);
	if($count > 0)
	{
		$strremarks="微信订房,[/*".$out_trade_no."*/]";
		$sql = sprintf("EXECUTE TRS_OrderAddPayForVal '%s','','%s',%f,'%s'", $orderno, '微信支付', $out_total_fee,$strremarks);//wx
		//echo $sql;
		$result = querySql($sql);
		//$row = fetch_array($result);
		//var_dump($row);
				
		//更改为已入帐
		$sql = sprintf("UPDATE WX_Pay_List SET DoFlag=6 Where WXOrderNo='%s' And DoFlag=5",$out_trade_no);
		$result = querySql($sql);
	}	
 
	closeDb();
 
 
 
 
 
 
 
 
 

?>