OrderService.php 754 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Order;
  4. use Illuminate\Support\Facades\DB;
  5. class OrderService
  6. {
  7. public $order;
  8. public function __construct(Order $order)
  9. {
  10. $this->order = $order;
  11. }
  12. public function cancel():bool
  13. {
  14. $order = $this->order;
  15. DB::beginTransaction();
  16. $order->status = 2;
  17. if (!$order->save()) {
  18. DB::rollBack();
  19. return false;
  20. }
  21. if ($order->balance_amount) {
  22. $userService = new UserService();
  23. if (!$userService->addBalance($order->user_id, $order->balance_amount)) {
  24. DB::rollBack();
  25. return false;
  26. }
  27. }
  28. DB::commit();
  29. return true;
  30. }
  31. }