Facebook
From Ivory Horse, 3 Years ago, written in PHP.
Embed
Download Paste or View Raw
Hits: 54
  1. public function requestPaymentPlafond(Request $request){
  2.         try{
  3.             DB::beginTransaction();
  4.             $currentUser = Auth::user();
  5.             $account = Account::whereId($request->input('accountId'))->first();
  6.             $plafond = $account->plafond;
  7.             $accountId = $request->input('accountId');
  8.             $totalToCheckout = $request->input('totalToCheckout');
  9.             $payPlafond = $request->input('payPlafond');
  10.  
  11.             if($payPlafond){
  12.                 if($plafond >= $totalToCheckout){
  13.                     $transaction = AccountTransactions::create([
  14.                         'account_id'    => $accountId,
  15.                         'type'          => 2, //1 -> rechargement; 2 -> payment
  16.                         'value'         => $totalToCheckout,
  17.                         'created_by'    => $currentUser->id,
  18.                     ]);
  19.                     $account->plafond -= $totalToCheckout;
  20.                     $account->updated_by = $currentUser->id;
  21.                     $account->save();
  22.                     $paymentController = new PaymentsController();
  23.                     $randomId = $paymentController->generateRandomId();
  24.                     $payment = Payment::create([
  25.                         'payment_method_id'         => 0,
  26.                         'random_id'                 => $randomId,
  27.                         'value'                     => 0,
  28.                         'account_transaction_id'    => $transaction->id,
  29.                         'state'                     => 3,
  30.                         'created_by'                => $currentUser->id,
  31.                     ]);
  32.                     DB::commit();
  33.                     return response()->json(true,200);
  34.                 }else{
  35.                     return response()->json(false,404);
  36.                 }
  37.             }else{
  38.                 throw new \Exception('Permission denied');
  39.             }
  40.  
  41.  
  42.         } catch (\Exception | \Throwable  $e) {
  43.             dd($e);
  44.             DB::rollback();
  45.             $parametersToLog = [
  46.                 'error' => "Message: '{$e->getMessage()}' | File: '{$e->getFile()}' | Line: {$e->getLine()}"
  47.             ];
  48.             Log::error('[OrdersController: requestPaymentPlafond] Error: ' . json_encode($parametersToLog));
  49.             return response()->json(trans('generic.error'), 500);
  50.         }
  51.     }