php 支付宝支付回调与查询订单

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/mengzuchao/article/details/82900617

https://docs.open.alipay.com/api_1/alipay.trade.query

 

<?php
/* *
 * 功能:支付宝服务器异步通知页面
 * 版本:2.0
 * 修改日期:2016-11-01
 * 说明:
 * 以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。

 *************************页面功能说明*************************
 * 创建该页面文件时,请留心该页面文件中无任何HTML代码及空格。
 * 该页面不能在本机电脑测试,请到服务器上做测试。请确保外部可以访问该页面。
 * 如果没有收到该页面返回的 success 信息,支付宝会在24小时内按一定的时间策略重发通知
 */
include('../../include/common.inc.php');
include('../sdkFunction.php');
require_once("alipay.config.php");
require_once("wappay/service/AlipayTradeService.php");
require_once("wappay/buildermodel/AlipayTradeQueryContentBuilder.php");

$alipay_config['notify_url'] = '';
if(CONF_MODE=='official'){
	$alipay_config['notify_url'] = 'http://sdk.17byh.com/sdk/alipay_v2/notify_url.php';
}else{
	$alipay_config['notify_url'] = 'http://t.sdk.17byh.com/sdk/alipay_v2/notify_url.php';
}

$alipaySevice = new AlipayTradeService($alipay_config);

function zfbHandle()
{
	global $alipaySevice;

    $arr = $_POST;

    /* 实际验证过程建议商户添加以下校验。
    1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号,
    2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额),
    3、校验通知中的seller_id(或者seller_email) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email)
    4、验证app_id是否为该商户本身。
    */
    $result = 1;
    if ($result) {
        //验证成功
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //请在这里加上商户的业务逻辑程序代


        //——请根据您的业务逻辑来编写程序(以下代码仅作参考)——

        //获取支付宝的通知返回参数,可参考技术文档中服务器异步通知参数列表

        //商户订单号

        $out_trade_no = $_POST['out_trade_no'];

        //支付宝交易号

        $trade_no = $_POST['trade_no'];

        //交易状态
        $trade_status = $_POST['trade_status'];

        //充值金额
        $amount = intval($_POST['total_amount']);

        if ($_POST['trade_status'] == 'TRADE_FINISHED') {

            //判断该笔订单是否在商户网站中已经做过处理
            //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
            //请务必判断请求时的total_amount与通知时获取的total_fee为一致的
            //如果有做过处理,不执行商户的业务程序

            //注意:
            //退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知
        } else if ($_POST['trade_status'] == 'TRADE_SUCCESS') {
            //判断该笔订单是否在商户网站中已经做过处理
            //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
            //请务必判断请求时的total_amount与通知时获取的total_fee为一致的
            //如果有做过处理,不执行商户的业务程序
            //注意:
            //付款完成后,支付宝系统发送该交易状态通知
            $check_sql = "select amount,status from cy_pay where orderid='" . $_REQUEST['out_trade_no'] . "'";
            $check_rs = mysql_query($check_sql);
            $check_data = array();
            while ($check_row = mysql_fetch_assoc($check_rs)) {
                $check_data = $check_row;
            }

            //验证订单数额的一致性
            if ($check_data['amount'] == $amount) {
                $sql = "UPDATE cy_pay SET status=1,beizhu='" . $_REQUEST['trade_no'] . "' WHERE orderid='" . $_REQUEST['out_trade_no'] . "'";
                $rs = mysql_query($sql);

                //通知游戏方
                if ($rs) {
                    $sql_info = "UPDATE cy_paycpinfo SET payflag=1 where orderid='" . $_REQUEST['out_trade_no'] . "'";
                    $rs_info = mysql_query($sql_info);

                    $sql_info = "select fcallbackurl,params,status,type from cy_paycpinfo where orderid='" . $_REQUEST['out_trade_no'] . "'";
                    $rs_info = mysql_query($sql_info);
                    $data = array();
                    while ($row = mysql_fetch_assoc($rs_info)) {
                        $data = $row;
                    }
                    $fcallbackurl = $data['fcallbackurl'];
                    $params = $data['params'];
                    $status = $data['status'];
                    $device = $data['type'];

                    if ($status == 0) {
                        $i = 0;
                        $result = true;
                        while ($result) {
                            $cp_rs = $alipaySevice->payback($fcallbackurl, $params, 'post');
                            if ($cp_rs) {
                                $time = time();
                                $sql_cp = "UPDATE cy_paycpinfo SET status=1,update_time='" . $time . "' where orderid='" . $_REQUEST['out_trade_no'] . "'";
                                $rs_cp = mysql_query($sql_cp);

                                $result = false;
                            } else {
                                $i++;
                                sleep(2);
                                $result = true;
                            }

                            if ($i == 5) {
                                $result = false;
                            }
                        }
                    }

                    //判断是否已录入
                    $sql = "select * from cy_pay_ok where orderid='" . $_REQUEST['out_trade_no'] . "'";
                    $has_info = mysql_query($sql);
                    $has_data = array();
                    while ($row = mysql_fetch_assoc($has_info)) {
                        $has_data = $row;
                    }
                    if (!$has_data) {
                        //录入pay_ok
                        $sql_info = "select * from cy_pay where orderid='" . $_REQUEST['out_trade_no'] . "'";
                        $rs_info = mysql_query($sql_info);
                        $data = array();
                        while ($row = mysql_fetch_assoc($rs_info)) {
                            $data = $row;
                        }
                        //取渠道信息
                        $cps_user_sql = "select id,username,agent,one_agent,one_username,second_agent,second_username from cy_ct_cps_user where agent='" . $data['regagent'] . "'";
                        $cps_user_info = mysql_query($cps_user_sql);
                        $cps_data = array();
                        while ($row = mysql_fetch_assoc($cps_user_info)) {
                            $cps_data = $row;
                        }
                        $data['second_agent'] = $cps_data['second_agent'];
                        $data['second_username'] = $cps_data['second_username'];
                        $data['one_agent'] = $cps_data['one_agent'];
                        $data['one_username'] = $cps_data['one_username'];
                        $data['agent_username'] = $cps_data['username'];
                        //游戏名字
                        $game_sql = "select id,name from cy_game where id='" . $data['gameid'] . "'";
                        $game_info = mysql_query($game_sql);
                        $game_data = array();
                        while ($row = mysql_fetch_assoc($game_info)) {
                            $game_data = $row;
                        }


                        $data['gamename'] = $game_data['name'];
                        $sql = "insert into cy_pay_ok (orderid,amount,username,roleid,paytype,serverid,servername,gameid,gamename,ip,imeil,create_time,agent,regagent,agent_username,prvite,second_agent,second_username,one_agent,one_username,type,attach) value ('{$data['orderid']}','{$data['amount']}','{$data['username']}','{$data['roleid']}','{$data['paytype']}','{$data['serverid']}','{$data['servername']}','{$data['gameid']}','{$data['gamename']}','{$data['ip']}','{$data['imeil']}','{$data['create_time']}','{$data['agent']}','{$data['regagent']}','{$data['agent_username']}','{$data['prvite']}','{$data['second_agent']}','{$data['second_username']}','{$data['one_agent']}','{$data['one_username']}','{$device}','{$data['attach']}')";
                        $rs = mysql_query($sql);
                    }


                }
            }
            echo "success";        //请不要修改或删除
        }
        //——请根据您的业务逻辑来编写程序(以上代码仅作参考)——


    } else {
        //验证失败
        echo "fail";    //请不要修改或删除

    }
}


set_time_limit(0);
$sql = "select id,orderid from cy_pay  where (beizhu is null or beizhu='') and paytype='zfb'  order by id desc limit 1";
$info = mysql_query($sql);
while ($row = mysql_fetch_assoc($info)) {
    $builder = new AlipayTradeQueryContentBuilder();
    $builder->setOutTradeNo($row['orderid']);
    $response = $alipaySevice->Query($builder);

    $res = json_decode(json_encode($response),true);
    $code = $res['code'];
    $tradeStatus = $res['trade_status'];

	if($res['code']==10000&&$tradeStatus=='TRADE_SUCCESS'){
		$_POST['out_trade_no'] = $res['out_trade_no'];
		$_POST['trade_no'] = $res['trade_no'];
        $_POST['trade_status'] = $res['trade_status'];
        $_POST['total_amount'] = $res['total_amount'];
        $_REQUEST = $_POST;
        zfbHandle();
	}
}
?>

 

展开阅读全文

没有更多推荐了,返回首页