<?php
/* *
 * 支付宝当面付异步通知页面
 */
require_once("../../includes/fun.global.php");
/**
 * 写入测试数据
 */
require_once("config.php");
require_once("AlipayTradeService.php");

//计算得出通知验证结果
$alipaySevice = new AlipayTradeService($config);
$verify_result = $alipaySevice->check($_REQUEST);

if ($verify_result && !empty($conf['alipay_public_key']) && !empty($conf['merchant_private_key']) && (int)$conf['alipay_fpay_id'] == (int)$_REQUEST['app_id'] && $conf['pay_alipay'] == 3) {
    //商户订单号
    $out_trade_no = (int)$_QET['out_trade_no'];
    //支付宝交易号
    $trade_no = $_REQUEST['trade_no'];
    $srow = $DB->get_row("SELECT * FROM sky_pay WHERE `order`='{$out_trade_no}' limit 1 for update");
    if ($_REQUEST['trade_status'] == 'TRADE_SUCCESS' && $srow['state'] == 2) {
        if ($DB->get_row("SELECT * FROM `sky_order` WHERE `order` = '$out_trade_no' LIMIT 1")) die('success');
        //付款完成后，支付系统发送该交易状态通知
        $_REQUEST['money'] = $_REQUEST['buyer_pay_amount'];
        pay::success($_REQUEST, 2);
    } else echo "success";
} else {
    //验证失败
    echo "fail";
}
?>