Sfoglia il codice sorgente

支付下单和回调方法

lizhen_gitee 1 anno fa
parent
commit
4142a9b525
1 ha cambiato i file con 200 aggiunte e 0 eliminazioni
  1. 200 0
      application/api/controller/Pay.php

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

@@ -0,0 +1,200 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use think\Db;
+use addons\epay\library\Service;
+/**
+ * 充值配置与充值订单
+ */
+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['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' => '',
+        ];
+
+        $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'=>10,
+            '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;
+
+    }
+
+    //////////////
+
+
+
+
+
+}