$itemPrice = CartItem::where('order_code',$orderCode)->pluck('total_price');
$totalPrices = $itemPrice->sum();
$orderCode = session('order_code');
$gateway = 'banka';
$total = $totalPrices;
$bin_number = rand(1000,10000);
$order_id = rand(1,100);
$status = 1;
$error_message = null;
try
{
$paymentSuccessfull = false;
if(!$paymentSuccessfull)
{
throw new \Exception('basarisiz');
}
Transaction::create([
'gateway'=>$gateway,
'order_id'=>$order_id,
'total'=>$total,
'eror_message'=>$error_message,
'bin_number'=>$bin_number,
'status'=>$status,
]);
session()->forget('order_code');
return response()->json(['message' => 'siparis tamamlandi']);
}
catch(\Exception $e)
{
$error_message = $e->getMessage();
$status = 0;
Transaction::where('order_id', $order_id)->update([
'eror_message' => $error_message,
'status' => $status,
]);
return response()->json(['error' => $error_message]);
}
我想让这段代码更可读,消除冗余,只执行一次创建操作,并有条件地更改状态值。“
我试图让代码更干净,但在这样做的时候,我无法将数据保存到数据库中。“