示例#1
0
文件: Order.php 项目: poiuty/midas
 /**
  * Cancel user order and refund coins
  *
  * @param Order         $order
  */
 public static function cancel(Order $order)
 {
     $response = [];
     if ($order->isCancelable() && $order->user_id == Auth::user()->id) {
         try {
             DB::beginTransaction();
             if ($order->type == 'sell') {
                 $user_wallet_id = $order->wantWallet()->first()->generateUserAddress(Auth::user());
             } else {
                 $user_wallet_id = $order->wallet()->first()->generateUserAddress(Auth::user());
             }
             $user_wallet = WalletsAddress::where('id', $user_wallet_id)->first();
             $total_price = round($order->amount * $order->price, 8);
             if ($order->type == 'sell') {
                 $need_amount = $order->amount;
             } else {
                 $need_amount = $total_price;
             }
             $user_wallet->deposit($need_amount);
             $order->status = "cancel";
             $order->save();
             $response['success'] = true;
             $response['message'] = 'Order canceled';
             Cache::tags('user' . Auth::user()->id)->forget('wallets');
             DB::commit();
         } catch (\Exception $e) {
             DB::rollback();
             $response['error'] = $e->getMessage();
         }
     } else {
         $response['success'] = false;
         $response['error'] = 'You cant cancel this order';
     }
     return $response;
 }