From 64d6de80cf3ee47924ae6410fb2936681f7875bb Mon Sep 17 00:00:00 2001 From: luofei <604446095@qq.com> Date: Mon, 29 Jan 2024 16:46:32 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A2=E5=8D=95=E9=80=80=E6=AC=BE=EF=BC=8C?= =?UTF-8?q?=E5=BA=97=E9=93=BA=E9=80=80=E4=BD=A3=E9=87=91=EF=BC=8C=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=9B=A2=E9=98=9F=E9=80=80=E4=BD=A3=E9=87=91=E5=92=8C?= =?UTF-8?q?=E7=BA=A2=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dao/store/consumption/CommissionDao.php | 115 +++++++++++++++++- .../consumption/StoreConsumptionUserDao.php | 33 +++++ .../order/StoreOrderCreateRepository.php | 1 + .../store/order/StoreOrderRepository.php | 2 +- .../order/StoreRefundOrderRepository.php | 3 + app/controller/api/Open.php | 24 ++++ app/listener/paySuccess.php | 6 +- route/api.php | 1 + 8 files changed, 178 insertions(+), 7 deletions(-) diff --git a/app/common/dao/store/consumption/CommissionDao.php b/app/common/dao/store/consumption/CommissionDao.php index c9960fa6..80315cb1 100755 --- a/app/common/dao/store/consumption/CommissionDao.php +++ b/app/common/dao/store/consumption/CommissionDao.php @@ -77,7 +77,7 @@ class CommissionDao } $financialRecordRepository = app()->make(FinancialRecordRepository::class); $financeSn = $financialRecordRepository->getSn(); - $users = $data['user']; + $users = $this->getUsers($data['user']); $order = StoreOrder::where('order_id', $data['order_id'])->find(); if (empty($order)) { return []; @@ -90,8 +90,8 @@ class CommissionDao $finance[] = [ 'order_id' => $order->order_id, 'order_sn' => $order->order_sn, - 'user_info' => $order->user->nickname, - 'user_id' => $order['uid'], + 'user_info' => $user['nickname'], + 'user_id' => $user['uid'], 'financial_type' => $user['type'] == 3 ? 'order_commission' : 'first_order_commission', 'financial_pm' => 0, 'type' => 2, @@ -104,7 +104,7 @@ class CommissionDao $redPack = bcmul($order['pay_price'], $consumption['config']['red_pack_rate'], 2); if ($redPack > 0) { try { - (new StoreConsumptionUserDao())->send($consumption, $consumption['config']['red_pack_rate'], $order['uid'], $order['order_id'], $order['pay_price'], StoreConsumptionUser::STATUS_UNUSED, StoreConsumptionUser::TYPE_TWO); + (new StoreConsumptionUserDao())->send($consumption, $consumption['config']['red_pack_rate'], $user['uid'], $order['order_id'], $order['pay_price'], StoreConsumptionUser::STATUS_UNUSED, StoreConsumptionUser::TYPE_TWO); } catch (\Exception $e) { Log::error($e->getMessage()); } @@ -149,4 +149,111 @@ class CommissionDao } } + /** + * 订单退款,店铺退佣金 + * @param $refundOrder + * @return void + */ + public function refundByOrder($refundOrder) + { + $commission = bcmul($refundOrder->order['pay_price'], 0.01, 2); + if ($commission > 0 && $refundOrder->order['order_type'] == 1) { + $financialRecordRepository = app()->make(FinancialRecordRepository::class); + $financeSn = $financialRecordRepository->getSn(); + // 订单为自提,且佣金大于0 + $finance[] = [ + 'order_id' => $refundOrder->order->order_id, + 'order_sn' => $refundOrder->order->order_sn, + 'user_info' => $refundOrder->order->user->nickname, + 'user_id' => $refundOrder->order['uid'], + 'financial_type' => 'first_order_commission_refund', + 'financial_pm' => 1, + 'type' => 2, + 'number' => $commission, + 'mer_id' => $refundOrder->order->mer_id, + 'financial_record_sn' => $financeSn + ]; + $financialRecordRepository->insertAll($finance); + app()->make(MerchantRepository::class)->subLockMoney($refundOrder->order['mer_id'], 'order', $refundOrder->order['order_id'], $commission); + } + $promotionCode = User::where('uid', $refundOrder['uid'])->value('promotion_code'); + if ($promotionCode) { + $curl = new Curl(); + $aes = new \AES(); + $timestamp = time(); + $json = ['timestamp' => $timestamp, 'data' => ['order_sn' => $refundOrder->order['order_sn']]]; + $iv = $aes->buildIv($timestamp); + $encrypt = $aes->encrypt($json, $iv); + $url = env('task.worker_host_url') . '/api/shop_call/handleRefund'; + $result = $curl->post($url, ['timestamp' => $timestamp, 'data' => $encrypt]); + $result = json_decode($result, true); + if ($result['code'] != 1) { + Log::error('发起佣金退款失败:' . var_export($result, true)); + } + } + } + + /** + * 供销平台退佣金回调 + * @param $data + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function refundByCallback($data) + { + $finance = []; + $result = []; + $users = $this->getUsers($data['user']); + $order = StoreOrder::where('order_id', $data['order_id'])->find(); + if (empty($order) || empty($users)) { + return []; + } + $financialRecordRepository = app()->make(FinancialRecordRepository::class); + $financeSn = $financialRecordRepository->getSn(); + foreach ($users as $k => $user) { + $commission = bcdiv($user['user_profit'], 100, 2); + if ($commission > 0) { + $finance[] = [ + 'order_id' => $order->order_id, + 'order_sn' => $order->order_sn, + 'user_info' => $user['nickname'], + 'user_id' => $user['uid'], + 'financial_type' => ($user['type'] == 3 ? 'order_commission' : 'first_order_commission') . '_refund', + 'financial_pm' => 1, + 'type' => 2, + 'number' => $commission, + 'mer_id' => $order['mer_id'], + 'financial_record_sn' => $financeSn . ($k + 1) + ]; + $result[] = $user; + } + $redPack = bcmul($order['pay_price'], 0.07, 2); + if ($redPack > 0) { + try { + (new StoreConsumptionUserDao())->refundByCommission($user['uid'], $order->order_id, $redPack); + } catch (\Exception $e) { + Log::error($e->getMessage()); + } + } + } + if (count($finance) > 0) { + $financialRecordRepository->insertAll($finance); + } + return $result; + } + + public function getUsers($info) + { + $info = reset_index($info, 'account'); + $users = User::whereIn('account', array_keys($info))->field('uid,account,nickname')->select()->toArray(); + foreach ($users as &$user) { + if (isset($info[$user['account']])) { + $user = array_merge($info[$user['account']], $user); + } + } + return $users; + } + } \ No newline at end of file diff --git a/app/common/dao/store/consumption/StoreConsumptionUserDao.php b/app/common/dao/store/consumption/StoreConsumptionUserDao.php index 6d6e263e..3a507c01 100755 --- a/app/common/dao/store/consumption/StoreConsumptionUserDao.php +++ b/app/common/dao/store/consumption/StoreConsumptionUserDao.php @@ -402,4 +402,37 @@ class StoreConsumptionUserDao extends BaseDao ]); } + /** + * 订单退款,服务团队退红包 + * @param $userId + * @param $orderId + * @param $amount + * @return void + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function refundByCommission($userId, $orderId, $amount) + { + $model = StoreConsumptionUser::where('uid', $userId)->where('type', StoreConsumptionUser::TYPE_TWO)->find(); + if (empty($model)) { + return; + } + $model->balance = bcsub($model->balance, $amount, 2); + if (!$model->save()) { + throw new ValidateException('红包退还失败'); + } + // 写入红包日志 + /** @var $userBillRepository UserBillRepository */ + $userBillRepository = app()->make(UserBillRepository::class); + $userBillRepository->decBill($userId, 'consumption_refund', 'deduction', [ + 'link_id' => $orderId, + 'status' => 1, + 'title' => '订单退款,退现金抵扣红包', + 'number' => $amount, + 'mark' => '订单退款,退现金抵扣红包' . floatval($amount) . '元', + 'balance' => 0 + ]); + } + } diff --git a/app/common/repositories/store/order/StoreOrderCreateRepository.php b/app/common/repositories/store/order/StoreOrderCreateRepository.php index d853e6d1..1b6126a5 100755 --- a/app/common/repositories/store/order/StoreOrderCreateRepository.php +++ b/app/common/repositories/store/order/StoreOrderCreateRepository.php @@ -895,6 +895,7 @@ class StoreOrderCreateRepository extends StoreOrderRepository 'give_integral' => $orderInfo['order_total_give_integral'], 'consumption_id' => $orderInfo['consumption_id'], 'consumption_money' => $orderInfo['consumption_money'], + 'source' => $orderInfo['source'], ]; event('order.create.before', compact('groupOrder', 'orderList')); $group = Db::transaction(function () use ($ex, $user, $topUid, $spreadUid, $uid, $receipt_data, $cartIds, $allUseCoupon, $groupOrder, $orderList, $orderInfo) { diff --git a/app/common/repositories/store/order/StoreOrderRepository.php b/app/common/repositories/store/order/StoreOrderRepository.php index b62ab49d..e06a18f3 100755 --- a/app/common/repositories/store/order/StoreOrderRepository.php +++ b/app/common/repositories/store/order/StoreOrderRepository.php @@ -506,7 +506,7 @@ class StoreOrderRepository extends BaseRepository ], $order->mer_id); //自动打印订单 $this->autoPrinter($order->order_id, $order->mer_id); - if ($order['pay_price'] > 0 && $order['source'] != 105) { + if ($order['pay_price'] > 0 && $order['source'] == 103) { // "惠农供销,谱写数字新篇章"活动首单分润,商户和村、小组合伙人 $finance = (new CommissionDao())->firstOrderCommission($order, $finance, $financeSn . ($i++)); } diff --git a/app/common/repositories/store/order/StoreRefundOrderRepository.php b/app/common/repositories/store/order/StoreRefundOrderRepository.php index 9bc99946..042792bf 100755 --- a/app/common/repositories/store/order/StoreRefundOrderRepository.php +++ b/app/common/repositories/store/order/StoreRefundOrderRepository.php @@ -14,6 +14,7 @@ namespace app\common\repositories\store\order; +use app\common\dao\store\consumption\CommissionDao; use app\common\dao\store\consumption\StoreConsumptionUserDao; use app\common\dao\store\order\StoreRefundOrderDao; use app\common\dao\store\StoreActivityOrderDao; @@ -1183,6 +1184,8 @@ class StoreRefundOrderRepository extends BaseRepository (new StoreActivityOrderDao())->repeal($refundOrder->order->group_order_id); + (new CommissionDao())->refundByOrder($refundOrder); + app()->make(FinancialRecordRepository::class)->dec([ 'order_id' => $refundOrder->refund_order_id, 'order_sn' => $refundOrder->refund_order_sn, diff --git a/app/controller/api/Open.php b/app/controller/api/Open.php index 8d05d96b..9935f798 100755 --- a/app/controller/api/Open.php +++ b/app/controller/api/Open.php @@ -33,4 +33,28 @@ class Open extends BaseController return app('json')->fail('解密失败'); } + + /** + * 供销平台退佣金回调 + * @return mixed + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function refundCommission() + { + $timestamp = $this->request->post('timestamp'); + $data = $this->request->post('data'); + $aes = new \AES(); + $iv = !empty($timestamp) ? $aes->buildIv($timestamp) : ''; + $decrypted = $aes->decrypt($data, $iv); + Log::error('供销平台退佣金回调:' . var_export($decrypted, true)); + if (!empty($decrypted)) { + $storeConsumptionUserDao = new CommissionDao(); + $result = $storeConsumptionUserDao->refundByCallback($decrypted); + return app('json')->success($result); + } + return app('json')->fail('解密失败'); + } + } \ No newline at end of file diff --git a/app/listener/paySuccess.php b/app/listener/paySuccess.php index c7413042..810c1023 100755 --- a/app/listener/paySuccess.php +++ b/app/listener/paySuccess.php @@ -35,8 +35,10 @@ class paySuccess { try { $orderList = $event['groupOrder']['orderList']; - $storeConsumptionUserDao = new StoreConsumptionUserDao(); - $storeConsumptionUserDao->check($event['groupOrder']['uid'], $event['groupOrder']['group_order_id']); + if ($event['groupOrder']['source'] == 103) { + $storeConsumptionUserDao = new StoreConsumptionUserDao(); + $storeConsumptionUserDao->check($event['groupOrder']['uid'], $event['groupOrder']['group_order_id']); + } foreach ($orderList as $k => $order) { // $StoreProcessing->AutomaticallyCreateOrders($order); diff --git a/route/api.php b/route/api.php index e5eceeec..0c299b63 100755 --- a/route/api.php +++ b/route/api.php @@ -650,6 +650,7 @@ Route::group('api/', function () { Route::get('store/product/town_cloud', 'api.store.product.CloudWarehouse/town'); Route::get('storeActivity/consumption', 'api.store.StoreActivity/consumption'); //消费金列表 Route::post('open/activityCommission', 'api.open/activityCommission'); //活动佣金回调 + Route::post('open/refundCommission', 'api.open/refundCommission'); //活动佣金退款回调 })->middleware(UserTokenMiddleware::class, false); //微信支付回调