在这里插入图片描述

index.html

 &lt;a class="btn btn-danger btn-change btn-tuikuan btn-disabled"  href="javascript:;"&gt;<i class="fa fa-tuikuan"&gt;</i&gt; 订单退款</a&gt;--&gt;

order.js

// 为表格绑定事件
   Table.api.bindevent(table);

   //退款按钮
   $(document).on("click", ".btn-tuikuan", function () {
       //在table外不可以使用添加.btn-change方法
       //只能自己调用Table.api.multi实现
       //如果操作全部则ids可以为空
       Layer.confirm(__('确定要对此订单退款吗?'), {
           icon: 3,
           title: '提示'
       }, function (index) {
           var ids = Table.api.selectedids(table);
           Backend.api.ajax({
               url: "daren/Daren_order/tuikuan",
               data: {ids: ids.join(","),table: "daren_order"}
           }, function () {
               $(".btn-refresh").trigger("click");
           });
           Layer.close(index);
       });
   });
/**
     * 启用
     */
    public function tuikuan($ids = '',$table = "")
    {
        $roomid = explode(',',$ids);
        if(empty($table)) $this->error("参数请求错误");
        if(count($roomid)>1) $this->error("请选择一条订单操作");

        if(strcasecmp($table,'user_yecz_order')==0){
            $order = Db::name($table)->where('id',$roomid[0])->find();
            if(empty($order)) $this->error("订单存在");
            if(strcasecmp($order['order_status'],'1')==0) $this->error("订单未付款");
            if(strcasecmp($order['order_status'],'9')==0) $this->error("订单取消");

            $user = Db::name('user')->where('id',$order['user_id'])->find();
            if($order['money']>$user['money']){
                $this->error("会员余额不足");
            }
            $order['ddfy'] = $order['money'];
            $updata = ['order_status'=>'10'];

        }else{
            $order = Db::name($table)->where('id',$roomid[0])->find();
            if(empty($order)) $this->error("订单存在");
            if(strcasecmp($order['status'],'1')==0) $this->error("订单未付款");
            if(strcasecmp($order['status'],'9')==0) $this->error("订单取消");
            if(strcasecmp($order['status'],'10')==0) $this->error("订单已退款");
            $updata = ['status'=>'10'];
        }

        $refund_order = [
            'refund_no' => CommonFunction::createShopOrderSn('refund_order','TK'),
            'order_sn' =>$order['order_sn'],
            'total_fee' =>$order['ddfy'],
            'refund_fee' =>$order['ddfy'],
            'order_type' =>'1',
            'refund_status' =>'0',
            'createtime'=>time(),
        ];

        $refund_order['id'] = Db::name('refund_order')->insertGetId($refund_order);

        if(strcasecmp($order['paytype'],'2')==0){
           $rel = $this->wechatRefund($order,$refund_order,$table,$updata);
        }else{
            $rel = $this->yueRefund($order,$refund_order,$table,$updata);
        }

        if($rel['code']){
            $this->success($rel['msg']);
        }else{
            $this->error($rel['msg']);
        }
    }

    private function wechatRefund($order,$refund_order,$table,$updata){

        $site = Config::get("site");
        require dirname(dirname(dirname(dirname(__FILE__))))."/api/vendor/wechat-developer/include.php";
        $config = [
            'appid' => $site['appid'],
            'appsecret' => $site['appsecret'],
            // 配置商户支付参数(可选,在使用支付功能需要
            'mch_id' => $site['shh'],
            'mch_key' => $site['shmy'],
            'ssl_key' => dirname(dirname(dirname(dirname(__FILE__))))."/api/vendor/apiclient_key.pem",
            'ssl_cer' => dirname(dirname(dirname(dirname(__FILE__))))."/api/vendor/apiclient_cert.pem",
            // 缓存目录配置(可选,需拥有读写权限
            'cache_path' => '',
        ];
        // 创建接口实例
        $wechat = new WeChatPay($config);

        // 构造退款请求参数
        $options = [
            'out_trade_no' => $order['order_sn'], // 商户订单号
            'out_refund_no' => $refund_order['refund_no'], // 商户退款单号
            'total_fee' => $order['ddfy'] * 100, // 订单总金额,单位为分
            'refund_fee' => $order['ddfy'] * 100, // 退款金额,单位为分
        ];

        // 创建退款请求
        $result = $wechat->createRefund($options);
        // 处理退款结果
        if ($result['return_code'] == 'SUCCESS' &amp;&amp; $result['result_code'] == 'SUCCESS') {

            Db::name($table)->where('id',$order['id'])->update($updata);
            Db::name('refund_order')->where('id',$refund_order['id'])->update(['refund_status'=>'1','refundtime'=>time()]);
            Db::name('pay_log')->where('order_sn',$order['order_sn'])->update(['pay_status'=>'3']);

            if(strcasecmp($table,'user_yecz_order')==0){

                $user = Db::name('user')->where('id',$order['user_id'])->find();
                $money = $user['money'];
                $new_money = bcsub($user['money'],$order['ddfy'],2);
                Db::name('user')->where('id',$user['id'])->update(['money'=>$new_money]);

                $user_money_log = [
                    'user_id'=>$user['id'],
                    'money'=>-$order['ddfy'],
                    'before'=>$money,
                    'after'=>$new_money,
                    'memo'=>'订单余额退款',
                    'createtime'=>time(),
                ];
                Db::name('user_money_log')->insert($user_money_log);
            }

            return ['code'=>1,'msg'=>'退款成功'];
        } else {

            return ['code'=>0,'msg'=>'退款失败:' . $result['return_msg']];
        }

    }

    private function yueRefund($order,$refund_order,$table,$updata){

        Db::startTrans();
        try{
            $user = Db::name('user')->where('id',$order['user_id'])->find();

            $money = $user['money'];
            $new_money = bcadd($user['money'],$order['ddfy'],2);
            Db::name('user')->where('id',$user['id'])->update(['money'=>$new_money]);
            Db::name($table)->where('id',$order['id'])->update($updata);
            Db::name('refund_order')->where('id',$refund_order['id'])->update(['refund_status'=>'1','refundtime'=>time()]);
            Db::name('pay_log')->where('order_sn',$order['order_sn'])->update(['pay_status'=>'3']);

            $user_money_log = [
                'user_id'=>$user['id'],
                'money'=>$order['ddfy'],
                'before'=>$money,
                'after'=>$new_money,
                'memo'=>'订单余额退款',
                'createtime'=>time(),
            ];
            Db::name('user_money_log')->insert($user_money_log);

            Db::commit();
            return ['code'=>1,'msg'=>'退款成功'];
        }catch (Exception $e){
            Db::rollback();
            return ['code'=>0,'msg'=>'退款失败:'];
        }
    }

原文地址:https://blog.csdn.net/hanzhuhuaa/article/details/134617878

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_20354.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注