<?php

namespace xh\run\server\controller;

use xh\library\request;
use xh\library\mysql;
use xh\unity\callbacks;
use xh\unity\cog;
use xh\library\functions;
use xh\unity\sms;
use xh\unity\encrypt;

//支付宝-全自动版-服务端
class alipayAutomatic
{

    private $mysql;

    public function __construct()
    {
        $this->mysql = new mysql();
    }

    //验证服务端KEY
    protected function keyVerification()
    {
        $key = request::filter('request.key');
        //$key = (request::filter('get.key') or request::filter('post.key'));
        //验证key是否正确
        if (cog::read("server")['key'] != $key) functions::json(-1, '通讯失败');
    }

    //安卓验证账号密码
    protected function loginAndroid()
    {
        $encrpty = new encrypt();
        $data = json_decode($encrpty->Decode(request::filter('post.data'), cog::read("server")['key']), true);
        $username = $data['member_id'];
        $pwd = $data['pwd'];
        $DEVICE_Key = $data['device_Key'];
        $find = $this->mysql->query("client_user", "username='{$username}'")[0];
        if (is_array($find)) {
            //开始验证密码
            if (functions::pwd($pwd, $find['token']) != $find['pwd']) functions::json_encode(-2, '密码错误!');
            $find['device'] = $this->mysql->query("client_alipay_automatic_account", "user_id={$find['id']} and key_id='{$DEVICE_Key}'")[0];
            if (!is_array($find['device'])) functions::json_encode(-4, 'DEVICE Key识别失败!');
            $find['data'] = $data;

            return $find;
        }
        //如果没找到用户名，检测是否为手机号
        if (!functions::isMobile($username)) functions::json_encode('-6', request::filter('post.data', '', 'htmlspecialchars'));
        //检测手机号码
        $find = $this->mysql->query("client_user", "phone={$username}")[0];
        if (is_array($find)) {
            //开始验证密码
            if (functions::pwd($pwd, $find['token']) != $find['pwd']) functions::json_encode(-2, '密码错误!');
            $find['device'] = $this->mysql->query("client_alipay_automatic_account", "user_id={$find['id']} and key_id='{$DEVICE_Key}'")[0];
            if (!is_array($find['device'])) functions::json_encode(-3, 'DEVICE Key识别失败!');
            $find['data'] = $data;

            return $find;
        }
        functions::json_encode(-3, '手机号码输入有误');
    }

    //安卓获取二维码生成任务 -> 废除
    public function orderGet()
    {
        $user = $this->loginAndroid();
        //查询准备生成二维码的订单
        $order = $this->mysql->query('client_alipay_automatic_orders', "alipay_id={$user['device']['id']} and status=1 and user_id={$user['id']}", "id,amount,trade_no");
        //更新心跳
        $this->mysql->update("client_alipay_automatic_account", ['android_heartbeat' => time()], "id={$user['device']['id']}");
        functions::json_encode(200, 'success', $order[0]);
    }


    //安卓上载二维码到服务器
    public function uploadCode()
    {
        $user = $this->loginAndroid();
        //上载二维码
        $order_id = $user['data']['order_id'];//订单ID
        $qrcode = $user['data']['qrcode'];//支付二维码
        $order_eck = $this->mysql->query("client_alipay_automatic_orders", "status=4 and trade_no={$order_id}")[0];

        if (!is_array($order_eck)) {
            $this->mysql->update("client_alipay_automatic_orders", [
                'status' => 2,
                'qrcode' => $qrcode
            ], "alipay_id={$user['device']['id']} and trade_no={$order_id}");
        }

        functions::json_encode(200, 'success');
    }

    //获取所有需要登录的支付宝账户，并处理一些事物
    public function loginGet()
    {
        $this->keyVerification();
        $NowTime = time() - 120;
        $find = $this->mysql->query("client_alipay_automatic_account", "status=2 and login_time>{$NowTime}", "id,user_id,key_id");
        $this->mysql->update("client_alipay_automatic_account", ['status' => 3], "status=2");
        //判定支付宝掉线
        $droppedResult = $this->mysql->query('client_alipay_automatic_account', "status!=4 and status!=1 and login_time<{$NowTime} or status=6");
        //判断异常的支付宝账户，并发送短信
        $errorResult = $this->mysql->query("client_alipay_automatic_account", "status=5");
        //更改状态
        $this->mysql->update("client_alipay_automatic_account", ['status' => 1, 'training' => 2, 'receiving' => 2], "status!=4 and status!=1 and login_time<{$NowTime} or status=6");
        if (count($errorResult) > 0) {
            foreach ($errorResult as $error) {
                $find_user = $this->mysql->query("client_user", "id={$error['user_id']}")[0];
                if (is_array($find_user)) {
                    //发送短信
                    (new sms())->sendError($find_user['phone'], $error['name']);
                    $this->mysql->update("client_alipay_automatic_account", ['status' => 1], "id={$error['id']}");
                }
            }
        }
        $erroralipay = array_merge($droppedResult, $errorResult);
        functions::json(200, ' [' . date("Y/m/d H:i:s", time()) . ']: alipayAutomatic Success', [
            'login'   => ['list' => $find, 'num' => count($find)],
            'dropped' => ['list' => $erroralipay, 'num' => count($erroralipay)]
        ]);
    }

    //上载支付宝二维码
    public function uploadLoginImg()
    {
        $this->keyVerification();
        $id = request::filter('post.id');
        $login_img = request::filter('post.img');
        if (empty($login_img)) functions::json(-2, ' [' . date("Y/m/d H:i:s", time()) . ']: 支付宝ID->' . $id . ' 没有截取到登录二维码');
        $this->mysql->update("client_alipay_automatic_account", [
            'status'    => 7,
            'login_img' => str_replace("@", "+", $login_img)
        ], "id={$id}");
        functions::json(200, ' [' . date("Y/m/d H:i:s", time()) . ']: 支付宝ID->' . $id . ' 登录二维码上载完毕');
    }

    //上载登录成功，以及更新支付宝信息
    public function uploadLoginData()
    {
        $this->keyVerification();
        $id = request::filter('post.id');
        $name = request::filter('post.name');
        if (trim($name) == "") $name = '商户' . $id;
        $this->mysql->update("client_alipay_automatic_account", [
            'name'        => $name,
            'status'      => 4,
            'login_time'  => time(),
            'active_time' => time()
        ], "id={$id}");
        functions::json(200, ' [' . date("Y/m/d H:i:s", time()) . ']: 支付宝ID->' . $id . ' 登录成功');
    }

    //上载异常通知
    public function uploadLoginError()
    {
        $this->keyVerification();
        $id = request::filter('post.id');
        $this->mysql->update("client_alipay_automatic_account", [
            'status'    => 5,
            'training'  => 2,
            'receiving' => 2
        ], "id={$id}");
        functions::json(200, ' [' . date("Y/m/d H:i:s", time()) . ']: 支付宝ID->' . $id . ' 异常通知成功');
    }
	public function curl_post($sUrl, $aHeader, $aData){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $sUrl);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($aData));
    $sResult = curl_exec($ch);
    if($sError=curl_error($ch)){
        die($sError);
    }
    curl_close($ch);
    return $sResult;
	}

    //上载订单通知
    public function uploadOrder()
    {
		//file_put_contents("ddlog.txt", json_encode($_POST));
        $this->keyVerification();
        $order = intval(request::filter('post.id'));
        $uid = intval(request::filter('post.uid'));
        $orderid = intval(request::filter('post.orderid'));
        $keyid = str_replace("￥", '', request::filter('post.keyid'));

		$find_order = $this->mysql->query('client_alipay_automatic_orders', "id={$order} and status=2")[0];
        $id=$find_order['alipay_id'];
		$fid=$find_order['uid'];
		$kuk=$find_order['cookie'];
		
		
		///接收过来再次查单以防错误回调
/**********************************************************************************************************************************************/		
               $header="Cookie:".$find_order['cookie'];
                
                //请求地址
                $url = "https://pay.sc.weibo.com/aj/pc/biz/list?biz_id=".$find_order['order_id'];

                $context_options = array(
                 'http' =>
                  array(
                   'method' => "GET",
                   'header' => $header,
                  ));
                $context = stream_context_create($context_options);
                //获取资源
                $paylist = file_get_contents($url,FALSE,$context);
				 $arr = json_decode($paylist, true);
				 $res=$arr['data']['biz'];
				 $amount=$res[0]['amount'];
				 $status=$res[0]['status'];
				 $out_pay_id=$res[0]['order_id'];
				 $status_desc=$res[0]['status_desc'];		
				 $status_name='交易成功';
		file_put_contents("log/回调检测查单.txt", date('Y-m-d H:i:s')."|订单ID:|".$find_order['id'].'|'."|状态:|".$status_desc.'|'."|查单url:|".$url.'|'."\n", FILE_APPEND);
				 $a=$status;
				 $b=2;

				 
		if ($find_order['order_id'] > 0 && $a == $b && $status_desc == $status_name) {		 
		//检测成功开始领包回调
/**********************************************************************************************************************************************/	


	
        //$find_appid = $this->mysql->query("client_alipay_automatic_appid", "status=4 and user_id={$find_order['user_id']} and weid={$find_order['alipay_id']} and amount<10000");
        $find_appid = $this->mysql->query("client_alipay_automatic_appid", "status=4 and user_id={$find_order['user_id']} and amount<10000");
        $count_alipay = count($find_appid);
   
        $find_appid = $find_appid[mt_rand(0, $count_alipay - 1)];
     
		$shouid=$find_appid['appid'];
		$cookieSeller=$find_appid['cookie'];
        if($syrmb<201){
        	
        	
           $find_ordera = $this->mysql->query('client_alipay_automatic_orders', "id={$order}")[0];
		

        	
		       
		        $iad=$find_appid['id']; 
		        $all_amount=$find_appid['all_amount'];   
		        $max_amount=$find_appid['max_amount'];
		        $jr_amount=$find_appid['amount'];
		        $today_number=$find_appid['today_number'];
		        $all_number=$find_appid['all_number'];
				$appid=$find_appid['appid']; //收包ID
        	
        	
/********************************************************************************************************************************************************/        	
   // 发红包信息至收款人     	
             $cookie='Cookie:'.$kuk;
             $msgid=$shouid;  //收包id
             $payercookie=$cookie; //发包人cookie
/********************************************************************************************************************************************************/ 
//先互相关注 在发包
//先关注发包人关注收包人微博 // 请注意 只能他妈电脑获取得cookie 否则无效！
                $addUrl = 'https://weibo.com/aj/f/followed?ajwvr=6';
                //post数据
                $addData = array(
                    'uid' => $msgid,
                    'f' => '1',
                    'refer_flag' => '1005050001_',
                    'location' => 'page_100505_home',
                    'oid' => $msgid,
                    'wforce' => '1',
                    'nogroup' => '1',
                    'refer_from' => 'profile_headerv6',
                    'template' => '7',
                    'special_focus' => '1',
                    'isrecommend' => '1',
                    'is_special' => '0',
                    'redirect_url' => '%2Fp%2F1005057345773101%2Fmyfollow%3Fgid%3D4444185002098079%23place',
                    '_t' => '0'
                );
                
                $refer='https://weibo.com/u/'.$msgid.'?is_hot=1&noscale_head=1';
                
			    $Referer='Referer:'.$refer;
                $addHeader = array('Host:weibo.com','Origin:https://weibo.com','Content-type:application/x-www-form-urlencoded','User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36','X-Requested-With:XMLHttpRequest',$Referer,$payercookie);
                
                
                
                $addResult = $this->curl_post($addUrl, $addHeader, $addData);
                //file_put_contents("addpayResult.txt", $addResult);
                
                $addResData = json_decode($addResult, true);
                $addcode=$addResData['code'];
                //100000 代表成功 详情看日志 
                file_put_contents("log/paycode.txt", date('Y-m-d H:i:s')."|发包关注收包:|".$addcode.'|'."\n", FILE_APPEND);
                
                //file_put_contents("addResData.txt", $addResData);
                
///////////////////////////////然后收包人关注发包人


				if($addcode==100000){
                	
                	
                $Sellerookie='Cookie:'.$cookieSeller;	
                $aadfUrl = 'https://weibo.com/aj/f/followed?ajwvr=6';
                //post数据
                $aadfData = array(
                    'uid' => $fid,
                    'f' => '1',
                    'refer_flag' => '1005050001_',
                    'location' => 'page_100505_home',
                    'oid' => $fid,
                    'wforce' => '1',
                    'nogroup' => '1',
                    'refer_from' => 'profile_headerv6',
                    'template' => '7',
                    'special_focus' => '1',
                    'isrecommend' => '1',
                    'is_special' => '0',
                    'redirect_url' => '%2Fp%2F1005057345773101%2Fmyfollow%3Fgid%3D4444185002098079%23place',
                    '_t' => '0'
                );
                
                $refer='https://weibo.com/u/'.$fid.'?is_hot=1&noscale_head=1';
                
			    $Referer='Referer:'.$refer;
                $aadfHeader = array('Host:weibo.com','Origin:https://weibo.com','Content-type:application/x-www-form-urlencoded','User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36','X-Requested-With:XMLHttpRequest',$Referer,$Sellerookie);
                
                
                
                $aadfResult = $this->curl_post($aadfUrl, $aadfHeader, $aadfData);
               // file_put_contents("aadfResult.txt", $aadfResult);
                
                $aadfResData = json_decode($aadfResult, true);
                $aadfcode=$aadfResData['code'];
                //100000 代表成功 详情看日志 
				file_put_contents("log/takeusercode.txt", date('Y-m-d H:i:s')."|收包关注发包:|".$aadfcode.'|'."\n", FILE_APPEND);
                }



/********************************************************************************************************************************************************/ 
                            
                $sUrl = 'https://mall.e.weibo.com/aj/redenvelope/sendmsg';
                
                $aData = array(
                    'msgid' => $msgid,
                    'msgtype' => '1',
                    'set_id'   => $orderid,
                    '_t'      => '0'
                );
                
                $aHeader = array('Content-type:application/x-www-form-urlencoded','User-Agent:Mozilla/5.0 (Linux; Android 5.1.1; DUK-AL20 Build/LMY48Z) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/39.0.0.0 Safari/537.36 Weibo (HUAWEI-DUK-AL20__weibo__9.7.2.1__android__android5.1.1))','X-Requested-With:XMLHttpRequest',$cookie);
                $sResult = $this->curl_post($sUrl, $aHeader, $aData);
                //file_put_contents("sResult.txt", $sResult);
                $aResData = json_decode($sResult, true);	
                $code=$aResData['code'];
/********************************************************************************************************************************************************/                
              
              
        		if($code==100000){
/********************************************************************************************************************************************************/	
//小于200金额得开始收包！

         	   $xheader="X-Requested-With:com.sina.weibo\r\nx-user-agent:HUAWEI-DUK-AL20__weibo__9.7.2.1__android__android5.1.1\r\nUser-Agent:Mozilla/5.0 (Linux; Android 5.1.1; DUK-AL20 Build/LMY48Z) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/39.0.0.0 Safari/537.36 Weibo (HUAWEI-DUK-AL20__weibo__9.7.2.1__android__android5.1.1)\r\nCookie:".$cookieSeller."\r\n";
                
                //请求地址
                $urla = "https://mall.e.weibo.com/h5/redenvelope/draw?set_id=".$orderid."&sinainternalbrowser=topnav&portrait_only=1&from=1097295010&weiboauthoruid=";
                //headers请求头
                $context_options = array(
                 'http' =>
                  array(
                   'method' => "GET",
                   'header' => $xheader,
                  ));
                $contexta = stream_context_create($context_options);
                //获取资源
                $html = file_get_contents($urla,FALSE,$contexta);
                
//file_put_contents("html.txt", $html);

         	    $matches = array();
				preg_match_all('( <p class="red_amount">(.*)</p>)', $html, $matches);
				$money=$matches[1][0];
			
				$arry=empty($money);
				
			    
/********************************************************************************************************************************************************/
				
				if($arry==false){
					
				$rea = callbacks::curl(URL_ROOT . '/server/callback/takelist', http_build_query(['id' => $order,'userid' => $find_order['user_id'],'amount' =>$money,'fid' => $fid,'seler_uid' => $appid,'set_id' => $orderid,'kuk' => $kuk]));
				
					
					
					
				$this->mysql->update("client_alipay_automatic_orders", [
                'syrmb'         => $find_ordera['syrmb']-$money,
            	 ], "id={$find_ordera['id']}");
				
				
        		 
        		 
              	$ins=$this->mysql->update("client_alipay_automatic_appid", [
		                'all_amount'         => $all_amount+$money,
		                'amount'             => $jr_amount+$money,
		                'today_number'        => $today_number+1,
		                'all_number'         => $all_number+1,
		            ], "id={$iad}");

			
	}    
		            
		}

                
                
        	
        }  


            
            

        
        
        

     
        if (is_array($find_order)) {
            $this->mysql->update("client_alipay_automatic_orders", [
                'status'        => 4,
                'pay_time'      => time(),
                'callback_from' => 'app',
            ], "id={$find_order['id']}");
            $remark = ' - 订单信息：' . $order;
            $average = 1;

        } else {
            $remark = ' - 该订单不是第三方交易订单';
            $average = 0;
        }
        //查询用户信息
        $find_uid = $this->mysql->query("client_alipay_automatic_account", "id={$id}")[0]['user_id'];
        
  
        
        //写到交易记录
        $this->mysql->insert("client_pay_record", [
            'pay_time'     => time(),
            'amount'       => $money,
            'user_id'      => $find_uid,
            'pay_note'     => '[公开版]支付宝ID：' . $order . $remark,
            'types'        => 2,
            'version_code' => 'alipay_auto',
            'average'      => $average
        ]);
   

        $result = callbacks::curl(URL_ROOT . '/server/callback/alipay', http_build_query(['id' => $id]));

        functions::json(200, ' [' . date("Y/m/d H:i:s", time()) . ']: 这里是支付宝回调-订单ID->' . $order . ' 订单处理完成' . $result);
        
        //判断发过来的是否是成功的 
		}else{
			
		functions::json(200, ' [' . date("Y/m/d H:i:s", time()) . ']: 这里是支付宝回调-订单ID->' . $order . ' 有异常未至付订单已拦截');	
			
		}   
    }

    public function pcUploadOrder()
    {

        //$this->keyVerification();
        $remark = request::filter('request.remark');
        if (strpos($remark, ')') !== false) {
            $remark = substr($remark, strpos($remark, ')') + 1);

        } elseif (strpos($remark, 'ORDER') !== false) {
            $remark = explode("QX", str_replace("ORDER", "", $remark));
        }

        $remark = explode('|', $remark);
        if (empty($remark)) functions::json(500, 'remark信息错误');
        $log_id = $this->mysql->insert("order_log", [
            'id'        => $remark[0],
            'order_id'  => $remark[1],
            'money'     => request::filter('post.money'),
            'json_data' => json_encode($_REQUEST, JSON_UNESCAPED_UNICODE),
            'type'      => 'pc-alypay',
            'add_time'  => time(),
        ]);

        $id = $remark[0];
        $money = str_replace("￥", '', request::filter('request.money'));
        $money = sprintf("%.2f", $money);
        $no = request::filter('request.orderNo');
        $order = $remark[1];
        $today_money = str_replace("￥", '', request::filter('post.today_money'));
        $today_money = number_format($today_money, 2);

        $today_pens = intval(request::filter('post.today_pens'));
        $find_order = $this->mysql->query('client_alipay_automatic_orders', "alipay_id={$id} and status=2 and amount={$money} and id={$order}")[0];
        if (is_array($find_order)) {
            $update_id = $this->mysql->update("client_alipay_automatic_orders", [
                'status'        => 4,
                'pay_time'      => time(),
                'callback_from' => 'pc',
                'no'            => $no,
            ], "id={$find_order['id']}");
            $remark = ' - 订单信息：' . $order;
            $average = 1;
            if ($log_id > 0 && $update_id !== false) {
                $this->mysql->update("order_log", [
                    'status' => 1,
                ], "log_id={$log_id}");
            }

        } else {
            $order = $this->mysql->query('client_alipay_automatic_orders', "id={$remark[1]}", 'status,amount')[0];
            if (is_array($order) && $order['status'] == 4) {
                $this->mysql->update("order_log", [
                    'status' => 1,
                ], "log_id={$log_id}");
                exit('ok');
            }
            functions::json(700, '订单已成功或失效,订单信息' . json_encode($_REQUEST));

        }

        //查询用户信息
        //$find_uid = $this->mysql->query("client_alipay_automatic_account", "id={$id}")[0]['user_id'];
        //写到交易记录
//        $this->mysql->insert("client_pay_record", [
//            'pay_time'     => time(),
//            'amount'       => $money,
//            'user_id'      => $find_uid,
//            'pay_note'     => '[公开版]支付宝ID：' . $order . $remark,
//            'types'        => 2,
//            'version_code' => 'alipay_auto',
//            'average'      => $average
//        ]);
        //更新当前支付宝账号的实时统计
        //$this->mysql->update("client_alipay_automatic_account", ['today_money' => $today_money, 'today_pens' => $today_pens], "id={$id}");

        $result = callbacks::curl(URL_ROOT . '/server/callback/alipay', http_build_query(['id' => $id]));
        echo 'ok';

        //  functions::json(200, ' [' . date("Y/m/d H:i:s", time()) . ']: 这里是支付宝回调-支付宝ID->' . $order . ' 订单处理完成' . $result);
    }

    public function testPcUploadOrder()
    {

        //$this->keyVerification();
        $remark = request::filter('request.remark');
        $remark = explode('|', $remark);
        if (empty($remark)) exit('remark信息错误');

        $id = $remark[0];
        $money = str_replace("￥", '', request::filter('request.money'));
        $money = sprintf("%.2f", $money);
        $no = request::filter('request.orderNo');
        $order = $remark[1];
        $today_money = str_replace("￥", '', request::filter('post.today_money'));
        $today_money = number_format($today_money, 2);

        $today_pens = intval(request::filter('post.today_pens'));
        $find_order = $this->mysql->query('client_alipay_automatic_orders', "alipay_id={$id} and status=2 and amount={$money} and id={$order}")[0];
        echo "alipay_id={$id} and status=2 and amount={$money} and id={$order}";

        if (is_array($find_order)) {
            $this->mysql->update("client_alipay_automatic_orders", [
                'status'   => 4,
                'pay_time' => time(),
                'no'       => $no,
            ], "id={$find_order['id']}");
            $remark = ' - 订单信息：' . $order;
            $average = 1;
        } else {
            exit('订单已成功或失效,订单信息' . json_encode($_REQUEST));
        }
        //查询用户信息
        $find_uid = $this->mysql->query("client_alipay_automatic_account", "id={$id}")[0]['user_id'];
        //写到交易记录
        echo $this->mysql->insert("client_pay_record", [
            'pay_time'     => time(),
            'amount'       => $money,
            'user_id'      => $find_uid,
            'pay_note'     => '[公开版]支付宝ID：' . $order . $remark,
            'types'        => 2,
            'version_code' => 'alipay_auto',
            'average'      => $average
        ]);
        //更新当前支付宝账号的实时统计
        $this->mysql->update("client_alipay_automatic_account", ['today_money' => $today_money, 'today_pens' => $today_pens], "id={$id}");

        $result = callbacks::curl(URL_ROOT . '/server/callback/alipay', http_build_query(['id' => $id]));
        echo 'ok';

        //  functions::json(200, ' [' . date("Y/m/d H:i:s", time()) . ']: 这里是支付宝回调-支付宝ID->' . $order . ' 订单处理完成' . $result);
    }

    //程序自杀通知
    public function cillself()
    {
        $this->keyVerification();
        $id = request::filter('post.id');
        $this->mysql->update("client_alipay_automatic_account", [
            'status'    => 1,
            'training'  => 2,
            'receiving' => 2
        ], "id={$id}");
        functions::json(200, ' [' . date("Y/m/d H:i:s", time()) . ']: 支付宝ID->' . $id . ' 自杀成功');
    }


    function appCallback()
    {
        $callback_list = request::filter('request.callback_list');
        $callback_list = json_decode($callback_list, true);
        foreach ($callback_list as $val) {
            $remark = explode('|', $val['tradeRemark']);

            if (!empty($remark)) {

                $id = $remark[0];
                $money = str_replace("￥", '', $val['tradeAmount']);
                $money = sprintf("%.2f", $money);
                $no = request::filter('request.tradeNo');
                $pay_time = request::filter('request.tradeTime');
                $order = $remark[1];

                $find_order = $this->mysql->query('client_alipay_automatic_orders', "alipay_id={$id} and amount={$money} and id={$order}", 'id,status')[0];
                if (is_array($find_order)) {
                    if ($find_order['status'] == 4) {
                        continue;
                        //成功的信息
                    } else {

                        $this->mysql->update("client_alipay_automatic_orders", [
                            'status'        => 4,
                            'pay_time'      => time(),
                            'callback_from' => 'app-call',
                            'no'            => $no,
                        ], "id={$find_order['id']}");

                        $result = callbacks::curl(URL_ROOT . '/server/callback/alipay', http_build_query(['id' => $id]));
                        $result = json_decode($result, true);
                        if ($result['code'] == 200) {
                            //插入日志
                            continue;
                        }
                    }

                }

                //失败的全写入日志
                $this->mysql->insert("app_log", [
                    'alipay_id' => $id,
                    'order_id'  => $order,
                    'money'     => $money,
                    'no'        => $no,
                    'pay_time'  => $pay_time,
                ]);

            }

        }
        functions::json(200, '已接收到请求');

    }
}
