<?php
header("Content-Type: text/html;charset=utf-8");
include_once "./config.php";
$_POST = $_GET;
file_put_contents('log.txt', "\r\n " . json_encode($_POST), FILE_APPEND);
$verify_result = verifyNotify($ukey);
if (!$verify_result) {
    exit('error:id or key');
}
function verifyNotify($ukey) {
    //if(empty($_GET)) return false;
    $sign=buildRequestPara($_POST);
    if ($sign === $_POST['sign']) {
        $signResult = true;
    } else {
        $signResult = false;
    }
    return $signResult;
}
$tc = '';
$arrs=explode("-sp-",$_POST['name']);
$qu=$quarr[$arrs[0]];
$uid = str_replace("赞助","",$arrs[1]);
$amount = $_POST['money'];
$sdpayno = $_POST['trade_no'];
$conn = new mysqli($db_ip, $db_name, $db_psd, $server);
if ($sdpayno == null) {
    exit("数据校验失败！");
} else {
    $order = mysqli_query($conn,"SELECT * FROM chongzhi WHERE orderid='" . $sdpayno . "' limit 1");
    if($order&&mysqli_num_rows($order)>0) {
    exit("此订单已存在！");
    }
}
$goods_item = null;
foreach ($goods as $w) {
    if ($w["amount"] == (int)$amount) {
        $goods_item = $w;
        break;
    }
}
if ($goods_item == null) {
    exit("未匹配金额为" . (int)$amount . "元的物品");
}
if(count($arrs)==3){
    $roleID = $arrs[1];
    $roleName = $arrs[2];
}else{
    $roleID = getRoleIDByName($qu['url'], $uid);
     $roleName = str_replace("赞助","",$arrs[1]);
}
if ($roleID == 0) {
    exit("该角色不存在！".$arrs[1]);
}
$params=array(
"ids"=>$roleID,
"title"=>"管理员的邮件",
"content"=>"管理员的邮件",
"items"=>$goods_item["goods"],
);
$rmb = (int)$amount;
$time = time();
if (mailByRoleName($qu['url'], $params)) {
    $sql = "INSERT INTO `chongzhi`(`uid`, `roleid`, `rmb`, `shijian`, `orderid`, `server`, `everyday`, `everydaycmd`, `recommend`, `tc`, `status`,`today`) VALUES ('" . $roleName . "','" . $roleID . "', " . (int)$amount . ", '" . date("Y-m-d H:i:s", time()) . "', '" . $sdpayno . "', '".$arrs[0]."', " . 0 . ", '" . 0 . "', (select a.recommend from (select recommend from user where uid='" . $roleName . "' and server='".$arrs[0]."' limit 1) a), '" . $goods_item["name"] . "', '成功'," . 0 . ")";
    mysqli_query($conn, $sql);
    echo "success";
    exit();
}
    
?>