Browse Source

增加充值

15954078560 2 years ago
parent
commit
8b8ba172d3
3 changed files with 62 additions and 66 deletions
  1. 2 1
      application/api/controller/Index.php
  2. 28 65
      application/api/controller/User.php
  3. 32 0
      extend/wxpay/WxPay.php

+ 2 - 1
application/api/controller/Index.php

@@ -517,7 +517,6 @@ class Index extends Api
             $rechar_order['pay_type'] = 'wechat';
             $rechar_order['relation_id'] = $rs;
             $rechar_order['createtime'] = time();
-            $rechar_order['money'] = $total_amount;
 
             $result = Db::name('rechar_order')->insertGetId($rechar_order);
             if (!$result) {
@@ -525,6 +524,8 @@ class Index extends Api
                 $this->error('网络延迟,请稍后再试');
             }
 
+            Db::commit();
+
             //构建支付链接数据
             $wxData['body'] = '报名活动支付';
             $wxData['out_trade_no'] = $rechar_order['order_no'];

+ 28 - 65
application/api/controller/User.php

@@ -9,6 +9,7 @@ use fast\Random;
 use think\Config;
 use think\Validate;
 use think\Db;
+use wxpay;
 
 /**
  * 会员接口
@@ -1493,83 +1494,45 @@ class User extends Api
 
     //充值
     public function recharge() {
-        //检查是否已经开通体验会员
-        if ($this->auth->experiencetime >= time()) {
-            $this->error('您已开通体验会员,不能重复开通');
-        }
-
         $id = input('id', 0, 'intval');
         if (!$id) {
             $this->error('参数缺失');
         }
 
-        $info = Db::name('vip')->where(['id' => $id])->find();
+        $info = Db::name('recharge')->where(['id' => $id])->find();
         if (!$info) {
-            $this->error('会员不存在');
-        }
-        if ($info['status'] != 1) {
-            $this->error('体验会员已下架');
+            $this->error('充值金额不存在');
         }
         if ($info['price'] <= 0) {
-            $this->error('会员价格异常');
-        }
-        //体验会员等级必须大于成长值会员
-        if ($id <= $this->auth->maxlevel) {
-            $this->error('请开通更高等级体验会员');
-        }
-        //判断余额
-        if ($this->auth->money < $info['price']) {
-            $this->success('您的余额不足,请先去充值', ['code' => 2]);
+            $this->error('充值价格异常');
         }
 
-        $data['user_id'] = $this->auth->id;
-        $data['vip_id'] = $id;
-        $data['title'] = $info['title'];
-        $data['level'] = $info['level'];
-        $data['growthvalue'] = $info['growthvalue'];
-        $data['free'] = $info['free'];
-        $data['price'] = $info['price'];
-        $data['endtime'] = time() + $info['day'] * 86400;
-        $data['vipdiscount'] = $info['vipdiscount'];
-        $data['birthdiscount'] = $info['birthdiscount'];
-        $data['manypeople'] = $info['manypeople'];
-        $data['createtime'] = time();
-
-        //开启事务
-        Db::startTrans();
+        //生成支付订单记录
+        $rechar_order['user_id'] = $this->auth->id;
+        $rechar_order['order_no'] = date('YmdHis', time()) . rand(10000000, 99999999); //微信订单编号
+        $rechar_order['money'] = $info['price'];
+        $rechar_order['purpose'] = 2; //充值用途:1=支付订单,2=充值,3=开通会员
+        $rechar_order['pay_type'] = 'wechat';
+        $rechar_order['relation_id'] = $id;
+        $rechar_order['createtime'] = time();
 
-        //添加开通记录
-        $rs = Db::name('vip_log')->insertGetId($data);
-        if (!$rs) {
-            Db::rollback();
-            $this->error('开通失败');
-        }
-        //扣除余额
-        $result = create_log(-$info['price'], '开通会员', $this->auth->id, 4, $rs);
-        if ($result != 1) {
-            Db::rollback();
-            $this->error('资金异常,请联系管理员');
-        }
-        //成长值会员信息
-        $growth_vip_info = Db::name('vip')->find($this->auth->growthlevel);
-        //修改用户表信息
-        $user_data['experiencelevel'] = $id;
-        $user_data['experiencetime'] = $data['endtime'];
-        $user_data['maxlevel'] = $id;
-        $freenumber = $this->auth->freenumber + $info['free'] - $growth_vip_info['free'];
-        $user_data['freenumber'] = $freenumber > 0 ? $freenumber : 0;
-
-        $rt = Db::name('user')->where([
-            'user_id' => $this->auth->id,
-            'experiencelevel' => $this->auth->experiencelevel,
-            'maxlevel' => $this->auth->maxlevel,
-            'freenumber' => $this->auth->freenumber])->setField($user_data);
-        if (!$rt) {
-            Db::rollback();
-            $this->error('开通失败');
+        //开始事务
+        $result = Db::name('rechar_order')->insertGetId($rechar_order);
+        if (!$result) {
+            $this->error('网络延迟,请稍后再试');
         }
 
-        Db::commit();
-        $this->success('开通成功');
+        //构建支付链接数据
+        $wxData['body'] = '充值';
+        $wxData['out_trade_no'] = $rechar_order['order_no'];
+        $wxData['total_fee'] = $info['price'];
+//            $wxData['total_fee'] = 0.01;
+        $wxData['openid'] = $this->auth->openid;
+
+//            require_once($_SERVER['DOCUMENT_ROOT'] . '/Plugins/Weixin/WxPay/WxPay.php');
+        $wxPay = new wxpay\WxPay(config('wxchatpay'));
+        $doResult = $wxPay->WxPayJs($wxData);
+
+        $this->success('微信支付参数返回成功', $doResult);
     }
 }

+ 32 - 0
extend/wxpay/WxPay.php

@@ -419,4 +419,36 @@ class WxPay {
         return $output;
     }
 
+    //签名排序
+    private function sign($data)
+    {
+        $stringA = '';
+        foreach ($data as $key => $value) {
+            if(!$value) continue;
+            if($stringA) $stringA .= '&'.$key."=".$value;
+            else $stringA = $key."=".$value;
+        }
+
+        $wx_key = $this->config['key'];
+
+        $stringSignTemp = $stringA.'&key='.$wx_key;
+        return strtoupper(md5($stringSignTemp));
+    }
+
+    //xml转换
+    private function xml($xml)
+    {
+        $p = xml_parser_create();
+        xml_parse_into_struct($p ,$xml ,$vals , $index);
+        xml_parser_free($p);
+        $data = [];
+        foreach ($index as $key => $value) {
+            if($key =='xml' || $key=='XML') continue;
+            $tag = $vals[$value[0]]['tag'];
+            $value = $vals[$value[0]]['value'];
+            $data[$tag] = $value;
+        }
+
+        return $data;
+    }
 }