Browse Source

余额充值及支付回调

panda 5 months ago
parent
commit
f52a4e4fe3

+ 62 - 161
application/api/controller/Notify.php

@@ -1,8 +1,10 @@
 <?php
+
 namespace app\api\controller;
 
 use app\common\controller\Api;
 use app\common\model\PayOrderModel;
+use app\common\model\Wallet;
 use app\utils\LogUtil;
 use think\Db;
 use addons\epay\library\Service;
@@ -26,124 +28,64 @@ class Notify extends Api
         LogUtil::getInstance('Api/'); //设置日志存入通道
     }
 
-    //人民币充值 异步回调对外方法
-    public function recharge_notify_base(){
-
+    // 余额充值 支付回调
+    public function recharge()
+    {
         //验签
-        $paytype = input('paytype','wechat');
-        $notify_file = $this->notify_log_start($paytype);
-        $pay = Service::checkNotify($paytype);
-        if (!$pay) {
-            echo '签名错误';
-            exit;
+        $pay_type = input('pay_type', 'wechat');
+        LogUtil::info("用户充值 {$pay_type}支付回调", self::LOG_MODULE, __FUNCTION__, $this->request->param());
+        if (!$pay = Service::checkNotify($pay_type)) {
+            LogUtil::info('签名错误', self::LOG_MODULE, __FUNCTION__);
+            return $this->error('签名错误 err01');
         }
 
         //验证,拿订单号等信息
-        $data = $pay->verify();
+        $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;
-        }
-
-        //逻辑开始
-        $order_amount = $orderInfo['order_amount'];
-        $result = model('Wallet')->lockChangeAccountRemain($orderInfo['user_id'],'money',$order_amount,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;
+        [$res, $msg] = PayOrderModel::recharge($out_trade_no);
+        if (!$res) {
+            LogUtil::info($msg, self::LOG_MODULE, __FUNCTION__);
+            return $this->error('签名错误 err02');
         }
-
-        //默认提交
-        Db::commit();
-        return true;
+        LogUtil::info('处理成功', self::LOG_MODULE, __FUNCTION__);
+        return $pay->success();
     }
 
-
     //充值VIP 异步回调对外方法
-    public function vip_notify_base(){
+    public function vip_notify_base()
+    {
 
         //验签
-        $paytype = input('paytype','wechat');
+        $paytype     = input('paytype', 'wechat');
         $notify_file = $this->notify_log_start($paytype);
-        $pay = Service::checkNotify($paytype);
+        $pay         = Service::checkNotify($paytype);
         if (!$pay) {
             echo '签名错误';
             exit;
         }
 
         //验证,拿订单号等信息
-        $data = $pay->verify();
+        $data         = $pay->verify();
         $out_trade_no = $data['out_trade_no'];
         //订单查询
-        $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();
+        $info = Db::name('pay_order')->where('out_trade_no', $out_trade_no)->find();
 
-        if(empty($info)){
+        if (empty($info)) {
             echo $pay->success();
             exit;
         }
 
-        if($info['order_status'] != 0)
-        {
+        if ($info['order_status'] != 0) {
             echo $pay->success();
             exit;
         }
         //你可以在此编写订单逻辑
         $rs = $this->vip_notify_do($out_trade_no);
-        if($rs === false){
+        if ($rs === false) {
             //不论结果都应返回success
             echo $pay->success();
             exit;
-        }else{
+        } else {
             //不论结果都应返回success
             echo $pay->success();
             exit;
@@ -155,7 +97,8 @@ class Notify extends Api
     }
 
     //充值金币 逻辑
-    private function vip_notify_do($out_trade_no){
+    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();
@@ -164,43 +107,42 @@ class Notify extends Api
             return false;
         }
 
-        if($orderInfo['order_status'] != 0){
+        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()){
+        $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);
-            $vip_type = 1;
-        }else{
+            $vip_type    = 1;
+        } else {
             //追加vip
             $vip_endtime = $user_info['vip_endtime'] + (intval($args['days']) * 86400);
-            $vip_type = 2;
+            $vip_type    = 2;
         }
         $update_data = [
-            'vip_endtime'=>$vip_endtime,
+            'vip_endtime' => $vip_endtime,
         ];
-        $result = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->update($update_data);
+        $result      = Db::name('user_wallet')->where('user_id', $orderInfo['user_id'])->update($update_data);
 
         //记录日志
         $log_data = [
-            'user_id' => $orderInfo['user_id'],
-            'before'  => $user_info['vip_endtime'],
-            'change_value'  => intval($args['days']) * 86400,
-            'remain'  => $vip_endtime,
-            'remark'  => '购买vip',
-            'createtime'  => time(),
-            'vip_type'  => $vip_type,
+            'user_id'      => $orderInfo['user_id'],
+            'before'       => $user_info['vip_endtime'],
+            'change_value' => intval($args['days']) * 86400,
+            'remain'       => $vip_endtime,
+            'remark'       => '购买vip',
+            'createtime'   => time(),
+            'vip_type'     => $vip_type,
         ];
         Db::name('user_vip_log')->insertGetId($log_data);
 
-        if($result === false)
-        {
+        if ($result === false) {
             Db::rollback();
             return false;
         }
@@ -209,8 +151,8 @@ class Notify extends Api
         //逻辑结束
 
         //状态
-        $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
-        if($ros === 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;
         }
@@ -220,87 +162,46 @@ class Notify extends Api
         return true;
     }
 
-
     //异步日志
-    private function notify_log_start($paytype = 'wechat'){
+    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))
-        {
+        $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)) {
+        $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自动切换
+        if (filesize($notify_file) > 5242880)//大于5M自动切换
         {
-            rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
+            rename($notify_file, $log_base_dir . 'notify_' . date('Y_m_d_H_i_s') . '.txt');
         }
-        if(!file_exists($notify_file)) {
+        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);
+        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);
+            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);
+            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');
+        ini_set('display_errors', 'On');
 
         return $notify_file;
 
     }
-
-    // 老年大学活动报名支付回调
-    public function university_event_wechat()
-    {
-        LogUtil::info('老年大学活动 微信支付回调', self::LOG_MODULE,__FUNCTION__, $this->request->param());
-        $pay = Service::checkNotify('wechat');
-        if (!$pay){
-            LogUtil::info('签名错误', self::LOG_MODULE,__FUNCTION__);
-            return $this->error('签名错误 err01');
-        }
-        $data = $pay->verify();
-        $out_trade_no = $data['out_trade_no'];
-        [$res,$msg] = PayOrderModel::university_event($out_trade_no);
-        if (!$res){
-            LogUtil::info($msg, self::LOG_MODULE,__FUNCTION__);
-            return $this->error('签名错误 err02');
-        }
-        LogUtil::info('处理成功', self::LOG_MODULE,__FUNCTION__);
-        return $pay->success();
-    }
-
-    // 老年大学活动报名支付回调
-    public function university_event_alipay()
-    {
-        LogUtil::info('老年大学活动 支付宝支付回调', self::LOG_MODULE,__FUNCTION__, $this->request->param());
-        $pay = Service::checkNotify('alipay');
-        if (!$pay){
-            LogUtil::info('签名错误', self::LOG_MODULE,__FUNCTION__);
-            return $this->error('签名错误 err01');
-        }
-        $data = $pay->verify();
-        $out_trade_no = $data['out_trade_no'];
-        [$res,$msg] = PayOrderModel::university_event($out_trade_no);
-        if (!$res){
-            LogUtil::info($msg, self::LOG_MODULE,__FUNCTION__);
-            return $this->error('签名错误 err02');
-        }
-        LogUtil::info('处理成功', self::LOG_MODULE,__FUNCTION__);
-        return $pay->success();
-    }
 }

+ 99 - 101
application/api/controller/Pay.php

@@ -3,8 +3,11 @@
 namespace app\api\controller;
 
 use app\common\controller\Api;
+use app\common\model\Wallet;
+use app\utils\CurlUtil;
 use think\Db;
 use addons\epay\library\Service;
+
 /**
  * 充值配置与充值订单
  */
@@ -14,167 +17,162 @@ class Pay extends Api
     protected $noNeedRight = ['*'];
 
     //vip用的
-    public function vip_config(){
-        $list = Db::name('payvip_config')->order('weigh asc,id asc')->select();
-        $data['vipconfig'] = $list;
-        $data['vip_endtime'] = model('wallet')->getWallet($this->auth->id,'vip_endtime');
-        $data['is_vip'] = $data['vip_endtime'] > time() ? 1 : 0;
-        $data['avatar'] = localpath_to_netpath($this->auth->avatar);
-        $this->success('success',$data);
+    public function vip_config()
+    {
+        $list                = Db::name('payvip_config')->order('weigh asc,id asc')->select();
+        $data['vipconfig']   = $list;
+        $data['vip_endtime'] = model('wallet')->getWallet($this->auth->id, 'vip_endtime');
+        $data['is_vip']      = $data['vip_endtime'] > time() ? 1 : 0;
+        $data['avatar']      = localpath_to_netpath($this->auth->avatar);
+        $this->success('success', $data);
     }
 
     //vip用的,创建订单
-    public function vip_recharge(){
+    public function vip_recharge()
+    {
 
-        $rc_id = input('rc_id',0);
-        $pay_type = input('pay_type','wechat');
+        $rc_id    = input('rc_id', 0);
+        $pay_type = input('pay_type', 'wechat');
         $platform = 'app';
-        $uid = $this->auth->id;
+        $uid      = $this->auth->id;
 
-        if(!$rc_id){
+        if (!$rc_id) {
             $this->error('请选择会员套餐');
         }
 
-        if(!$this->user_auth_limit()){
+        if (!$this->user_auth_limit()) {
             $this->error('请先完成实名认证');
         }
 
 
         //赋值money
-        $recharge_config = Db::name('payvip_config')->where('id',$rc_id)->find();
-        $money = $recharge_config['money'];
+        $recharge_config = Db::name('payvip_config')->where('id', $rc_id)->find();
+        $money           = $recharge_config['money'];
 
-        if($money<=0)
-        {
+        if ($money <= 0) {
             $this->error('支付金额必须大于0');
         }
-        if($money > 10000){
+        if ($money > 10000) {
             $this->error('支付金额太大');
         }
 
 
         //创建订单
-        $data = [];
-        $data['user_id'] = $uid;
-        $data['out_trade_no'] = createUniqueNo('P',$uid); // 数据库订单号加密
+        $data                 = [];
+        $data['user_id']      = $uid;
+        $data['out_trade_no'] = createUniqueNo('P', $uid); // 数据库订单号加密
         $data['order_amount'] = $money;
-        $data['createtime'] = time();
+        $data['createtime']   = time();
 
-        $data['pay_type'] = $pay_type;
-        $data['platform'] = $platform;
+        $data['pay_type']     = $pay_type;
+        $data['platform']     = $platform;
         $data['order_status'] = 0;
-        $data['table_name'] = 'vip_recharge';
-        $data['table_id'] = 0;
-        $data['args'] = json_encode(['days'=>$recharge_config['days']]);
+        $data['table_name']   = 'vip_recharge';
+        $data['table_id']     = 0;
+        $data['args']         = json_encode(['days' => $recharge_config['days']]);
 
         $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,
+            'type'      => $pay_type,
+            'orderid'   => $data['out_trade_no'],
+            'title'     => '支付订单',
+            'amount'    => $data['order_amount'],
+            'method'    => $platform,
 //            'openid'       => $openid,
-            'notifyurl' => config('pay_notify_url').'/api/notify/vip_notify_base/paytype/'.$pay_type,
+            'notifyurl' => config('pay_notify_url') . '/api/notify/vip_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);
+        if ($pay_type == 'wechat') {
+            $this->success('success', json_decode($res, true));
+        } else {
+            $this->success('success', $res);
         }
     }
 
-
-
     //人民币充值
-    public function money_config(){
-        $list = Db::name('paymoney_config')->order('weigh asc,id asc')->where('is_show',1)->select();
-        $data['config'] = $list;
-
-        $wallet = model('wallet')->getWallet($this->auth->id);
+    public function money_config()
+    {
+        $wallet         = (new Wallet())->getWallet($this->auth->id);
         $data['wallet'] = $wallet;
-        $this->success('success',$data);
+        $list           = Db::name('paymoney_config')->order('weigh asc,id asc')->where('is_show', 1)->select();
+        $data['config'] = $list;
+        $this->success('success', $data);
     }
 
     //人民币充值 创建订单
-    public function money_recharge(){
-
-        $rc_id     = input('rc_id',0);
-        $pay_type  = input('pay_type','wechat');
-        $platform  = 'app';
-        $freemoney = input('freemoney',0);
-        $uid = $this->auth->id;
-
-        if(!$rc_id && !$freemoney){
+    public function money_recharge()
+    {
+        $rc_id      = input('rc_id', 0);
+        $pay_type   = input('pay_type', 'wechat');
+        $platform   = input('platform', 'app');
+        $free_money = input('free_money', 0);
+        $user_id        = $this->auth->id;
+        if (!$rc_id && !$free_money) {
             $this->error('请选择或填写充值金额');
         }
 
-        //赋值money
-        if($rc_id){
-            $recharge_config = Db::name('paymoney_config')->where('id',$rc_id)->find();
-            $money = $recharge_config['money'] ?: 0;
-        }
-
-        //自由输入覆盖
-        if(!empty($freemoney)){
-            $rc_id = 0;
-            $money = floatval($freemoney);
+        if ($rc_id) {
+            //赋值money
+            $recharge_config = Db::name('paymoney_config')->where('id', $rc_id)->find();
+            $money           = $recharge_config['money'] ?: 0;
+        } else {
+            //自由输入覆盖
+            $money = floatval($free_money);
         }
 
-        //
-        if($money<=0)
-        {
+        if ($money <= 0) {
             $this->error('支付金额必须大于0');
         }
-        if($money > 10000){
+        if ($money > 10000) {
             $this->error('支付金额太大');
         }
 
-        //创建订单
-        $data = [];
-        $data['user_id'] = $uid;
-        $data['out_trade_no'] = createUniqueNo('M',$uid); // 数据库订单号加密
-        $data['order_amount'] = $money;
-        $data['createtime'] = time();
-
-        $data['pay_type'] = $pay_type;
-        $data['platform'] = $platform;
-        $data['order_status'] = 0;
-        $data['table_name'] = 'money_recharge';
-        $data['table_id'] = 0;
-        $data['args'] = '';
-
-        $orderid = Db::name('pay_order')->insertGetId($data);
+        // 创建支付订单
+        $remark    = '充值订单';
+        $orderData = [
+            'user_id'      => $user_id,
+            'out_trade_no' => createUniqueNo('M', $user_id),
+            'order_amount' => $money,
+            'pay_type'     => $pay_type,
+            'platform'     => $platform,
+            'table_name'   => 'money_recharge',
+            'table_id'     => 0,
+            'createtime'   => time(),
+            'args'         => json_encode([
+                'table_id' => 0,
+                'money'    => $money,
+                'remark'   => $remark
+            ], JSON_UNESCAPED_UNICODE),
+        ];
+        if (!Db::name('pay_order')->insert($orderData)) {
+            return $this->error('订单创建失败');
+        }
 
-//        $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/notify/recharge_notify_base/paytype/'.$pay_type,
+            'type'      => $orderData['pay_type'],
+            'orderid'   => $orderData['out_trade_no'],
+            'title'     => $remark,
+            'amount'    => $orderData['order_amount'],
+            'method'    => $orderData['platform'],
+            'notifyurl' => CurlUtil::getHttp("/api/notify/recharge/pay_type/{$pay_type}"),
             'returnurl' => '',
         ];
-
+        // 如果是小程序则需要添加 openid
+        if ($pay_type == 'wechat' && $platform == 'miniapp') {
+            $params['openid'] = $this->auth->mini_openid;
+        }
         $res = Service::submitOrder($params);
-
-        if($pay_type == 'wechat'){
-            $this->success('success',json_decode($res,true));
-        }else{
-            $this->success('success',$res);
+        if ($pay_type == 'wechat') {
+            $this->success('success', json_decode($res, true));
+        } else {
+            $this->success('success', $res);
         }
     }
-
-
-
 }

+ 0 - 2
application/api/controller/User.php

@@ -347,8 +347,6 @@ class User extends Api
         $this->success();
     }
 
-
-
     /**
      * 修改手机号
      *

+ 3 - 1
application/common/library/Auth.php

@@ -4,6 +4,7 @@ namespace app\common\library;
 
 use app\common\model\User;
 use app\common\model\UserRule;
+use app\common\model\Wallet;
 use fast\Random;
 use think\Config;
 use think\Db;
@@ -26,7 +27,7 @@ class Auth
     //默认配置
     protected $config = [];
     protected $options = [];
-    protected $allowFields = ['id', 'username', 'nickname', 'mobile', 'avatar', 'group_id'];
+    protected $allowFields = ['id', 'username', 'nickname', 'mobile', 'avatar', 'group_id','idcard_status'];
 
     public function __construct($options = [])
     {
@@ -402,6 +403,7 @@ class Auth
 
         $userinfo['avatar'] = localpath_to_netpath($userinfo['avatar']);
         $userinfo['chat_id'] = im_prefix($userinfo['id']);
+        $userinfo['balance'] = (new Wallet())->getWallet($userinfo['id'],'money');
         return $userinfo;
     }
 

+ 42 - 3
application/common/model/PayOrderModel.php

@@ -19,6 +19,44 @@ class PayOrderModel extends Model
     protected $updateTime = false;
     protected $deleteTime = false;
 
+    /**
+     * 余额充值支付回调
+     * @param $out_trade_no
+     * @return array
+     * @throws \think\Exception
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     * @throws \think\exception\PDOException
+     */
+    public static function recharge($out_trade_no)
+    {
+        Db::startTrans();
+        if (!$order = self::where('out_trade_no',$out_trade_no)->lock(true)->find()) {
+            Db::rollback();
+            return [false,'未找到订单'];
+        }
+
+        if ($order['order_status'] != 0) {
+            Db::rollback();
+            return [true,'订单已支付'];
+        }
+
+        //更新订单状态
+        if (!self::where(['out_trade_no' => $out_trade_no])->update(['order_status' => 1, 'notifytime' => time()])) {
+            Db::rollback();
+            return [false,'订单状态更新失败'];
+        }
+
+        //钱包更新
+        $walletService = new Wallet();
+        if (!$walletService->change($order['user_id'], $order['order_amount'], 'money', 10, '充值', 'pay_order', $order['table_id'])) {
+            Db::rollback();
+            return [false,'余额充值失败'];
+        }
+        Db::commit();
+        return [true,'操作成功'];
+    }
 
     /**
      * 老年大学活动支付回调
@@ -27,14 +65,15 @@ class PayOrderModel extends Model
      */
     public static function university_event($out_trade_no)
     {
-        if (!$info = self::where('out_trade_no',$out_trade_no)->find()){
+        Db::startTrans();
+        if (!$info = self::where('out_trade_no',$out_trade_no)->lock(true)->find()){
+            Db::rollback();
             return [false,'未找到订单'];
         }
         if ($info['order_status'] == 1){
+            Db::rollback();
             return [true,'订单已支付'];
         }
-
-        Db::startTrans();
         //更新订单状态
         if (!self::where(['id' => $info['id']])->update(['status' => 1])) {
             Db::rollback();

+ 8 - 6
application/common/model/Wallet.php

@@ -13,6 +13,7 @@ class Wallet extends Model
     // 日志变动类型
     const log_type = [
         1  => '系统调节',//money + -
+        10  => '用户充值',//money + -
         20 => '老年大学活动报名',//money + -
     ];
     // 操作钱包余额类型
@@ -48,15 +49,14 @@ class Wallet extends Model
 
     /**
      * 获取钱包余额
-     * @param string $username 用户编号
+     * @param int $user_id 用户编号
      * @param string $wallet_name 指定钱包名
      * @return array|float
      */
-    public function getWallet($user_id = '', $wallet_name = '')
+    public function getWallet($user_id = 0, $wallet_name = '')
     {
         //所有钱包余额
-        $wallet = Db::name('user_wallet')->lock(true)->where(['user_id' => $user_id])->find();
-        if (!$wallet) {
+        if (!$wallet = Db::name('user_wallet')->where(['user_id' => $user_id])->find()) {
             abort(500, '钱包余额获取失败');
         }
 
@@ -250,8 +250,10 @@ class Wallet extends Model
         return $this->message;
     }
 
-    public function getData()
+    public function getData($name = null)
     {
-        return $this->data;
+        $data = $this->data;
+        !empty($name) && $data = $this->data[$name];
+        return $data;
     }
 }