<?php
error_reporting(0); 
include("../../../config/ht.php");
include_once("log_.php");
include_once("WxPayPubHelper.php");
$ttw = new Notify_pub();
$xml = $GLOBALS['HTTP_RAW_POST_DATA'];	
$ttw->saveData($xml);
if($ttw->checkSign() == FALSE){
$ttw->setReturnParameter("return_code","FAIL");//返回状态码
$ttw->setReturnParameter("return_msg","签名失败");//返回信息
}else{
$ttw->setReturnParameter("return_code","SUCCESS");//设置返回码
}
$tte = $ttw->returnXml();
echo $tte;
$ttr = new Log_();
$ttrname="wx.log";//log文件路径
$ttr->log_result($ttrname,"【接收到的notify通知】:\n".$xml."\n");
if($ttw->checkSign() == TRUE){
if ($ttw->data["return_code"] == "FAIL") {
$ttr->log_result($ttrname,"【通信出错】:\n".$xml."\n");
}elseif($ttw->data["result_code"] == "FAIL"){
$ttr->log_result($ttrname,"【业务出错】:\n".$xml."\n");
}else{
$ttu=$ttw->data["out_trade_no"];//
$qudao=$ttw->data["transaction_id"];//微信订单号
$money=$ttw->data["total_fee"]/100;
$ddh=$ttw->data["attach"];//我提交的订单号
//读取临时订单号信息 
$ddhmulu="../../../".date("Y-m-d")."ddh";
$file=$ddhmulu."/".$ddh.".txt"; 
$content = file_get_contents($file);  
if (strpos($content, "|ubo|") !== false) {
$pontent = explode("|ubo|",$content); 
$userid=$pontent[0]; 
$zyid=$pontent[1]; 
$ip=$pontent[2]; 
$lx=$pontent[3]; 
$ddhto=$pontent[5]; 
} 
$cgpd=md5($ddhto.$userid.$zyid.$ip); 
$lujing="../../../peizhi/ddh/".$cgpd.".txt";
$neir =$ddh; 
writefile($lujing,$neir); 
if($kl=="1"){ 
$type="where userid='$userid'";
$klt=queryall(link,$type);
$ns=$klt[ns]; 
if($klt){ 
$cs=$klt[cs]-1;
if (strpos($cs, "-") !== false) { 
$cs=$ns-1; 
}
if($cs=="0"){
if($lx=="gl"){
$type="where id='$zyid'";
$shipin=queryall(shipin,$type);
}else{
$type="where id='$zyid'";
$shipin=queryall(daili,$type);
} 
$zymc=$shipin[name]; 
$type="where userid='$userid'";
$user=queryall(user,$type);
$name=$user[name];
$type="(`id`, `name`, `userid`, `ddh`, `qudao`,`money`,`shijian`,`zymc`,`zyid`)   VALUES (null,'$name','$userid','$ddh','$qudao','$money','$shijian','$zymc','$zyid')";
dbinsert(kl,$type);
$type="cs='$ns' where userid='$userid'";
upalldt(link,$type); 
}else{
$type="cs='$cs' where userid='$userid'";
upalldt(link,$type); 
echo dingdanto($ddh,$userid,$money,$lx,$zyid,$qudao,$shijian,$ip); 
}
}else{
echo dingdanto($ddh,$userid,$money,$lx,$zyid,$qudao,$shijian,$ip);
}
}else{ 
echo dingdanto($ddh,$userid,$money,$lx,$zyid,$qudao,$shijian,$ip);
}
echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml> ';
exit;
}
} else {
echo 'sign error';
}
///---------------------------------------------------------以下禁止修改------------------------------------------------------------------
function dingdanto($ddh,$userid,$money,$lx,$zyid,$qudao,$shijian,$ip) {
$type="where ddh='$ddh'";
$row=queryall(dingdan,$type);
if(!$row){
$type="where userid='$userid'";
$user=queryall(user,$type);
if($user){ 
if($money<1){ 
$m2=$money;
}else{ 
$m2=$money-(($money*$user[txfl])/100); 
}
$m=$user[money]+$m2;
$type="money='$m'  where userid='$userid'";
upalldt(user,$type);
} 
if($lx=="gl"){
$type="where id='$zyid'";
$shipin=queryall(shipin,$type);
}else{
$type="where id='$zyid'";
$shipin=queryall(daili,$type);
}
$zymc=$shipin[name];
if($shipin){
$cs=$shipin[cs]+1;
if($lx=="gl"){
$type="cs='$cs'  where id='$zyid'";
upalldt(shipin,$type);
}else{ 
$type="cs='$cs'  where id='$zyid'";
upalldt(daili,$type); 
}
} 
$type="(`id`, `userid`, `zyid`, `zymc`, `money`, `shijian`,`ddh`,`wxddh`,`ip`) VALUES (null,'$userid','$zyid','$zymc','$money','$shijian','$ddh','$qudao','$ip')"; 
dbinsert(dingdan,$type); 
} 
}
?>