mirror of
				https://github.com/v2board/v2board.git
				synced 2025-10-31 09:21:46 +08:00 
			
		
		
		
	add balance payment
This commit is contained in:
		| @@ -3,6 +3,7 @@ | ||||
| namespace App\Services; | ||||
|  | ||||
| use App\Models\Order; | ||||
| use Illuminate\Support\Facades\DB; | ||||
|  | ||||
| class OrderService | ||||
| { | ||||
| @@ -13,8 +14,20 @@ class OrderService | ||||
|         $this->order = $order; | ||||
|     } | ||||
|  | ||||
|     public function cancel() | ||||
|     public function cancel():void | ||||
|     { | ||||
|  | ||||
|         $order = $this->order; | ||||
|         DB::beginTransaction(); | ||||
|         $order->status = 2; | ||||
|         if (!$order->save()) { | ||||
|             DB::rollBack(); | ||||
|         } | ||||
|         if ($order->balance_amount) { | ||||
|             $userService = new UserService(); | ||||
|             if (!$userService->addBalance($order->user_id, $order->balance_amount)) { | ||||
|                 DB::rollBack(); | ||||
|             } | ||||
|         } | ||||
|         DB::commit(); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -47,4 +47,20 @@ class UserService | ||||
|     { | ||||
|         return User::all(); | ||||
|     } | ||||
|  | ||||
|     public function addBalance(int $userId, int $balance):bool | ||||
|     { | ||||
|         $user = User::find($userId); | ||||
|         if (!$user) { | ||||
|             return false; | ||||
|         } | ||||
|         $user->balance = $user->balance + $balance; | ||||
|         if ($user->balance < 0) { | ||||
|             return false; | ||||
|         } | ||||
|         if (!$user->save()) { | ||||
|             return false; | ||||
|         } | ||||
|         return true; | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user