<?php

/**
 * @Author: 琳飞
 * @Date:   2019-10-26 23:48:28
 * @Last Modified by:   琳飞
 * @Last Modified time: 2019-10-27 01:26:51
 * @E-mail: admin@eyabc.cn
 */
error_reporting(0);
include "../../config/ht.php";
//第三方支付接口
$notify_time  = $_REQUEST["notify_time"]; //支付时间
$out_trade_no = $_REQUEST["out_trade_no"]; //商户订单号
$trade_no     = $_REQUEST["trade_no"]; //支付宝订单号
$total_amount = $_REQUEST["total_fee"]; //订单金额
$subject      = $_REQUEST["subject"]; //订单标题
$body         = $_REQUEST["body"]; //商品描述
$trade_status = $_REQUEST['trade_status'];
//第三方支付接口
//成功回调以后///////////////////////////////////////////////////////////////////////////////////////////////////////
$ddh   = $out_trade_no;
$qudao = $trade_no;
$money = $total_amount;
//读取临时订单号信息
$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]; //通信秘钥
}

$sdkFilePath = __DIR__ . '/lib/xiaowei.php';

if (!file_exists($sdkFilePath)) {
    die('SDK文件不存在!');
}

require_once $sdkFilePath;

$sdk = new Xiaowei([
    'apiurl' => $payurl,
    'appid'  => $payid,
    'key'    => $paykey,
]);

//验证成功
if ($sdk->notify($_REQUEST)) {
    if ($trade_status == 'TRADE_FINISHED') {
    } else if ($trade_status == 'TRADE_SUCCESS') {
        echo "success";
        $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 'fail';
    }
} 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);
    }
}
//打印log到本地
///////////////////////////////////////////////////////////
$val             = "";
$currentDateTime = date('YmdHis', time());
$currentDate     = date('Ymd', time()) . '.txt';
$fileName        = $currentDate;
@$data           = fopen($fileName, 'a+');
$val .= $currentDateTime;
if ($_REQUEST) {
    $val .= '|POST' . '|' . $_REQUEST . "\n";
    foreach ($_REQUEST as $key => $value) {
        $val .= '|' . $key . ":" . $value;
    }
} else {
    $val .= '|GET' . '|' . $_GET . "\n";
    foreach ($_GET as $key => $value) {
        $val .= '|' . $key . ":" . $value;
    }
}
$val .= "\n";
fwrite($data, $val); //写入文本中
///////////////////////////////////////////////////////////
