<?php

require_once("alipay.config.php");
require_once("lib/alipay_notify.class.php");
include "../../include/conn.php";
?>
<!DOCTYPE HTML>
<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<?php
//计算得出通知验证结果
$alipayNotify = new AlipayNotify($aliapy_config);
$verify_result = $alipayNotify->verifyReturn();
if($verify_result) {//验证成功
    $out_trade_no	= $_GET['out_trade_no'];	//获取订单号
    $trade_no		= $_GET['trade_no'];		//获取支付宝交易号
    $total_fee		= $_GET['total_fee'];		//获取总价格
    $payuser		= $_GET['subject'];	

    if($_GET['trade_status'] == 'TRADE_FINISHED' || $_GET['trade_status'] == 'TRADE_SUCCESS') {
    }
    else {
      echo "trade_status=".$_GET['trade_status'];
    }
		
	global $db;
	//检测订单，存在就加点
	$result=$db->query("select * from s_pay where S_Payid='".$out_trade_no."' and S_Payuser='".$payuser."' and S_Vid=0");
	if($row=$db->fetch_array($result)){	
	$S_Payrmb=$row["S_Payrmb"];
				$db->query("update s_users set S_RMB=S_RMB+$S_Payrmb where S_Name='".$payuser."'");
				$db->query("update s_pay set S_Vid=1 where S_Payid='".$out_trade_no."' and S_Payuser='".$payuser."'");
				//产生消息
				$s_xid=0;
				$s_cid=0;
				$s_namea=$payuser;
				$s_nameb="系统消息";
				$s_content="恭喜您，成功充值了 ".$S_Payrmb." 元，若出现问题请尽快联系客服。";
				$s_addtime=date('Y-m-d H:i:s');
				$sqlx="insert into s_xiaoxi (S_Xid,S_Nameb,S_Namea,S_Content,S_Addtime) values (".$s_xid.",'".$s_nameb."','".$s_namea."','".$s_content."','".$s_addtime."')";
				if($db->query($sqlx));
	        echo "恭喜您，成功充值了 ".$total_fee." 元，若出现问题请尽快联系客服。</br>";
	        echo "请牢记您的定单号：".$out_trade_no;
	}else{
	        echo "验证失败,非法订单。";
	}
}
else {
    //验证失败
    echo "验证失败";
}
?>
        <title>支付宝即时到帐接口</title>
	</head>
    <body>
    </body>
</html>