<?php
/**
 * 支付同步返回页面
 * 用户支付完成后跳转到此页面
 */

require_once __DIR__ . '/../MPHX/common.php';

// 获取支付返回参数
$pid = isset($_GET['pid']) ? $_GET['pid'] : '';
$trade_no = isset($_GET['trade_no']) ? $_GET['trade_no'] : '';
$out_trade_no = isset($_GET['out_trade_no']) ? $_GET['out_trade_no'] : '';
$type = isset($_GET['type']) ? $_GET['type'] : '';
$name = isset($_GET['name']) ? $_GET['name'] : '';
$money = isset($_GET['money']) ? $_GET['money'] : '';
$trade_status = isset($_GET['trade_status']) ? $_GET['trade_status'] : '';
$sign = isset($_GET['sign']) ? $_GET['sign'] : '';
$sign_type = isset($_GET['sign_type']) ? $_GET['sign_type'] : '';

// 验证签名
$verify_result = false;
if (!empty($sign) && !empty($alipay_config['key'])) {
    // 构建待签名数组
    $para = array(
        'pid' => $pid,
        'trade_no' => $trade_no,
        'out_trade_no' => $out_trade_no,
        'type' => $type,
        'name' => $name,
        'money' => $money,
        'trade_status' => $trade_status
    );
    
    // 过滤空值
    $para_filter = array();
    foreach ($para as $key => $val) {
        if ($val !== '' && $val !== null) {
            $para_filter[$key] = $val;
        }
    }
    
    // 排序
    ksort($para_filter);
    reset($para_filter);
    
    // 构建签名字符串
    $arg = '';
    foreach ($para_filter as $key => $val) {
        $arg .= $key . '=' . $val . '&';
    }
    $arg = substr($arg, 0, -1);
    
    // 计算签名
    $mysign = md5($arg . $alipay_config['key']);
    
    // 验证签名
    $verify_result = ($mysign === $sign);
}

// 判断支付状态
$is_success = ($trade_status === 'TRADE_SUCCESS' || $trade_status === 'TRADE_FINISHED');

?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>支付结果 - <?php echo $conf['name']; ?></title>
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/css/all.min.css">
    <style>
        body {
            background-color: #f5f7fa;
            padding: 50px 20px;
        }
        .result-container {
            max-width: 500px;
            margin: 0 auto;
            background: #fff;
            border-radius: 10px;
            padding: 40px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            text-align: center;
        }
        .result-icon {
            font-size: 80px;
            margin-bottom: 20px;
        }
        .result-icon.success {
            color: #28a745;
        }
        .result-icon.error {
            color: #dc3545;
        }
        .result-title {
            font-size: 24px;
            font-weight: bold;
            margin-bottom: 20px;
        }
        .result-info {
            background: #f8f9fa;
            border-radius: 8px;
            padding: 20px;
            margin: 20px 0;
            text-align: left;
        }
        .result-info p {
            margin: 10px 0;
            color: #666;
        }
        .result-info strong {
            color: #333;
            float: right;
        }
        .btn-container {
            margin-top: 30px;
        }
        .btn-container .btn {
            margin: 5px;
        }
    </style>
</head>
<body>
    <div class="result-container">
        <?php if ($is_success && $verify_result): ?>
            <div class="result-icon success">
                <i class="fas fa-check-circle"></i>
            </div>
            <div class="result-title text-success">支付成功</div>
            <div class="result-info">
                <p>订单号：<strong><?php echo htmlspecialchars($out_trade_no); ?></strong></p>
                <p>交易号：<strong><?php echo htmlspecialchars($trade_no); ?></strong></p>
                <p>商品名称：<strong><?php echo htmlspecialchars($name); ?></strong></p>
                <p>支付金额：<strong style="color: #dc3545;">¥<?php echo number_format($money, 2); ?></strong></p>
                <p>支付方式：<strong><?php echo $type == 'wxpay' ? '微信支付' : '支付宝'; ?></strong></p>
            </div>
            <div class="alert alert-success">
                <i class="fas fa-info-circle"></i> 您的余额已自动到账，请查收！
            </div>
        <?php else: ?>
            <div class="result-icon error">
                <i class="fas fa-times-circle"></i>
            </div>
            <div class="result-title text-danger">支付处理中</div>
            <div class="alert alert-warning">
                <i class="fas fa-exclamation-triangle"></i> 
                <?php if (!$verify_result): ?>
                    签名验证失败，请联系客服处理。
                <?php else: ?>
                    支付状态异常，请稍后在订单记录中查看支付结果。
                <?php endif; ?>
            </div>
        <?php endif; ?>
        
        <div class="btn-container">
            <a href="index.php" class="btn btn-primary">
                <i class="fas fa-home"></i> 返回首页
            </a>
            <a href="orders.php" class="btn btn-outline-primary">
                <i class="fas fa-list"></i> 查看订单
            </a>
        </div>
    </div>
</body>
</html>
