service_id = $request->service; $order->user_id = Auth::user()->id; $order->quantity = $request->quantity; $order->url = $request->url; $order->note = $request->note ?: ''; $order->status = 'processing'; if (!$this->validateOrder($request)){ abort(422); } $cost = ceil($order->quantity*$order->service->price/1000); $user->credits = $user->credits - $cost; if ($cost > $user->credits) { abort(520, 'Insufficient Credits'); } $user->save(); $order->save(); } // Should probably check for other things like service availability public function validateOrder($order) { $service = Service::find($order->service); if ($order->quantity < $service->minimum || $order->quantity > $service->maximum) { return false; } return true; } public function changeURL(Request $request) { $validated = $request->validate([ 'order' => 'required', 'url' => 'required' ]); $order = Order::find($request->order); $user = Auth::user(); if (!in_array($order->status, ['processing', 'error'])) { abort(422); } $order->url = $request->url; $order->save(); } }