Ken 6 дней назад
Родитель
Сommit
fadc315263

+ 37 - 6
app/Http/Controllers/admin/PaymentOrder.php

@@ -7,11 +7,42 @@ use App\Constants\HttpStatus;
 use App\Http\Controllers\Controller;
 use App\Services\PaymentOrderService;
 use Exception;
+use Illuminate\Support\Facades\DB;
 use Illuminate\Validation\ValidationException;
 
 class PaymentOrder extends Controller
 {
 
+
+    public function audit()
+    {
+        try {
+            $validate = [
+                'id' => ['required', 'integer', 'min:1'],
+                'status' => ['required', 'integer', 'in:1,3'],
+            ];
+            $status = request()->input('status', null);
+            if ($status == 3) {
+                $validate['remark'] = ['required', 'string', 'min:1', 'max:120'];
+            }
+            $params = request()->validate($validate);
+            $remark = request()->input('remark', '');
+            if ($params['status'] == 1) {
+                $ret = PaymentOrderService::createPayout($params['id']);
+                if ($ret['code'] !== 0) throw new Exception($ret['msg'], HttpStatus::CUSTOM_ERROR);
+            } else {
+                $ret = PaymentOrderService::withdrawalFailed($params['id'], $remark);
+                if ($ret['code'] !== 0) throw new Exception($ret['msg'], HttpStatus::CUSTOM_ERROR);
+            }
+        } catch (ValidationException $e) {
+            return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
+        } catch (Exception $e) {
+            return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
+        }
+        return $this->success();
+    }
+
+
     /**
      * @apiParam {int} [page]
      * @apiParam {int} [limit]
@@ -37,7 +68,7 @@ class PaymentOrder extends Controller
         } catch (ValidationException $e) {
             return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
         } catch (Exception $e) {
-            return $this->error(intval($e->getCode()));
+            return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
         }
         return $this->success($result);
     }
@@ -45,20 +76,20 @@ class PaymentOrder extends Controller
     /**
      * @description: 查询订单的支付情况
      * @return {*}
-     */    
+     */
     public function check()
     {
         $id = request()->input('id');
-        if(empty($id)){
-            return $this->error(HttpStatus::CUSTOM_ERROR,'参数错误');
+        if (empty($id)) {
+            return $this->error(HttpStatus::CUSTOM_ERROR, '参数错误');
 
         }
         try {
             $result = PaymentOrderService::singlePayOrder($id);
-            $this->success([],$result['msg'] ?? '操作成功');
+            $this->success([], $result['msg'] ?? '操作成功');
         } catch (Exception $e) {
             return $this->error(intval($e->getCode()));
         }
-        return $this->success([],$result['msg'] ?? '操作成功');
+        return $this->success([], $result['msg'] ?? '操作成功');
     }
 }

+ 2 - 0
app/Models/PaymentOrder.php

@@ -9,6 +9,8 @@ namespace App\Models;
  * @property $account
  * @property $card_no
  * @property $status
+ * @property $member_id
+ * @property $id
  */
 class PaymentOrder extends BaseModel
 {

+ 55 - 3
app/Services/PaymentOrderService.php

@@ -323,14 +323,66 @@ class PaymentOrderService extends BaseService
     }
 
     /**
-     * @description: 创建代付订单 (根据 orderId 将钱转到用户指定账户)
+     * 拒绝提现
+     * @description 改变订单状态为失败,将金额退给用户,并记录提现退款的资金日
+     * @param int $orderId 订单ID PaymentOrder 表的ID
+     * @param string $remark 失败原因
+     * @return array
+     */
+    public static function withdrawalFailed($orderId, $remark): array
+    {
+        DB::beginTransaction();
+        try {
+            $order = PaymentOrder::where('id', $orderId)
+                ->where('type', 2)
+                ->where('status', self::STATUS_STAY)
+                ->first();
+            if (!$order) throw new Exception("订单不存在", HttpStatus::CUSTOM_ERROR);
+            // 更新提现记录状态为失败
+            $order->status = self::STATUS_FAIL;
+            $order->remark = $remark;
+            if (!$order->save()) {
+                throw new Exception("更新失败,请重试", HttpStatus::CUSTOM_ERROR);
+            }
+            //钱包余额增加
+            $wallet = WalletService::findOne(['member_id' => $order->member_id]);
+            $beforeBalance = $wallet->available_balance;
+            $availableBalance = bcadd($wallet->available_balance, $order->amount, 10);
+            $wallet->available_balance = $availableBalance;
+            if (!$wallet->save()) {
+                throw new Exception("更新失败,请重试", HttpStatus::CUSTOM_ERROR);
+            }
+            // 记录退款日志
+            BalanceLogService::addLog(
+                $order->member_id,
+                $order->amount,
+                $beforeBalance,
+                $availableBalance,
+                '三方提现',
+                $order->id,
+                '提现失败退款'
+            );
+            DB::commit();
+        } catch (Exception $e) {
+            DB::rollBack();
+            return ['code' => HttpStatus::CUSTOM_ERROR, 'msg' => $e->getMessage()];
+        }
+        return ['code' => 0, 'msg' => 'ok'];
+    }
+
+    /**
+     * 创建代付订单 (根据 orderId 将钱转到用户指定账户)
      * @param int $orderId PaymentOrder 表的ID
      */
-    public static function createPayout(int $orderId)
+    public static function createPayout(int $orderId): array
     {
         DB::beginTransaction();
         try {
-            $order = PaymentOrder::where('id', $orderId)->first();
+            $order = PaymentOrder::where('id', $orderId)
+                ->where('type', 2)
+                ->where('status', self::STATUS_STAY)
+                ->first();
+            if (!$order) throw new Exception("订单不存在", HttpStatus::CUSTOM_ERROR);
             $amount = $order->amount;
             $amount = number_format($amount, 2, '.', '');
             $ret = QianBaoService::payout($amount, $order->order_no, $order->bank_name, $order->account, $order->card_no);