<?php
/**
 * 余额充值同步返回处理
 * 支付完成后页面跳转
 */
include("../MPHX/common.php");
require_once(SYSTEM_ROOT."lib/notify.class.php");

// 验证签名
$alipayNotify = new AlipayNotify($alipay_config);
$verify_result = $alipayNotify->verifyReturn();

if($verify_result) {
    // 获取支付参数
    $out_trade_no = $_GET['out_trade_no'];
    $trade_no = $_GET['trade_no'];
    $trade_status = $_GET['trade_status'];
    $money = $_GET['money'];
    
    if($_GET['trade_status'] == 'TRADE_SUCCESS') {
        // 查询订单信息
        $order = $DB->get_row("SELECT * FROM `balance_orders` WHERE `order_no` = '{$out_trade_no}' LIMIT 1");
        
        if($order && $order['status'] != 'paid') {
            // 更新订单状态为已支付
            $DB->query("UPDATE `balance_orders` SET `status` = 'paid', `pay_time` = NOW() WHERE `order_no` = '{$out_trade_no}'");
            
            // 增加用户余额
            $uid = intval($order['uid']);
            $amount = floatval($order['amount']);
            $DB->query("UPDATE `zj` SET `balance` = `balance` + {$amount} WHERE `id` = {$uid}");
            
            // 记录日志
            $user_info = $DB->get_row("SELECT * FROM `zj` WHERE `id` = {$uid}");
            if($user_info) {
                $DB->query("INSERT INTO `log` (`user`, `date`, `lx`, `lr`, `ip`) VALUES ('{$user_info['user']}', NOW(), '余额充值', '充值金额：{$amount}元，订单号：{$out_trade_no}', '{$clientip}')");
            }
        }
        
        // 支付成功，跳转到充值页面并显示成功消息
        echo '<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>支付成功</title>
    <script src="https://cdn.bootcss.com/sweetalert/1.1.3/sweetalert.min.js"></script>
    <link href="https://cdn.bootcss.com/sweetalert/1.1.3/sweetalert.min.css" rel="stylesheet">
</head>
<body>
<script type="text/javascript">
    swal({
        title: "支付成功",
        text: "您的余额充值已成功到账！",
        type: "success"
    }, function() {
        window.location.href = "pay.php";
    });
</script>
</body>
</html>';
    } else {
        // 支付未完成
        echo '<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>支付未完成</title>
    <script src="https://cdn.bootcss.com/sweetalert/1.1.3/sweetalert.min.js"></script>
    <link href="https://cdn.bootcss.com/sweetalert/1.1.3/sweetalert.min.css" rel="stylesheet">
</head>
<body>
<script type="text/javascript">
    swal({
        title: "支付未完成",
        text: "订单状态：'.$_GET['trade_status'].'",
        type: "warning"
    }, function() {
        window.location.href = "pay.php";
    });
</script>
</body>
</html>';
    }
} else {
    // 验证失败
    echo '<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>支付验证失败</title>
    <script src="https://cdn.bootcss.com/sweetalert/1.1.3/sweetalert.min.js"></script>
    <link href="https://cdn.bootcss.com/sweetalert/1.1.3/sweetalert.min.css" rel="stylesheet">
</head>
<body>
<script type="text/javascript">
    swal({
        title: "支付验证失败",
        text: "支付签名验证失败，请联系客服处理！",
        type: "error"
    }, function() {
        window.location.href = "pay.php";
    });
</script>
</body>
</html>';
}
?>
