| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 | <?phpnamespace app\api\controller;use app\common\controller\Api;use think\Db;use addons\epay\library\Service;/** * 订单支付回调 */class Notify extends Api{    protected $noNeedLogin = ['*'];    protected $noNeedRight = ['*'];    //充值金币 异步回调对外方法    public function recharge_notify_base(){        //验签        $paytype = input('paytype','wechat');        $notify_file = $this->notify_log_start($paytype);        $pay = Service::checkNotify($paytype);        if (!$pay) {            echo '签名错误';            exit;        }        //验证,拿订单号等信息        $data = $pay->verify();        $out_trade_no = $data['out_trade_no'];        //订单查询        $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();        if(empty($info)){            echo $pay->success();            exit;        }        if($info['order_status'] != 0)        {            echo $pay->success();            exit;        }        //你可以在此编写订单逻辑        $rs = $this->recharge_notify_do($out_trade_no);        if($rs === false){            //不论结果都应返回success            echo $pay->success();            exit;        }else{            //不论结果都应返回success            echo $pay->success();            exit;        }        //默认        echo $pay->success();        exit;    }    //充值金币 逻辑    private function recharge_notify_do($out_trade_no){        Db::startTrans();        $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();        if (empty($orderInfo)) {            Db::rollback();            return false;        }        if($orderInfo['order_status'] != 0){            Db::rollback();            return false;        }        //逻辑开始        $args = json_decode($orderInfo['args'],true);        $result = model('Wallet')->lockChangeAccountRemain($orderInfo['user_id'],'gold',$args['gold'],10, '金币充值','pay_order',$orderInfo['id']);        if($result['status']===false)        {            Db::rollback();            return false;        }        //逻辑结束        //状态        $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);        if($ros === false) {            Db::rollback();            return false;        }        //默认提交        Db::commit();        return true;    }    //充值VIP 异步回调对外方法    public function vip_notify_base(){        //验签        $paytype = input('paytype','wechat');        $notify_file = $this->notify_log_start($paytype);        $pay = Service::checkNotify($paytype);        if (!$pay) {            echo '签名错误';            exit;        }        //验证,拿订单号等信息        $data = $pay->verify();        $out_trade_no = $data['out_trade_no'];        //订单查询        $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();        if(empty($info)){            echo $pay->success();            exit;        }        if($info['order_status'] != 0)        {            echo $pay->success();            exit;        }        //你可以在此编写订单逻辑        $rs = $this->vip_notify_do($out_trade_no);        if($rs === false){            //不论结果都应返回success            echo $pay->success();            exit;        }else{            //不论结果都应返回success            echo $pay->success();            exit;        }        //默认        echo $pay->success();        exit;    }    //充值金币 逻辑    private function vip_notify_do($out_trade_no){        Db::startTrans();        $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();        if (empty($orderInfo)) {            Db::rollback();            return false;        }        if($orderInfo['order_status'] != 0){            Db::rollback();            return false;        }        //逻辑开始        //先充值        $args = json_decode($orderInfo['args'],true);        $user_info = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->lock(true)->find();        if($user_info['vip_endtime'] < time()){            //过期了            $vip_endtime = time() + (intval($args['days']) * 86400);        }else{            //追加vip            $vip_endtime = $user_info['vip_endtime'] + (intval($args['days']) * 86400);        }        $update_data = [            'vip_endtime'=>$vip_endtime,        ];        $result = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->update($update_data);        if($result === false)        {            Db::rollback();            return false;        }        //tag任务赠送金币        //开通VIP 50金币        /*$task_rs = \app\common\model\TaskLog::tofinish($args['user_id'],9);        if($task_rs === false){            Db::rollback();            $this->error('完成任务赠送奖励失败');        }*/        //逻辑结束        //状态        $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);        if($ros === false) {            Db::rollback();            return false;        }        //默认提交        Db::commit();        return true;    }    //异步日志    private function notify_log_start($paytype = 'wechat'){        //记录支付回调数据        ignore_user_abort(); // run script in background        set_time_limit(30);        // 日志文件 start        $log_base_dir = '../paylog/'.$paytype.'/';        if (!is_dir($log_base_dir))        {            mkdir($log_base_dir, 0770, true);            @chmod($log_base_dir, 0770);        }        $notify_file = $log_base_dir.'notify.txt';        if(!file_exists($notify_file)) {            @touch($notify_file);            @chmod($notify_file, 0770);        }        if(filesize($notify_file)>5242880)//大于5M自动切换        {            rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');        }        if(!file_exists($notify_file)) {            @touch($notify_file);            @chmod($notify_file, 0770);        }        // 日志文件 end        //开始写入        $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();        if($_REQUEST && $paytype == 'alipay') {            file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收request]".json_encode($_REQUEST), FILE_APPEND);        } else {            $xml = file_get_contents("php://input");            file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);            $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);            file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流] ".json_encode($xmlObj), FILE_APPEND);        }        ini_set('display_errors','On');        return $notify_file;    }}
 |