diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index ba168e48..96813802 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -24,7 +24,6 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule) { - $schedule->call(fn () => logger('schedule running'))->everyMinute(); $schedule->command('order-profit:send')->daily(); $schedule->command('order-profit:check')->daily(); } diff --git a/app/Listeners/OrderDistribute.php b/app/Listeners/OrderDistribute.php index a4d6028c..495a2a6c 100644 --- a/app/Listeners/OrderDistribute.php +++ b/app/Listeners/OrderDistribute.php @@ -35,24 +35,8 @@ class OrderDistribute try { DB::beginTransaction(); $service = new DistributeService(); - $profits = $service->storeByOrder($order); + $service->storeByOrder($order); DB::commit(); - - // 如果没有提成记录 - if ($profits->count() <= 0 && in_array($order->pay_way, [PayWay::WxpayApp, PayWay::WxpayH5, PayWay::WxpayJsApi, PayWay::WxpayMiniProgram])) { - // 解冻资金 - $sn = serial_number(); - $result = (new WxpayService())->finishShare($order->out_trade_no, $sn); - if (data_get($result, 'return_code') != 'SUCCESS') { - logger()->error('微信分账-解冻资金失败', $result); - } - else if (data_get($result, 'result_code') != 'SUCCESS') { - logger()->error('微信分账-解冻资金失败', $result); - } else { - $attributes = ['finish_sn' => $sn, 'status' => 'N']; - $order->update(['wx_share' => $order->wx_share ? array_merge($order->wx_share, $attributes) : $attributes]); - } - } } catch (Exception $e) { logger('添加提成记录失败'); logger()->error($e); diff --git a/app/Services/OrderService.php b/app/Services/OrderService.php index 5076298e..0013c0a8 100644 --- a/app/Services/OrderService.php +++ b/app/Services/OrderService.php @@ -23,7 +23,7 @@ use App\Models\ShippingAddress; use App\Models\SocialiteUser; use App\Models\Store\{Store, Desk, DeviceRecord}; use App\Models\UserCoupon; -use App\Models\{User, OrderPre, Tag}; +use App\Models\{Agent, User, OrderPre, Tag}; use App\Services\Payment\WxpayService; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\QueryException; @@ -1240,12 +1240,22 @@ class OrderService throw new BizException('支付方式 非法'); } + // 是否开启分账 + $share = 'Y'; + $user = $order->user; + $agentIds = User::whereIn('id', $user->userInfo->parent_ids)->pluck('agent_id'); + $ratio = Agent::whereIn('id', $agentIds)->sum('ratio'); + // 没有提成 + if ($ratio == 0) { + $share = 'N'; + } + $params = [ 'body' => app_settings('app.app_name').'-商城订单', 'out_trade_no' => $payLog->pay_sn, 'total_fee' => $order->total_amount, 'trade_type' => $tradeType->value, - 'profit_sharing' => 'Y', + 'profit_sharing' => $share, ]; if ($tradeType === WxpayTradeType::JSAPI) { @@ -1269,7 +1279,8 @@ class OrderService }); // 更新订单冻结状态 - $order->update(['wx_share' => $order->wx_share ? array_merge($order->wx_share, ['status' => 'Y']) : ['status' => 'Y']]); + $attributes = ['status' => $share]; + $order->update(['wx_share' => $order->wx_share ? array_merge($order->wx_share, $attributes) : $attributes]); } elseif ($payLog->isAlipay()) { $params = [ 'subject' => app_settings('app.app_name').'-商城订单', diff --git a/tests/Feature/ExampleTest.php b/tests/Feature/ExampleTest.php index 6e5eb091..9f10a184 100644 --- a/tests/Feature/ExampleTest.php +++ b/tests/Feature/ExampleTest.php @@ -3,6 +3,8 @@ namespace Tests\Feature; use App\Enums\PayWay; +use App\Models\Agent; +use App\Models\User; use App\Services\DistributeService; use App\Services\Payment\WxpayService; use Illuminate\Support\Facades\DB; @@ -18,8 +20,16 @@ class ExampleTest extends TestCase */ public function test_example() { - $result = ['status' => 'N', 'sn' => '21322132']; - dump($result ? array_merge($result, ['status' => 'Y']) : ['status' => 'Y']); + $share = 'Y'; + $user = User::find(4); + $agentIds = User::whereIn('id', $user->userInfo->parent_ids)->pluck('agent_id'); + dump($user->userInfo->parent_ids); + $ratio = Agent::whereIn('id', $agentIds)->sum('ratio'); + // 没有提成 + if ($ratio == 0) { + $share = 'N'; + } + dump($share); $this->assertTrue(true); } }