<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php

	$paymentResult = $_POST["paymentResult"];//获取信息
	$xml=simplexml_load_string($paymentResult,'SimpleXMLElement', LIBXML_NOCDATA); 
	$ReferenceIDs = $xml->xpath("GateWayRsp/head/ReferenceID");//关联号
	$ReferenceID = $ReferenceIDs[0];//关联号
	$RspCodes = $xml->xpath("GateWayRsp/head/RspCode");//响应编码
	$RspCode=$RspCodes[0];
	$RspMsgs = $xml->xpath("GateWayRsp/head/RspMsg"); //响应说明
	$RspMsg=$RspMsgs[0];
	$ReqDates = $xml->xpath("GateWayRsp/head/ReqDate"); // 接受时间
	$ReqDate=$ReqDates[0];
	$RspDates = $xml->xpath("GateWayRsp/head/RspDate");// 响应时间
	$RspDate=$RspDates[0];
	$Signatures = $xml->xpath("GateWayRsp/head/Signature"); //数字签名
	$Signature=$Signatures[0];
	$MerBillNos = $xml->xpath("GateWayRsp/body/MerBillNo"); // 商户订单号
	$MerBillNo=$MerBillNos[0];
	$CurrencyTypes = $xml->xpath("GateWayRsp/body/CurrencyType");//币种
	$CurrencyType=$CurrencyTypes[0];
	$Amounts = $xml->xpath("GateWayRsp/body/Amount"); //订单金额
	$Amount=$Amounts[0];
	$Dates = $xml->xpath("GateWayRsp/body/Date");    //订单日期
	$Date=$Dates[0];
	$Statuss = $xml->xpath("GateWayRsp/body/Status");  //交易状态
	$Status=$Statuss[0];
	$Msgs = $xml->xpath("GateWayRsp/body/Msg");    //发卡行返回信息
	$Msg=$Msgs[0];
	$Attachs = $xml->xpath("GateWayRsp/body/Attach");    //数据包
	$Attach=$Attachs[0];
	$IpsBillNos = $xml->xpath("GateWayRsp/body/IpsBillNo"); //IPS订单号
	$IpsBillNo=$IpsBillNos[0];
	$IpsTradeNos = $xml->xpath("GateWayRsp/body/IpsTradeNo"); //IPS交易流水号
	$IpsTradeNo=$IpsTradeNos[0];
	$RetEncodeTypes = $xml->xpath("GateWayRsp/body/RetEncodeType");    //交易返回方式
	$RetEncodeType=$RetEncodeTypes[0];
	$BankBillNos = $xml->xpath("GateWayRsp/body/BankBillNo"); //银行订单号
	$BankBillNo=$BankBillNos[0];
	$ResultTypes = $xml->xpath("GateWayRsp/body/ResultType"); //支付返回方式
	$ResultType=$ResultTypes[0];
	$IpsBillTimes = $xml->xpath("GateWayRsp/body/IpsBillTime"); //IPS处理时间
	$IpsBillTime=$IpsBillTimes[0];
			 
			 
	$pmercode='193273';	
	$arrayMer['mercert'] = '9DH6k7ctDvhNxjmcpLJeA2gd7thpZE1d5mBBccmxyBvGp9yrxzZnxnswp6lP1ZOYOs8EoOnqcng0oke7EnudU3noBsh0J24KzQCBMmFBBlyfuc1lqmOdxAg4gPpU3lKV';
	$sbReq = "<body>"
			  . "<MerBillNo>" . $MerBillNo . "</MerBillNo>"
			  . "<CurrencyType>" . $CurrencyType . "</CurrencyType>"
			  . "<Amount>" . $Amount . "</Amount>"
			  . "<Date>" . $Date . "</Date>"
			  . "<Status>" . $Status . "</Status>"
			  . "<Msg><![CDATA[" . $Msg . "]]></Msg>"
			  . "<Attach><![CDATA[" . $Attach . "]]></Attach>"
			  . "<IpsBillNo>" . $IpsBillNo . "</IpsBillNo>"
			  . "<IpsTradeNo>" . $IpsTradeNo . "</IpsTradeNo>"
			  . "<RetEncodeType>" . $RetEncodeType . "</RetEncodeType>"
			  . "<BankBillNo>" . $BankBillNo . "</BankBillNo>"
			  . "<ResultType>" . $ResultType . "</ResultType>"
			  . "<IpsBillTime>" . $IpsBillTime . "</IpsBillTime>"
	          . "</body>";
		$sign=$sbReq.$pmercode.$arrayMer['mercert'];		
		$md5sign=  md5($sign);
		//判断签名
	if($Signature==$md5sign)
	{
		if($RspCode=='000000')
		{
				$data['orderNo'] = $MerBillNo;
				$data['tradeAmt'] = $Amount;
				require_once("../../include/conn_1.php");
				require_once("../../include/public.php");
				$sqlsc = "select username,amount,status,ordernumber from  {$db_prefix}member_order where ordernumber = '".$data['orderNo']."'";
				$rssc = $db->get_one($sqlsc);
				if($rssc['status'] == 1){
					$url = "notify.php";
					Header("Location: $url");
					return true;
				}else if((int)$rssc['amount'] !== (int)$data['tradeAmt']){
					echo "金额不相等";
					return false;
				}else{

					$addamount    = $rssc['amount'];
					$username  	  = $rssc['username'];

					//查询上家
					$sqlSj 	 = "select inviter from {$db_prefix}member where username = '".$username."'";
					$rs_sj   = $db->get_one($sqlSj);
					$Topusername  = trim($rs_sj['inviter']);	//上线
					
					//发送记录
					$compnayN = configW('webname');
					$title    = "尊敬的".$username."会员您好！您成功充值".$addamount."元";
					$content  = "您成功充值".$addamount."元";
					$memo     = "在线充值";
					$rs_msg   = sendMsg($username,$compnayN,$title,$content,$memo);

					//增加积分
					$sql_amo = "update {$db_prefix}member set amount = amount + {$addamount}  where  username = '".$username."'";
					$rs_amo  =$db->query($sql_amo);

					//更新状态，插入记录
					$sql_lgn = "update {$db_prefix}member_order set status = 1,paytime = '".time()."',accNo = '".$data['accNo']."'  where  ordernumber = '".$data['orderNo']."'";
					$rs_plus =$db->query($sql_lgn);

					//对下线充值奖励
					// if(!empty($Topusername)){

					// 	//查询奖励比例
					// 	$sqlpr = "select XiaXianFenCheng from  {$db_prefix}member_config where id = 1";
					// 	$rssc = $db->get_one($sqlpr);
					// 	$XiaXianFenCheng = $rssc['XiaXianFenCheng'];	//下线分成比例
					// 	$XiaXianAmount   = $rssc['XiaXianFenCheng'] * $addamount;	//抽成金额
					// 	$XiaXianAmount   = intval($XiaXianAmount); 
						
					// 	//插入分成数据
					// 	$sqlFc="insert into {$db_prefix}member_cashback(username,xxusername,amount,preamount,type,status,posttime) values('{$Topusername}','{$username}','{$addamount}','{$XiaXianAmount}','下线分成',1,'".time()."')";
					// 	$db->query($sqlFc);

					// 	//发送记录
					// 	$title    = "尊敬的".$Topusername."会员您好！您的下线分成".$XiaXianAmount."元";
					// 	$content  = "您的下线".$username."充值分成".$XiaXianAmount."元,已赠送到您的账号。";
					// 	$memo     = "下线分成";
					// 	$rs_msg   = sendMsg($Topusername,$compnayN,$title,$content,$memo);

					// 	//增加金额
					// 	$sql_inver = "update {$db_prefix}member set amount = amount + {$XiaXianAmount}  where  username = '".$Topusername."'";
					// 	$db->query($sql_inver);
					// }

					if($rs_msg && $rs_amo && $rs_plus){
						//$url = "notify.php";
						//Header("Location: $url");
						echo "充值成功!";
						return true;
					}else{
						echo $rssc['ordernumber'].",充值不成功";
						return false;
					}
				}



			//echo "<script>alert('充值成功!');window.close();</script>";				
		}			
	}
	else
	{
			
		echo "Failed";
		die();
	}


?>