<?php
require_once("alipay.config.php");
require_once("lib/alipay_notify.class.php");

//计算得出通知验证结果
$alipayNotify=new AlipayNotify($alipay_config);
$verify_result=$alipayNotify->verifyNotify();

if($verify_result) { //验证成功

    $out_trade_no=$_POST['out_trade_no']; //订单号
    $trade_no=$_POST['trade_no']; //支付宝交易号
    $trade_status=$_POST['trade_status']; //交易状态

    if($_POST['trade_status'] == 'WAIT_BUYER_PAY') {
        //该判断表示买家已在支付宝交易管理中产生了交易记录，但没有付款
        echo "success";
    } else if($_POST['trade_status'] == 'WAIT_SELLER_SEND_GOODS') {
        //该判断表示买家已在支付宝交易管理中产生了交易记录且付款成功，但卖家没有发货
/*         global $db;
        $db->query("update " . tname('pay') . " set cd_state='等待发货' where cd_key='" . $out_trade_no . "'");
        $sql="select * from " . tname('pay') . " where cd_key='" . $out_trade_no . "'";
        $row=$db->getrow($sql);
        //发送通知
        $setarr=array(
            'cd_uid'=>0,
            'cd_uname'=>'系统提示',
            'cd_uids'=>$row['cd_uid'],
            'cd_unames'=>$row['cd_uname'],
            'cd_icon'=>'paypoints',
            'cd_data'=>'亲爱的用户，系统正在处理发货信息，稍后将进行发货，请注意本站系统通知！',
            'cd_dataid'=>0,
            'cd_state'=>1,
            'cd_addtime'=>date('Y-m-d H:i:s')
        );
        inserttable('notice', $setarr, 1); */
        echo "success";
    } else if($_POST['trade_status'] == 'WAIT_BUYER_CONFIRM_GOODS') {
        //该判断表示卖家已经发了货，但买家还没有做确认收货的操作
        global $db;
        $db->query("update " . tname('pay') . " set cd_state='等待确认收货' where cd_key='" . $out_trade_no . "'");
        $sql="select * from " . tname('pay') . " where cd_key='" . $out_trade_no . "'";
        $row=$db->getrow($sql);
        //发送通知
        $setarr=array(
            'cd_uid'=>0,
            'cd_uname'=>'系统提示',
            'cd_uids'=>$row['cd_uid'],
            'cd_unames'=>$row['cd_uname'],
            'cd_icon'=>'paypoints',
            'cd_data'=>'亲爱的用户，系统已经发货，您确认收货后，VIP会员将自动生效！',
            'cd_dataid'=>0,
            'cd_state'=>1,
            'cd_addtime'=>date('Y-m-d H:i:s')
        );
        inserttable('notice', $setarr, 1);
        echo "success";
    } else if($_POST['trade_status'] == 'TRADE_FINISHED') {
        //该判断表示买家已经确认收货，这笔交易完成
        global $db;
        $db->query("update " . tname('pay') . " set cd_state='交易完成',cd_extime='" . date('Y-m-d H:i:s') . "' where cd_key='" . $out_trade_no . "'");
        $sql="select * from " . tname('pay') . " where cd_key='" . $out_trade_no . "'";
        $row=$db->getrow($sql);
        $sqls="select cd_grade,cd_vipenddate,cd_vipindate from " . tname('user') . " where cd_name='" . $row['cd_uname'] . "'";
        $row3=$db->getrow($sqls);
        if($row3['cd_grade'] == "1") { //续费
            $cd_vippoints=cd_vippoints;
            $cd_viptime=cd_viptime;
            $cd_vipenddate=date("Y-m-d", strtotime("+" . $cd_viptime . " day", strtotime($row3['cd_vipenddate'])));
            $db->query("update " . tname('user') . " set cd_vipenddate='$cd_vipenddate' where cd_name='" . $row['cd_uname'] . "'");
            //记录账单
            $setarr=array(
                'cd_type'=>0, //1为加,0为减
                'cd_uid'=>$row['cd_uid'],
                'cd_uname'=>$row['cd_uname'],
                'cd_icon'=>'payvip',
                'cd_title'=>'支付续费' . cd_viptime . '天VIP会员',
                'cd_points'=>cd_vippoints,
                'cd_addtime'=>date('Y-m-d H:i:s')
            );
            inserttable('bill', $setarr, 1);
			//发送通知
            $setarr=array(
                'cd_uid'=>0,
                'cd_uname'=>'系统提示',
                'cd_uids'=>$row['cd_uid'],
                'cd_unames'=>$row['cd_uname'],
                'cd_icon'=>'payvip',
                'cd_data'=>cd_vipxtxt,
                'cd_dataid'=>0,
                'cd_state'=>1,
                'cd_addtime'=>date('Y-m-d H:i:s')
            );
            inserttable('notice', $setarr, 1);

        } else { //充值
            $cd_vippoints=cd_vippoints;
            $cd_viptime=cd_viptime;
            $cd_vipindate=date('Y-m-d H:i:s');
            $tomorrow=mktime(date("H"), date("i"), date("s"), date("m"), date("d")+$cd_viptime, date("Y"));
            $cd_vipenddate=date("Y-m-d H:i:s", $tomorrow);
            $db->query("update " . tname('user') . " set cd_vipgrade=1,cd_grade=1,cd_vipindate='$cd_vipindate',cd_vipenddate='$cd_vipenddate' where cd_name='" . $row['cd_uname'] . "'");
            //记录账单
            $setarr=array(
                'cd_type'=>0, //1为加,0为减
                'cd_uid'=>$row['cd_uid'],
                'cd_uname'=>$row['cd_uname'],
                'cd_icon'=>'payvip',
                'cd_title'=>'支付充值' . cd_viptime . '天VIP会员',
                'cd_points'=>cd_vippoints,
                'cd_addtime'=>date('Y-m-d H:i:s')
            );
            inserttable('bill', $setarr, 1);
			//发送通知
            $setarr=array(
                'cd_uid'=>0,
                'cd_uname'=>'系统提示',
                'cd_uids'=>$row['cd_uid'],
                'cd_unames'=>$row['cd_uname'],
                'cd_icon'=>'payvip',
                'cd_data'=>cd_vipktxt,
                'cd_dataid'=>0,
                'cd_state'=>1,
                'cd_addtime'=>date('Y-m-d H:i:s')
            );
            inserttable('notice', $setarr, 1);
        }
        echo "success";
    } else {
        //其他状态判断
        echo "success";
    }
} else {
    //验证失败
    echo '数据错误，请联系管理员！';
}
?>