Переглянути джерело

活动,支付异步回调

lizhen_gitee 1 рік тому
батько
коміт
9f72214339

+ 2 - 33
application/api/controller/Active.php

@@ -108,41 +108,10 @@ class Active extends Api
             $this->error('报名失败');
         }
 
-        //支付订单
-        $pay_order = [
-            'user_id'      => $this->auth->id,
-            'out_trade_no' => $data['order_no'],
-            'order_amount' => $info['pay_fee'],
-            'createtime'   => time(),
-            'pay_type'     => 'wechat',
-            'platform'     => 'miniapp',
-            'order_status' => 0,
-            'table_name'   => 'order',
-            'table_id'     => $order_id,
-        ];
-        $payorder_id = Db::name('pay_order')->insertGetId($pay_order);
-        if(!$payorder_id){
-            Db::rollback();
-            $this->error('报名失败');
-        }
+        Db::commit();
+        $this->success(1,$order_id);
 
-        //拉起支付
-        $notifyurl = $this->request->root(true) . '/api/notify/order_notify_base/paytype/wechat';
-        $returnurl = config('h5_url') . '/#/pages/public/paySuc';
-
-        $params = [
-            'type'      => 'wechat',
-            'orderid'   => $pay_order['out_trade_no'],
-            'title'     => '报名活动',
-            'amount'    => $pay_order['order_amount'],
-            'method'    => 'miniapp',
-            'openid'    => $this->auth->mini_openid,
-            'notifyurl' => $notifyurl,
-            'returnurl' => $returnurl,
-        ];
 
-        $res = Service::submitOrder($params);
-        $this->success('success',json_decode($res,true));
     }
 
 

+ 197 - 0
application/api/controller/Pay.php

@@ -0,0 +1,197 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use fast\Random;
+use think\Db;
+use addons\epay\library\Service;
+use think\Exception;
+/**
+ * 充值配置与充值订单
+ */
+class Pay extends Api
+{
+    protected $noNeedLogin = ['order_notify_base'];
+    protected $noNeedRight = ['*'];
+
+    //支付订单
+    //微信小程序、微信app下单使用。
+    public function pay_order(){
+        $pay_type = input('pay_type','wechat');
+        $platform = input('platform','miniapp');
+        $orderid = input('orderid','0');
+        $uid = $this->auth->id;
+
+        $orderinfo = Db::name('order')->where('id',$orderid)->where('user_id',$uid)->find();
+
+        //创建订单
+        $data['user_id'] = $uid;
+        $data['out_trade_no'] = createUniqueNo('P',$uid); // 数据库订单号加密
+        $data['order_amount'] = $orderinfo['pay_fee'];
+        $data['createtime'] = time();
+        $data['pay_type'] = $pay_type;
+        $data['platform'] = $platform;
+        $data['order_status'] = 0;
+        $data['table_name'] = 'order';
+        $data['table_id'] = $orderid;
+
+        $orderid = Db::name('pay_order')->insertGetId($data);
+
+        $openid = $this->auth->mini_openid;
+        //下单
+        $params = [
+            'type'         => $pay_type,
+            'orderid'      => $data['out_trade_no'],
+            'title'        => '支付订单',
+            'amount'       => $data['order_amount'],
+            'method'       => $platform,
+            'openid'       => $openid,
+            'notifyurl' => config('pay_notify_url').'/api/pay/order_notify_base/paytype/'.$pay_type,
+            'returnurl' => config('h5_url').'/#/page/',
+        ];
+
+        $res = Service::submitOrder($params);
+
+        if($pay_type == 'wechat'){
+            $this->success('success',json_decode($res,true));
+        }else{
+            $this->success('success',$res);
+        }
+    }
+
+    //异步回调对外方法
+    public function order_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)){
+            return $pay->success()->send();
+            exit;
+        }
+
+        if($info['order_status'] != 0)
+        {
+            return $pay->success()->send();
+            exit;
+        }
+        //你可以在此编写订单逻辑
+        $rs = $this->order_notify_do($out_trade_no);
+        if($rs === false){
+            //不论结果都应返回success
+            return $pay->success()->send();
+            exit;
+        }else{
+            //不论结果都应返回success
+            return $pay->success()->send();
+            exit;
+        }
+
+        //默认
+        return $pay->success()->send();
+        exit;
+    }
+
+    //异步逻辑
+    private function order_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;
+        }
+
+        //逻辑开始
+        $update = [
+            'status'=>1,
+            'paytime'=>time(),
+            'pay_type'=>$orderInfo['pay_type'],
+            'pay_out_trade_no'=>$out_trade_no,
+        ];
+        $rs_order = Db::name('order')->where('id',$orderInfo['table_id'])->update($update);
+        if($rs_order === 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;
+
+
+    }
+
+
+
+    //异步日志
+    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;
+
+    }
+
+
+}

+ 1 - 0
application/config.php

@@ -310,6 +310,7 @@ return [
         'secret'=>'b2b868518490f070c8106d73d7e6657f',
     ],
 
+    'pay_notify_url' => 'https://yanxuebaoming.huxiukeji.cn',
     'domain_cdnurl' => 'https://yanxuebaoming.huxiukeji.cn',
     'h5_url'        => 'https://yanxueweb.huxiukeji.cn',
 ];