<?php
error_reporting(0); 
include("../../config/ht.php");
//成功回调以后 
$money=$_REQUEST['order_amount'];//这是回调的金额 
$ddh=$_REQUEST['out_pay_sn']; //这是回调的商户订单号 
$qudao=$_REQUEST['out_pay_sn'];//这是返回的平台订单号 
//读取临时订单号信息 
$ddhmulu="../../".date("Y-m-d")."ddh";
$file=$ddhmulu."/".$ddh.".txt"; 
$content = file_get_contents($file);  
if (strpos($content, "|ubo|") !== false) {
$pontent = explode("|ubo|",$content); 
$ddh=$pontent[0]; //订单号
$money=$pontent[1]; //订单金额
$userid=$pontent[2]; //代理ID
$zyid=$pontent[3];//资源ID
$lx=$pontent[4]; //
$ip=$pontent[5]; //资源类型
$ddhto=$pontent[6]; //真正的订单号
$tdid=$pontent[7]; //通道ID
$paylx=$pontent[9]; 
$blx=$pontent[10]; 
} 
if($blx=='hzpaylist'){
$type="WHERE id='$tdid'";
$td=queryall(hzpaylist,$type);
$payid=$td[payid];//商户ID
$paykey=$td[paykey];//商户秘钥
$payurl=$td[payurl];//通信秘钥
}else{
$type="WHERE biaoshi='$paylx' and  zt='1' and sz='1'";
$hzpayto=queryall(hzpayto,$type);
$payid=$hzpayto[payid];//商户ID
$paykey=$hzpayto[paykey];//商户秘钥
$payurl=$hzpayto[payurl];//通信秘钥
}
//第三方支付接口
$data = [];
$data['order_amount'] = $_REQUEST['order_amount'];
$data['out_pay_sn']   = $_REQUEST['out_pay_sn'];
$data['seller_id']    = $_REQUEST['seller_id'];
$data['sub_mch_id']   = $_REQUEST['sub_mch_id'];
$data['sign']         = $_REQUEST['sign'];
$skey =$paykey;//商户密钥 后台获取
$keysign = $data['sign'];
unset($data['sign']);
ksort($data);
$sign = '';
foreach ($data as $key => $value) {
$sign .= $value;
}
$sign = md5($sign.$skey);
//第三方支付接口 
if($keysign == $sign){
echo 'OK';
$qudao=$_REQUEST['seller_id'].'_'.$qudao;
$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);
}
}else{ 
echo "err"; 
}
///---------------------------------------------------------以下禁止修改------------------------------------------------------------------
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); 
} 
}

?>
