Browse Source

商城,已付款未发货的订单取消,自动退款

lizhen_gitee 10 months ago
parent
commit
1579400206
1 changed files with 193 additions and 2 deletions
  1. 193 2
      addons/unishop/controller/Order.php

+ 193 - 2
addons/unishop/controller/Order.php

@@ -23,6 +23,7 @@ use think\Exception;
 use addons\unishop\model\Address as AddressModel;
 use think\Hook;
 use think\Loader;
+use addons\epay\library\Service;
 
 /**
  * 订单
@@ -369,12 +370,202 @@ class Order extends Base
 
 //        if ($order['status'] == \addons\unishop\model\Order::STATUS_NORMAL && $order['have_paid'] == \addons\unishop\model\Order::PAID_NO) {
         if ($order['status'] == \addons\unishop\model\Order::STATUS_NORMAL && $order['have_delivered'] == \addons\unishop\model\Order::DELIVERED_NO) {
-            $order->status = \addons\unishop\model\Order::STATUS_CANCEL;
-            $order->save();
+
+            Db::startTrans();
+            /*$order->status = \addons\unishop\model\Order::STATUS_CANCEL;
+            $order->save();*/
+
+
+            //已付款的,自动退款
+            if($order['have_paid'] != \addons\unishop\model\Order::PAID_NO){
+                $refund_amount = $order['total_price'];
+                $refund_status = 3;
+
+                // 退款
+                $params = [
+                    'refund_status' => $refund_status,
+                    'updatetime'    => time(),
+                    'refund_amount' => $refund_amount,
+                    'status' => \addons\unishop\model\Order::STATUS_CANCEL,
+                ];
+                if($refund_status == 3) {
+                    $params['had_refund'] = time();
+                }
+
+                $result = Db::name('unishop_order')->where(['id' => $order_id])->update($params);
+                if (!$result) {
+                    Db::rollback();
+                    $this->error('取消失败');
+                }
+
+                //同意并执行退款
+                if($refund_status == 3 && $refund_amount > 0){
+                    $order = Db::name('unishop_order')->where('id',$order_id)->find();
+
+                    if($order['pay_type'] == 2){
+                        $wallet_rs = model('wallet')->lockChangeAccountRemain($order['user_id'],'money',$refund_amount,32,'商城取消订单退款'.$order['out_trade_no'],'unishop_order',$order_id);
+                        if($wallet_rs['status'] === false){
+                            Db::rollback();
+                            $this->error($wallet_rs['msg']);
+                        }
+                    }elseif($order['pay_type'] == 3 || $order['pay_type'] == 4){
+                        $refund_result = $this->old_refund($order,$refund_amount);
+                        if($refund_result !== true){
+                            Db::rollback();
+                            $this->error($refund_result);
+                        }
+                    }
+
+                }
+            }else{
+                $params = [
+                    'status' => \addons\unishop\model\Order::STATUS_CANCEL,
+                ];
+                $result = Db::name('unishop_order')->where(['id' => $order_id])->update($params);
+                if ($result === false) {
+                    Db::rollback();
+                    $this->error('取消失败');
+                }
+            }
+
+            Db::commit();
+
             $this->success('取消成功', true);
         }
     }
 
+    // 退款
+    public function old_refund($order, $refund_price)
+    {
+
+        $table = 'unishop_order';
+        $remark = '订单退款';
+
+        if($order['pay_type'] == 3){
+            $order['pay_type'] = 'wechat';
+        }
+        if($order['pay_type'] == 4){
+            $order['pay_type'] = 'alipay';
+        }
+
+        // 生成退款单
+        $refund_data = [
+            'order_id' => $order['id'],
+            'out_refund_no'=> createUniqueNo('R'),
+            'pay_fee'  => $order['total_price'],
+            'refund_price' => $refund_price,
+            'pay_type' => $order['pay_type'],
+            'status' => 0,
+            'createtime' => time(),
+            'updatetime' => time(),
+            'table' => $table,
+            'table_id' => $order['id'],
+        ];
+        $refund_log_id = Db::name('pay_order_refund_log')->insertGetId($refund_data);
+        if(!$refund_log_id){
+            return '退款失败';
+        }
+
+        if ($order['pay_type'] == 'wechat' || $order['pay_type'] == 'alipay') {
+            // 微信|支付宝退款
+
+            // 退款数据
+            $order_data = [
+                'out_trade_no' => $order['pay_out_trade_no']
+            ];
+
+            if ($order['pay_type'] == 'wechat') {
+                $total_fee = $order['total_price'] * 100;
+                $refund_fee = $refund_price * 100;
+
+                $order_data = array_merge($order_data, [
+                    'out_refund_no' => $refund_data['out_refund_no'],
+                    'total_fee' => $total_fee,
+                    'refund_fee' => $refund_fee,
+                    'refund_desc' => $remark,
+                ]);
+            } else {
+                $order_data = array_merge($order_data, [
+                    'out_request_no' => $refund_data['out_refund_no'],
+                    'refund_amount' => $refund_price,
+                ]);
+            }
+
+            //
+            if ($order['pay_type'] == 'wechat') {
+                $wxpay = new \app\common\library\Wxpay;
+                $result = $wxpay->WxPayRefund($order_data);
+                // 微信通知回调 pay->notifyr
+                if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
+                    Db::name('pay_order_refund_log')->where('id',$refund_log_id)->update(['status'=>1]);
+                    return true;
+                } else {
+                    return $result['return_msg'];
+                }
+            } else {
+
+                $result = Service::submitRefund($order['total_price'],$refund_price,$order['pay_out_trade_no'],$refund_data['out_refund_no'],$order['pay_type'],$remark,'');
+
+                if($result['code'] == '10000'){
+                    Db::name('pay_order_refund_log')->where('id',$refund_log_id)->update(['status'=>1]);
+                    return true;
+                }else{
+                    return $result['msg'];
+                }
+
+
+
+                /* return 'alipay wrong way';
+                 $alipay = new \app\common\library\AliPay;
+                 $result = $alipay->AliPayRefund($order_data);
+                 // 支付宝通知回调 pay->notifyx
+                 return $result;*/
+
+                /*if ($result['code'] == "10000") {
+                    return true;
+                } else {
+                    throw new \Exception($result['msg']);
+                }*/
+            }
+
+            // {        // 微信返回结果
+            //     "return_code":"SUCCESS",
+            //     "return_msg":"OK",
+            //     "appid":"wx39cd0799d4567dd0",
+            //     "mch_id":"1481069012",
+            //     "nonce_str":"huW9eIAb5BDPn0Ma",
+            //     "sign":"250316740B263FE53F5DFF50AF5A8FA1",
+            //     "result_code":"SUCCESS",
+            //     "transaction_id":"4200000497202004072822298902",
+            //     "out_trade_no":"202010300857029180027000",
+            //     "out_refund_no":"1586241595",
+            //     "refund_id":"50300603862020040700031444448",
+            //     "refund_channel":[],
+            //     "refund_fee":"1",
+            //     "coupon_refund_fee":"0",
+            //     "total_fee":"1",
+            //     "cash_fee":"1",
+            //     "coupon_refund_count":"0",
+            //     "cash_refund_fee":"1
+            // }
+
+            // {        // 支付宝返回结果
+            //     "code": "10000",
+            //     "msg": "Success",
+            //     "buyer_logon_id": "157***@163.com",
+            //     "buyer_user_id": "2088902485164146",
+            //     "fund_change": "Y",
+            //     "gmt_refund_pay": "2020-08-15 16:11:45",
+            //     "out_trade_no": "202002460317545607015300",
+            //     "refund_fee": "0.01",
+            //     "send_back_fee": "0.00",
+            //     "trade_no": "2020081522001464141438570535"
+            // }
+        }
+
+        return true;
+    }
+
     /**
      * @ApiTitle    (删除订单)
      * @ApiSummary  (只能删除已取消或已退货的订单)