From f3bd7a56736d99acb33d6d4e4dcea63f8ef508d4 Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Wed, 23 Feb 2022 10:35:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=B5=84=E9=87=91=E6=89=93?= =?UTF-8?q?=E6=AC=BE=E4=BD=99=E9=A2=9D=E6=94=AF=E4=BB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Dealer/EarningController.php | 90 +++++++++++++++++-- app/Enums/DealerWalletAction.php | 2 + 2 files changed, 83 insertions(+), 9 deletions(-) diff --git a/app/Endpoint/Api/Http/Controllers/Dealer/EarningController.php b/app/Endpoint/Api/Http/Controllers/Dealer/EarningController.php index b46fced5..5059c041 100644 --- a/app/Endpoint/Api/Http/Controllers/Dealer/EarningController.php +++ b/app/Endpoint/Api/Http/Controllers/Dealer/EarningController.php @@ -6,10 +6,15 @@ use App\Endpoint\Api\Http\Controllers\Controller; use App\Endpoint\Api\Http\Resources\Dealer\DealerEarningResource; use App\Endpoint\Api\Http\Resources\Dealer\DealerEarningSimpleResource; use App\Enums\DealerEarningStatus; +use App\Enums\DealerWalletAction; use App\Exceptions\BizException; +use App\Exceptions\PayPasswordIncorrectException; use App\Helpers\Paginator; use App\Models\DealerEarning; +use App\Services\Dealer\WalletService; use Illuminate\Http\Request; +use Illuminate\Support\Facades\DB; +use Throwable; class EarningController extends Controller { @@ -67,21 +72,88 @@ class EarningController extends Controller public function payEarning($id, Request $request) { $earning = DealerEarning::findOrFail($id); - - if (!$earning->isPayer($request->user()->id)) { + $user = $request->user(); + if (!$earning->isPayer($user->id)) { throw new BizException('无法操作该订单'); } $input = $request->validate([ - 'pay_image' => ['bail', 'required', 'string'], + 'pay_way' => ['bail', 'string'], + 'pay_image' => ['bail', 'required_if:pay_way,offline', 'string'], + 'pay_password' => ['bail', 'required_if:pay_way,wallet'], + ], [ + 'pay_image.required_if' => '打款凭证 不能为空。', + 'pay_password.required_if' => '支付密码 不能为空。', + ], [ + 'pay_image' => '打款凭证', + 'pay_way' => '支付方式', + 'pay_password' => '支付密码', ]); + + $payWay = $input['pay_way'] ?? 'offline'; + + if ( + $payWay === 'wallet' && + !$user->wallet?->verifyPassword($input['pay_password']) + ) { + throw new PayPasswordIncorrectException(); + } + if ($earning->isPending()) { - $earning->update([ - 'status' => DealerEarningStatus::Paid, - 'pay_info' => $earning->getPayInfo(), - 'pay_image'=> $input['pay_image'], - 'pay_at' => now(), - ]); + try { + DB::beginTransaction(); + switch ($payWay) { + case 'wallet': + //支付余额 + $walletService = new WalletService(); + + // 扣除打款人余额 + $walletService->changeBalance( + $user, + bcmul($earning->total_earnings, '-1', 2), + DealerWalletAction::EarningOut, + "渠道补贴:【{$earning->remark}】", + $earning + ); + + // 增加收款人余额 + if ($earning->user) { + $walletService->changeBalance( + $earning->user, + $earning->total_earnings, + DealerWalletAction::EarningIn, + "渠道补贴:【{$earning->remark}】", + $earning + ); + } + + $earning->update([ + 'status' => DealerEarningStatus::Completed, + 'pay_info' => $earning->getPayInfo(), + 'pay_at' => now(), + ]); + + break; + case 'offline': + $earning->update([ + 'status' => DealerEarningStatus::Paid, + 'pay_info' => $earning->getPayInfo(), + 'pay_image'=> $input['pay_image'], + 'pay_at' => now(), + ]); + break; + default: + throw new BizException('支付方式不存在'); + break; + } + DB::commit(); + } catch (BizException $e) { + throw $e; + } catch (Throwable $th) { + DB::rollBack(); + report($th); + throw new BizException('系统错误,请稍后再试'); + } } return response()->noContent(); } diff --git a/app/Enums/DealerWalletAction.php b/app/Enums/DealerWalletAction.php index 328a5809..6eae5c51 100644 --- a/app/Enums/DealerWalletAction.php +++ b/app/Enums/DealerWalletAction.php @@ -13,4 +13,6 @@ enum DealerWalletAction: int { case OrderIncome = 8; case TransferIn = 9; case TransferOut = 10; + case EarningIn = 11; + case EarningOut = 12; }