Browse Source

金币充值回调

lizhen_gitee 3 years ago
parent
commit
5ed37e7907
3 changed files with 152 additions and 16 deletions
  1. 113 15
      application/api/controller/Pay.php
  2. 38 1
      application/common/model/Recharge.php
  3. 1 0
      application/extra/wallet.php

+ 113 - 15
application/api/controller/Pay.php

@@ -5,6 +5,7 @@ namespace app\api\controller;
 use app\common\controller\Api;
 use think\Db;
 use addons\epay\library\Service;
+use app\common\model\Wallet;
 /**
  * 充值
  */
@@ -14,15 +15,18 @@ class Pay extends Api
     protected $noNeedRight = ['*'];
 
     //vip用的
-    public function pay_config(){
-        $list = Db::name('pay_config')->where('is_show',1)->order('weight asc,id asc')->select();
-        $this->success('success',$list);
+    public function vip_config(){
+        $list = Db::name('payvip_config')->where('is_show',1)->order('weight asc,id asc')->select();
+        $data['vipconfig'] = $list;
+        $data['vip_endtime'] = model('wallet')->getWallet($this->auth->id,'vip_endtime');
+        $this->success('success',$data);
     }
 
     //vip用的,创建订单
-    public function recharge(){
+    public function vip_recharge(){
 
         $rc_id = input('rc_id',0);
+        $pay_type = input('pay_type','wechat');
         $uid = $this->auth->id;
 
         if(!$rc_id){
@@ -31,7 +35,7 @@ class Pay extends Api
 
 
         //赋值money
-        $recharge_config = Db::name('pay_config')->where('id',$rc_id)->find();
+        $recharge_config = Db::name('payvip_config')->where('id',$rc_id)->find();
         $money = $recharge_config['money'];
 
         if($money<=0)
@@ -45,30 +49,30 @@ class Pay extends Api
         //创建订单
         $data = [];
         $data['status'] = 0;
-        $pay_no = createUniqueNo('P',$uid);
+        $pay_no = createUniqueNo('V',$uid);
         $data['pay_no'] = $pay_no;
         $data['money'] = $money;
-        $data['payment_class'] = 'Wechat';
+        $data['payment_class'] = $pay_type;
         $data['user_id'] = $uid;
-        $data['ext_info'] =  json_encode(['subject'=>'充值会员支付']);
+        $data['ext_info'] =  json_encode(['subject'=>'充值vip支付']);
         $data['memo'] =  '充值会员支付';
         $data['createtime'] = time();
         //$data['payment'] = 'miniapp';
-        $data['payment'] = 'mp';
+        $data['payment'] = 'app';
         $orderid = Db::name('pay_order')->insertGetId($data);
 
         //创建回调
         $even_data = [];
         $even_data['event'] = 'success';
         $even_data['class'] = 'app\common\model\Recharge';
-        $even_data['method'] = 'paySucc';
+        $even_data['method'] = 'vippaysucc';
         $even_data['args'] = json_encode(['user_id'=>$uid,'days'=>$recharge_config['days']]);
         $even_data['pay_no'] = $pay_no;
         Db::name('pay_event')->insertGetId($even_data);
 
         $return = [
             'pay_no'=>$pay_no,
-            'title' => '充值订单支付',
+            'title' => '充值vip支付',
         ];
 
       /*  $this->success('success',$return);
@@ -76,19 +80,113 @@ class Pay extends Api
 
     public function topay(){*/
 
-        $openid = $this->auth->openid;
+        //$openid = $this->auth->openid;
+        /*$pay_no = input('pay_no');
+        $orderInfo = Db::name('pay_order')->where('pay_no',$pay_no)->find();*/
+        //下单
+        $params = [
+            'type'         => $pay_type,
+            'orderid'      => $pay_no,
+            'title'        => $data['memo'],
+            'amount'       => $data['money'],
+            //'amount'       => 0.01,
+            //'method'       => 'miniapp',
+            'method'       => 'app',
+            //'openid'       => $openid,
+            'notifyurl' => $this->request->root(true) . '/notify.php',
+            'returnurl' => '',
+        ];
+        $res = Service::submitOrder($params);
+        $this->success('请求成功',json_decode($res,true));
+    }
+
+
+    //金币充值
+    public function gold_config(){
+        $list = Db::name('paygold_config')->where('is_show',1)->order('weight asc,id asc')->select();
+        $data['goldconfig'] = $list;
+        $data['gold'] = model('wallet')->getWallet($this->auth->id,'gold');
+        $this->success('success',$data);
+    }
+
+    //充值金币 创建订单
+    public function gold_recharge(){
+
+        $rc_id     = input_post('rc_id',0);
+        $pay_type  = input_post('pay_type','wechat');
+        $freemoney = input_post('freemoney',0);
+        $uid = $this->auth->id;
+
+        if(!$rc_id && !$freemoney){
+            $this->error('请选择或填写充值金额');
+        }
+
+        //赋值money
+        $recharge_config = Db::name('paygold_config')->where('id',$rc_id)->find();
+        $money = $recharge_config['money'] ?: 0;
+        $gold  = $recharge_config['gold'] ?: 0;
+        //自由输入覆盖
+        if(!empty($freemoney)){
+            $money = floatval($freemoney);
+            $bili = config('site.money_to_gold') ?: 10;
+            $gold  = bcmul($money,$bili,0);
+        }
+
+        //
+        if($money<=0)
+        {
+            $this->error('支付金额必须大于0');
+        }
+        if($money > 10000){
+            $this->error('支付金额太大');
+        }
+
+        //创建订单
+        $data = [];
+        $data['status'] = 0;
+        $pay_no = createUniqueNo('P',$uid);
+        $data['pay_no'] = $pay_no;
+        $data['money'] = $money;
+        $data['payment_class'] = $pay_type;
+        $data['user_id'] = $uid;
+        $data['ext_info'] =  json_encode(['subject'=>'充值金币支付']);
+        $data['memo'] =  '充值金币支付';
+        $data['createtime'] = time();
+        $data['payment'] = 'app';
+        $orderid = Db::name('pay_order')->insertGetId($data);
+
+        //创建回调
+        $even_data = [];
+        $even_data['event'] = 'success';
+        $even_data['class'] = 'app\common\model\Recharge';
+        $even_data['method'] = 'goldpaysucc';
+        $even_data['args'] = json_encode(['user_id'=>$uid,'gold'=>$gold]);
+        $even_data['pay_no'] = $pay_no;
+        Db::name('pay_event')->insertGetId($even_data);
+
+        $return = [
+            'pay_no'=>$pay_no,
+            'title' => '充值金币支付',
+        ];
+
+        /*  $this->success('success',$return);
+      }
+
+      public function topay(){*/
+
+        //$openid = $this->auth->openid;
         /*$pay_no = input('pay_no');
         $orderInfo = Db::name('pay_order')->where('pay_no',$pay_no)->find();*/
         //下单
         $params = [
-            'type'         => 'wechat',
+            'type'         => $pay_type,
             'orderid'      => $pay_no,
             'title'        => $data['memo'],
             'amount'       => $data['money'],
             //'amount'       => 0.01,
             //'method'       => 'miniapp',
-            'method'       => 'mp',
-            'openid'       => $openid,
+            'method'       => 'app',
+            //'openid'       => $openid,
             'notifyurl' => $this->request->root(true) . '/notify.php',
             'returnurl' => '',
         ];

+ 38 - 1
application/common/model/Recharge.php

@@ -2,6 +2,7 @@
 namespace app\common\model;
 
 use \think\Db;
+
 //use \com\Pay\Pay;
 /**
  * 订单支付模型
@@ -13,7 +14,43 @@ class Recharge
     /**
      * 支付回调
      */
-    public function paySucc($orderId,$args){
+
+
+
+    public function goldpaysucc($orderId,$args){
+        $order = Db::name('pay_order')->where(array('pay_no'=>$orderId,'status'=>2))->find();
+
+        if( $order )
+        {
+
+            Db::startTrans();
+            //更新订单状态
+            $rs = Db::name('pay_order')->where(['id'=>$order['id']])->update(['status'=>1]);
+            if(!$rs){
+                Db::rollback();
+                filePut('[wallet][paySucc] pay_order update fail'.$orderId);
+                abort(500,lang('订单更新失败'));
+            }
+
+            //先充值
+            $result = model('Wallet')->lockChangeAccountRemain($args['user_id'],'gold',$args['gold'],10, '金币充值:'.$orderId,'pay_order',$order['id']);
+            if($result['status']===false)
+            {
+                Db::rollback();
+                filePut('[wallet][paySucc]网银充值入账更新余额失败 recharge money fail'.$orderId.$result['msg']);
+                abort(500,lang('网银充值入账 model wallet recharge money fail'));
+            }
+            Db::commit();
+
+
+            return true;
+        }else{
+            filePut('[wallet][paySucc]在线支付订单有误 '.$orderId);
+            abort(500,lang('model wallet fail'));
+            return false;
+        }
+    }
+    public function vippaysucc($orderId,$args){
         $order = Db::name('pay_order')->where(array('pay_no'=>$orderId,'status'=>2))->find();
 
         if( $order )

+ 1 - 0
application/extra/wallet.php

@@ -7,6 +7,7 @@ return [
         1 => '系统调节',
 
 
+        10 => '金币充值',
         11 => '视频通话',
         12 => '语音通话',
         13 => '文字聊天',