Browse Source

增加申请退款

15954078560 2 years ago
parent
commit
5cf71ea0f3
2 changed files with 153 additions and 2 deletions
  1. 1 1
      application/api/controller/Index.php
  2. 152 1
      application/api/controller/User.php

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

@@ -470,7 +470,7 @@ class Index extends Api
         //开始事务
         Db::startTrans();
         //修改活动表数据
-        $active_rs = Db::name('active')->where(['id' => $id, 'currentperson' => $info['currentperson']])->setField('currentperson', $info['currentperson'] + count($active_people_arr));
+        $active_rs = Db::name('active')->where(['id' => $id, 'status' => 0,'currentperson' => $info['currentperson']])->setField('currentperson', $info['currentperson'] + count($active_people_arr));
         if (!$active_rs) {
             Db::rollback();
             $this->error('网络延迟,请稍后再试');

+ 152 - 1
application/api/controller/User.php

@@ -1807,12 +1807,163 @@ class User extends Api
     }
 
     //取消订单
+    public function cancelorder() {
+        $id = input('id', 0, 'intval'); //订单id
+        if (!$id) {
+            $this->error('参数缺失');
+        }
+        $info = Db::name('active_order')->where(['id' => $id, 'user_id' => $this->auth->id])->find();
+        if (!$info) {
+            $this->error('数据不存在');
+        }
+        if ($info['status'] != 0) {
+            $this->error('网络延迟,请刷新重试');
+        }
+        if ($info['createtime'] + 1800 < time()) {
+            $this->error('订单超时,系统将自动取消');
+        }
+        //查询活动是否存在
+        $active_info = Db::name('active')->find($info['active_id']);
+        if (!$active_info) {
+            $this->error('活动不存在');
+        }
+
+        //开启事务
+        Db::startTrans();
+        //修改订单信息
+        $rs = Db::name('active_order')->where(['id' => $id, 'status' => 0])->setField('status', 3);
+        if (!$rs) {
+            Db::rollback();
+            $this->error('网络延迟,请稍后再试');
+        }
+        //修改活动人员
+        $rt = Db::name('active_people')->where(['order_id' => $id, 'status' => 0])->setField(['status' => 3, 'modifystatus' => 0]);
+        if (!$rt) {
+            Db::rollback();
+            $this->error('网络延迟,请稍后再试');
+        }
+        //修改活动人员修改记录
+        $res = Db::name('active_people_modify')->where(['order_id' => $id, 'status' => 0])->setField('status', 2);
+        if ($res === false) {
+            Db::rollback();
+            $this->error('网络延迟,请稍后再试');
+        }
+        //减少活动已报名人数
+        $currentperson = $active_info['currentperson'] - $info['number'];
+        $active_rs = Db::name('active')->where(['id' => $info['active_id'], 'currentperson' => $active_info['currentperson']])->setField('currentperson', $currentperson);
+        if (!$active_rs) {
+            Db::rollback();
+            $this->error('网络延迟,请稍后再试');
+        }
 
+        $this->success('修改成功');
+    }
 
     //支付订单
+    public function payorder() {
+        $id = input('id', 0, 'intval'); //订单id
+        if (!$id) {
+            $this->error('参数缺失');
+        }
+        $info = Db::name('active_order')->where(['id' => $id, 'user_id' => $this->auth->id])->find();
+        if (!$info) {
+            $this->error('数据不存在');
+        }
+        if ($info['status'] != 0) {
+            $this->error('当前订单状态不能支付');
+        }
+        if ($info['createtime'] + 1800 < time()) {
+            $this->error('订单支付超时,请重新报名');
+        }
+        //查询活动是否存在
+        $active_info = Db::name('active')->find($info['active_id']);
+        if (!$active_info) {
+            $this->error('活动不存在');
+        }
 
+        //生成支付订单记录
+        $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'] = 1; //充值用途:1=支付订单,2=充值,3=开通会员
+        $rechar_order['pay_type'] = 'wechat';
+        $rechar_order['relation_id'] = $id;
+        $rechar_order['createtime'] = time();
+
+        $result = Db::name('rechar_order')->insertGetId($rechar_order);
+        if (!$result) {
+            $this->error('网络延迟,请稍后再试');
+        }
+
+        //构建支付链接数据
+        $wxData['body'] = '报名活动支付';
+        $wxData['out_trade_no'] = $rechar_order['order_no'];
+//            $wxData['total_fee'] = $total_amount;
+        $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);
+    }
+
+    //申请退款
+    public function applyrefund() {
+        $id = input('id', 0, 'intval'); //订单id
+        if (!$id) {
+            $this->error('参数缺失');
+        }
+        $info = Db::name('active_order')->where(['id' => $id, 'user_id' => $this->auth->id])->find();
+        if (!$info) {
+            $this->error('数据不存在');
+        }
+        if ($info['status'] != 1) {
+            $this->error('当前订单状态不能退款');
+        }
+        //查询是否已经申请过
+        $count = Db::name('active_refund')->where(['order_id' => $id, 'status' => 0])->count('id');
+        if ($count) {
+            $this->error('您已申请过退款,请等待后台审核');
+        }
+
+        //查询活动是否存在
+        $active_info = Db::name('active')->find($info['active_id']);
+        if (!$active_info) {
+            $this->error('活动不存在');
+        }
+        if (time() > $active_info['refundendtime']) {
+            $this->error('已超过退款截止时间,暂不能退款');
+        }
+
+        //用户取消订单每人扣费金额(元)
+        $cancelorder_price = config('site.cancelorder');
+        if ($cancelorder_price < 0) {
+            $this->error('退款手续费异常,请联系管理员');
+        }
+        //退款金额
+        $refundprice = number_format($info['price'] - $cancelorder_price * $info['number'], 2, '.', '');
+        $refundprice = $refundprice > 0 ? $refundprice : 0;
+
+        //构建申请数据
+        $data = [
+            'active_id' => $info['active_id'],
+            'order_id' => $id,
+            'user_id' => $this->auth->id,
+            'price' => $info['price'],
+            'number' => $info['number'],
+            'refundprice' => $refundprice,
+            'createtime' => time()
+        ];
+
+        $rs = Db::name('active_refund')->insertGetId($data);
+        if (!$rs) {
+            $this->error('申请退款失败');
+        }
+
+        $this->success('申请退款成功');
+    }
 
 
 }