6
0
Fork 0

添加资金打款余额支付

release
vine_liutk 2022-02-23 10:35:46 +08:00
parent b5dbb1dfde
commit f3bd7a5673
2 changed files with 83 additions and 9 deletions

View File

@ -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\DealerEarningResource;
use App\Endpoint\Api\Http\Resources\Dealer\DealerEarningSimpleResource; use App\Endpoint\Api\Http\Resources\Dealer\DealerEarningSimpleResource;
use App\Enums\DealerEarningStatus; use App\Enums\DealerEarningStatus;
use App\Enums\DealerWalletAction;
use App\Exceptions\BizException; use App\Exceptions\BizException;
use App\Exceptions\PayPasswordIncorrectException;
use App\Helpers\Paginator; use App\Helpers\Paginator;
use App\Models\DealerEarning; use App\Models\DealerEarning;
use App\Services\Dealer\WalletService;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Throwable;
class EarningController extends Controller class EarningController extends Controller
{ {
@ -67,21 +72,88 @@ class EarningController extends Controller
public function payEarning($id, Request $request) public function payEarning($id, Request $request)
{ {
$earning = DealerEarning::findOrFail($id); $earning = DealerEarning::findOrFail($id);
$user = $request->user();
if (!$earning->isPayer($request->user()->id)) { if (!$earning->isPayer($user->id)) {
throw new BizException('无法操作该订单'); throw new BizException('无法操作该订单');
} }
$input = $request->validate([ $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()) { if ($earning->isPending()) {
$earning->update([ try {
'status' => DealerEarningStatus::Paid, DB::beginTransaction();
'pay_info' => $earning->getPayInfo(), switch ($payWay) {
'pay_image'=> $input['pay_image'], case 'wallet':
'pay_at' => now(), //支付余额
]); $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(); return response()->noContent();
} }

View File

@ -13,4 +13,6 @@ enum DealerWalletAction: int {
case OrderIncome = 8; case OrderIncome = 8;
case TransferIn = 9; case TransferIn = 9;
case TransferOut = 10; case TransferOut = 10;
case EarningIn = 11;
case EarningOut = 12;
} }