// +---------------------------------------------------------------------- namespace app\kefu\controller; use app\kefu\controller\AuthController; use service\JsonService as Json; use app\admin\model\order\StoreOrder as StoreOrderModel; use service\HookService; use basic\ModelBasic; use app\wap\model\special\SpecialBuy; use service\AlipayTradeWapService; use app\wap\model\store\StorePink; use app\admin\model\user\User; use app\admin\model\user\UserBill; use app\admin\model\user\MemberShip; use app\admin\model\merchant\MerchantFlowingWater; use app\admin\model\order\StoreOrderStatus; use app\admin\model\ump\EventSignUp as EventSignUpModel; use behavior\wechat\PaymentBehavior; use app\admin\model\order\DataDownloadOrder as DataDownloadOrderModel; use app\admin\model\download\DataDownloadBuy; use app\admin\model\order\TestPaperOrder as TestPaperOrderModel; use app\admin\model\questions\TestPaperObtain; use app\admin\model\user\UserRecharge as UserRechargeModel; use service\SystemConfigService; class Order extends AuthController { public function get_order_list($uid = '', $search = '', $type = 0, $page = 1, $limit = 10) { if (!$uid) return Json::fail("用户不存在!"); $orders = []; $formatTime = true; if ($type < 3) { // 课程订单、商品订单、会员订单 $where = [ 'type' => '', 'status' => '', 'real_name' => $search, 'uid' => $uid, 'data' => '', 'types' => $type, 'order' => '', 'spread_type' => '', 'page' => $page, 'limit' => $limit, 'excel' => 0 ]; $orders = StoreOrderModel::OrderList($where)['data']; } else if ($type == 4) { // 报名订单 $where = [ 'id' => '', 'page' => $page, 'limit' => $limit, 'status' => '', 'type' => '', 'uid' => $uid, 'real_name' => $search, 'data' => '', 'excel' => 0, ]; $orders = EventSignUpModel::getUserSignUpAll($where)['data']; } else if ($type == 5) { // 资料订单 $where = [ 'status' => '', 'real_name' => $search, 'uid' => $uid, 'is_del' => 0, 'data' => '', 'type' => '', 'order' => '', 'page' => $page, 'limit' => $limit, 'excel' => 0 ]; $orders = DataDownloadOrderModel::orderList($where)['data']; } else if ($type == 6) { // 考试订单 $where = [ 'status' => '', 'real_name' => $search, 'is_del' => 0, 'uid' => $uid, 'data' => '', 'type' => '', 'order' => '', 'page' => $page, 'limit' => $limit, 'excel' => 0 ]; $orders = TestPaperOrderModel::OrderList($where)['data']; } else if ($type == 7) { // 充值订单 $where = [ 'status' => '', 'real_name' => $search, 'uid' => $uid, 'data' => '', 'order' => '', 'page' => $page, 'limit' => $limit, 'excel' => 0 ]; $formatTime = false; $orders = UserRechargeModel::systemPage($where)['data']; foreach ($orders as &$order) { $order['add_time'] = date("Y/m/d H:i:s", $order['add_time']); } } if ($formatTime) { foreach ($orders as &$order) { if ($order['pay_time']) { $order['pay_time'] = date("Y/m/d H:i:s", $order['pay_time']); } else { $order['pay_time'] = ''; } $order['add_time'] = date("Y/m/d H:i:s", $order['add_time']); } } return Json::successful($orders); } public function save_order_remark($order_id, $remark, $order_type = 0) { if ($order_type < 3) { // 课程订单、商品订单、会员订单 $order = (new StoreOrderModel)->where([ 'order_id' => $order_id, 'type' => $order_type ])->find(); if (!$order) return Json::fail("订单不存在!"); $order->mark = $remark; $order->save(); return Json::successful("保存成功!"); } } public function order_refund($id, $type) { if ($type < 3) { // 课程订单、商品订单、会员订单 $product = StoreOrderModel::get($id); if (!$product) return Json::fail('数据不存在!'); if ($product['pay_price'] == $product['refund_price']) return Json::fail('已退完支付金额!不能再退款了'); $data = [ 'refund_price' => $product['pay_price'], 'type' => 1 ]; if (!$data['refund_price']) return Json::fail('请输入退款金额'); $refund_price = $data['refund_price']; $data['refund_price'] = bcadd($data['refund_price'], $product['refund_price'], 2); $bj = bccomp((float)$product['pay_price'], (float)$data['refund_price'], 2); if ($bj < 0) return Json::fail('退款金额大于支付金额,请修改退款金额'); if ($data['type'] == 1) { $data['refund_status'] = 2; } else if ($data['type'] == 2) { $data['refund_status'] = 0; } $type = $data['type']; unset($data['type']); $refund_data['pay_price'] = $product['pay_price']; $refund_data['refund_price'] = $refund_price; if ($product['pay_type'] == 'weixin') { try { HookService::listen('wechat_pay_order_refund', $product['order_id'], $refund_data, true, PaymentBehavior::class); } catch (\Exception $e) { return Json::fail($e->getMessage()); } } else if ($product['pay_type'] == 'yue') { ModelBasic::beginTrans(); $res = User::bcInc($product['uid'], 'now_money', $refund_price, 'uid'); ModelBasic::checkTrans($res); if (!$res) return Json::fail('余额退款失败!'); } else if ($product['pay_type'] == 'zhifubao') { if ($product['type'] == 0 && $product['combination_id'] > 0) { $mask = '拼团失败退款'; } elseif ($product['type'] == 0 && $product['combination_id'] == 0) { $mask = '专题退款'; } elseif ($product['type'] == 2) { $mask = '商品退款'; } $res = AlipayTradeWapService::init()->AliPayRefund($product['order_id'], $product['trade_no'], $refund_price, $mask, 'refund'); if (empty($res) || $res != 10000) { return Json::fail('支付宝退款失败!'); } } $data['refund_reason_time'] = time(); $resEdit = StoreOrderModel::edit($data, $id); if ($resEdit) { $data['type'] = $type; if ($data['type'] == 1 && $product['type'] == 0) { StorePink::setRefundPink($id); (new SpecialBuy)->where('order_id', $product['order_id'])->delete(); } if ($product['type'] == 1) { $merber = (new MemberShip)->where('id', $product['member_id'])->find(); if ($merber['is_permanent']) { (new User)->where('uid', $product['uid'])->update(['is_permanent' => 0]); } else { $time = bcmul($merber['vip_day'], 86400, 0); (new User)->where('uid', $product['uid'])->setDec('overdue_time', $time); } } if ($product['type'] == 0 && $product['combination_id'] > 0) { $title = '专题拼团退款'; MerchantFlowingWater::orderRefund($id, $product['mer_id'], 0); } elseif ($product['type'] == 0 && $product['combination_id'] == 0) { $title = '专题退款'; MerchantFlowingWater::orderRefund($id, $product['mer_id'], 0); } elseif ($product['type'] == 1) { $title = '会员退款'; } elseif ($product['type'] == 2) { $title = '商品退款'; MerchantFlowingWater::orderRefund($id, $product['mer_id'], 2); } $pay_type = $product['pay_type'] == 'yue' ? 'now_money' : $product['pay_type']; if ($product['pay_type'] == 'yue') { $balance = (new User)->where(['uid' => $product['uid']])->value('now_money'); } else { $balance = 0; } UserBill::income($title, $product['uid'], $pay_type, 'pay_product_refund', $refund_price, $product['id'], $balance, '订单退款' . floatval($refund_price) . '元'); HookService::afterListen('store_product_order_refund_y', $data, $id, false, StoreProductBehavior::class); StoreOrderStatus::setStatus($id, 'refund_price', '退款给用户' . $refund_price . '元'); return Json::successful('退款成功!'); } else { StoreOrderStatus::setStatus($id, 'refund_price', '退款给用户' . $refund_price . '元失败'); return Json::successful('退款失败!'); } } else if ($type == 4) { // 报名订单 if (!$id) return Json::fail('数据不存在'); $product = EventSignUpModel::get($id); if (!$product) return Json::fail('数据不存在!'); $data = [ 'refund_price' => $product['pay_price'], ]; if ($product['pay_price'] == $product['refund_price']) return Json::fail('已退完支付金额!不能再退款了'); if (!$data['refund_price']) return Json::fail('请输入退款金额'); $refund_price = $data['refund_price']; $data['refund_price'] = bcadd($data['refund_price'], $product['refund_price'], 2); $bj = bccomp((float)$product['pay_price'], (float)$data['refund_price'], 2); if ($bj < 0) return Json::fail('退款金额大于支付金额,请修改退款金额'); $data['refund_status'] = 2; $refund_data['pay_price'] = $product['pay_price']; $refund_data['refund_price'] = $refund_price; if ($product['pay_type'] == 'weixin') { try { HookService::listen('wechat_pay_order_refund', $product['order_id'], $refund_data, true, PaymentBehavior::class); } catch (\Exception $e) { return Json::fail($e->getMessage()); } } else if ($product['pay_type'] == 'yue') { EventSignUpModel::beginTrans(); $res = User::bcInc($product['uid'], 'now_money', $refund_price, 'uid'); EventSignUpModel::checkTrans($res); if (!$res) return Json::fail('余额退款失败!'); } else if ($product['pay_type'] == 'zhifubao') { $res = AlipayTradeWapService::init()->AliPayRefund($product['order_id'], $product['trade_no'], $refund_price, '活动订单退款', 'refund'); if (empty($res) || $res != 10000) { return Json::fail('支付宝退款失败!'); } } $data['refund_reason_time'] = time(); $resEdit = EventSignUpModel::edit($data, $id); if ($resEdit) { $pay_type = $product['pay_type'] == 'yue' ? 'now_money' : $product['pay_type']; if ($product['pay_type'] == 'yue') { $balance = (new User)->where(['uid' => $product['uid']])->value('now_money'); } else { $balance = 0; } UserBill::income('活动订单退款', $product['uid'], $pay_type, 'pay_sign_up_refund', $refund_price, $product['id'], $balance, '活动订单退款' . floatval($refund_price) . '元'); MerchantFlowingWater::orderRefund($id, $product['mer_id'], 4); return Json::successful('修改成功!'); } else { return Json::successful('修改失败!'); } } else if ($type == 5) { // 资料订单 if (!$id) return Json::fail('数据不存在'); $product = DataDownloadOrderModel::get($id); if (!$product) return Json::fail('数据不存在!'); $data = [ 'refund_price' => $product['pay_price'], 'type' => 1, ]; if ($product['pay_price'] == $product['refund_price']) return Json::fail('已退完支付金额!不能再退款了'); if (!$data['refund_price']) return Json::fail('请输入退款金额'); $refund_price = $data['refund_price']; $data['refund_price'] = bcadd($data['refund_price'], (string)$product['refund_price'], 2); $bj = bccomp((float)$product['pay_price'], (float)$data['refund_price'], 2); if ($bj < 0) return Json::fail('退款金额大于支付金额,请修改退款金额'); if ($data['type'] == 1) { $data['refund_status'] = 2; } else if ($data['type'] == 2) { $data['refund_status'] = 0; } $type = $data['type']; unset($data['type']); $refund_data['pay_price'] = $product['pay_price']; $refund_data['refund_price'] = $refund_price; if ($product['pay_type'] == 'weixin') { try { HookService::listen('wechat_pay_order_refund', $product['order_id'], $refund_data, true, PaymentBehavior::class); } catch (\Exception $e) { return Json::fail($e->getMessage()); } } else if ($product['pay_type'] == 'yue') { ModelBasic::beginTrans(); $res = User::bcInc($product['uid'], 'now_money', $refund_price, 'uid'); ModelBasic::checkTrans($res); if (!$res) return Json::fail('余额退款失败!'); } else if ($product['pay_type'] == 'zhifubao') { $res = AlipayTradeWapService::init()->AliPayRefund($product['order_id'], $product['trade_no'], $refund_price, '资料退款', 'refund'); if (empty($res) || $res != 10000) { return Json::fail('支付宝退款失败!'); } } $data['refund_reason_time'] = time(); $resEdit = DataDownloadOrderModel::edit($data, $id); if ($resEdit) { $data['type'] = $type; (new DataDownloadBuy)->where('order_id', $product['order_id'])->delete(); $pay_type = $product['pay_type'] == 'yue' ? 'now_money' : $product['pay_type']; if ($product['pay_type'] == 'yue') { $balance = (new User)->where(['uid' => $product['uid']])->value('now_money'); } else { $balance = 0; } UserBill::income('资料退款', $product['uid'], $pay_type, 'pay_data_download_refund', $refund_price, $product['id'], $balance, '订单退款' . floatval($refund_price) . '元'); MerchantFlowingWater::orderRefund($id, $product['mer_id'], 3); return Json::successful('修改成功!'); } else { return Json::successful('修改失败!'); } } else if ($type == 6) { //考试订单 if (!$id) return Json::fail('数据不存在'); $product = TestPaperOrderModel::get($id); if (!$product) return Json::fail('数据不存在!'); $data = [ 'refund_price' => $product['pay_price'], 'type' => 1 ]; if ($product['pay_price'] == $product['refund_price']) return Json::fail('已退完支付金额!不能再退款了'); if (!$data['refund_price']) return Json::fail('请输入退款金额'); $refund_price = $data['refund_price']; $data['refund_price'] = bcadd($data['refund_price'], $product['refund_price'], 2); $bj = bccomp((float)$product['pay_price'], (float)$data['refund_price'], 2); if ($bj < 0) return Json::fail('退款金额大于支付金额,请修改退款金额'); if ($data['type'] == 1) { $data['refund_status'] = 2; } else if ($data['type'] == 2) { $data['refund_status'] = 0; } $type = $data['type']; unset($data['type']); $refund_data['pay_price'] = $product['pay_price']; $refund_data['refund_price'] = $refund_price; if ($product['pay_type'] == 'weixin') { try { HookService::listen('wechat_pay_order_refund', $product['order_id'], $refund_data, true, PaymentBehavior::class); } catch (\Exception $e) { return Json::fail($e->getMessage()); } } else if ($product['pay_type'] == 'yue') { ModelBasic::beginTrans(); $res = User::bcInc($product['uid'], 'now_money', $refund_price, 'uid'); ModelBasic::checkTrans($res); if (!$res) return Json::fail('余额退款失败!'); } else if ($product['pay_type'] == 'zhifubao') { $res = AlipayTradeWapService::init()->AliPayRefund($product['order_id'], $product['trade_no'], $refund_price, '试卷退款', 'refund'); if (empty($res) || $res != 10000) { return Json::fail('支付宝退款失败!'); } } $data['refund_reason_time'] = time(); $resEdit = TestPaperOrderModel::edit($data, $id); if ($resEdit) { $data['type'] = $type; (new TestPaperObtain)->where('order_id', $product['order_id'])->delete(); $pay_type = $product['pay_type'] == 'yue' ? 'now_money' : $product['pay_type']; if ($product['pay_type'] == 'yue') { $balance = (new User)->where(['uid' => $product['uid']])->value('now_money'); } else { $balance = 0; } UserBill::income('试卷退款', $product['uid'], $pay_type, 'pay_test_paper_refund', $refund_price, $product['id'], $balance, '订单退款' . floatval($refund_price) . '元'); return Json::successful('修改成功!'); } else { return Json::successful('修改失败!'); } } else if ($type == 7) { // 充值订单 if (!$id) return Json::fail('数据不存在'); $UserRecharge = UserRechargeModel::get($id); if (!$UserRecharge) return Json::fail('数据不存在!'); $data = [ 'refund_price' => $UserRecharge['price'], ]; if ($UserRecharge['price'] == $UserRecharge['refund_price']) return Json::fail('已退完支付金额!不能再退款了'); if (!$data['refund_price']) return Json::fail('请输入退款金额'); $refund_price = $data['refund_price']; $data['refund_price'] = bcadd($data['refund_price'], (string)$UserRecharge['refund_price'], 2); $bj = bccomp((float)$UserRecharge['price'], (float)$data['refund_price'], 2); if ($bj < 0) return Json::fail('退款金额大于支付金额,请修改退款金额'); $refund_data['pay_price'] = $UserRecharge['price']; $refund_data['refund_price'] = $refund_price; $data['refund_status'] = 2; if ($UserRecharge['recharge_type'] == 'weixin') { try { HookService::listen('user_recharge_refund', $UserRecharge['order_id'], $refund_data, true, PaymentBehavior::class); } catch (\Exception $e) { return Json::fail($e->getMessage()); } } else if ($UserRecharge['recharge_type'] == 'yue') { UserRechargeModel::beginTrans(); $res = User::bcInc($UserRecharge['uid'], 'now_money', $refund_price, 'uid'); UserRechargeModel::checkTrans($res); if (!$res) return Json::fail('余额退款失败!'); } else if ($UserRecharge['recharge_type'] == 'zhifubao') { $res = AlipayTradeWapService::init()->AliPayRefund($UserRecharge['order_id'], $UserRecharge['trade_no'], $refund_price, '虚拟币充值退款', 'refund'); if (empty($res) || $res != 10000) { return Json::fail('支付宝退款失败!'); } } $data['refund_reason_time'] = time(); $resEdit = UserRechargeModel::edit($data, $id); if ($resEdit) { $goldNum = money_rate_num($refund_price, 'gold'); $gold_name = SystemConfigService::get('gold_name'); //虚拟币名称 User::bcDec($UserRecharge['uid'], 'gold_num', $goldNum, 'uid'); UserBill::expend($gold_name . '充值退款', $UserRecharge['uid'], 'gold_num', 'return', $goldNum, $UserRecharge['id'], 0, '退' . floatval($goldNum) . $gold_name); $recharge_type = $UserRecharge['recharge_type'] == 'yue' ? 'now_money' : $UserRecharge['recharge_type']; UserBill::income('虚拟币充值退款', $UserRecharge['uid'], $recharge_type, 'user_recharge_refund', $refund_price, $UserRecharge['id'], 0, '订单退款' . floatval($refund_price) . '元'); return Json::successful('修改成功!'); } else { return Json::successful('修改失败!'); } } } }