Level 1
Thank you for your response. but I just want to innovate/update my code. I cant afford to use another platform this is just for study purposes.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Good day, can someone give me an example of how to implement a cart checkout with multiple items? Like I have 5 items in my cart and I want to check out all the 5 items in a single checkout.
My current code can only check out a single item at a time.
public function checkoutItem(Request $request)
{
$id = $request->user_id;
//Status TESTED AND OK.
try{
//Check if cart has an item whith status id 1 (Added).
$cart = \DB::table('cart_items')
->whereRaw('cart_items.id = ' .$request->cart_items_id.
' AND cart_items.product_id = ' .$request->product_id.
' AND cart_items.status_id = 1')->count();
if($cart != NULL){
Stripe::setApiKey( config('services.stripe.secret') );
$student= \App\User::find($id);
//Get the product details using the product_id
$product= \App\Product::find($request->product_id);
//Check if student's has no stripe id.
if($student->stripe_id == NULL){
//create stripe id for student.
$customer= Customer::create([
'email' => request('stripeEmail'),
'source' => request('stripeToken')
]);
//Update stripe_id from null to newly created stripe_id generated by stripe.
$user= \App\User::where('id', $id)
->update( array('stripe_id' => $customer->id) );
//Charge the student.
Charge::create([
'customer' => $customer->id,
'description' => $product->description,
'amount' => ($product->price * 100) * $request->qty, //Should be in cent.
'currency' => 'aud',
'receipt_email' => $request->stripeEmail,
'metadata' => array(
"product_id" => $request->product_id,
"product_name" => $product->name,
"product qty" => $request->qty,
"product_type_id" => $product->product_type_id,
"sub_type_id" => $product->service_sub_type_id,
"user_id" => $id,
"fullname" => $student->first_name." ".$student->last_name,
"email" => $request->stripeEmail
)
]);
//If student's has already stripe id, create a charge.
}else{
//Charge
Charge::create([
'customer' => $student->stripe_id,
'description' => $product->description,
'amount' => ($product->price * 100) * $request->qty, //Should be in cent.
'currency' => 'aud',
'receipt_email' => $student->email,
'metadata' => array(
"product_id" => $request->product_id,
"product_name" => $product->name,
"product qty" => $request->qty,
"product_type_id" => $product->product_type_id,
"sub_type_id" => $product->service_sub_type_id,
"user_id" => $id,
"fullname" => $student->first_name." ".$student->last_name,
"email" => $student->email
)
]);
}
//If successfully charge, status_id wll be updated from 1(Added) to 2(Checkout).
$upd_cart_items= \DB::table('cart_items')
->leftjoin('carts', 'cart_items.cart_id', '=', 'carts.id')
->whereRaw('cart_items.id = ' .$request->cart_items_id)
->update(['cart_items.quantity' => $request->qty,'cart_items.status_id' => 2,
'cart_items.updated_at' => Carbon::now()->toDateTimeString()]);
return ['success' => true, 'message' => "Product was successfully purchased"];
}
}catch(\Stripe\Error\Card $e){
return['error' => $e];
}
}
Please or to participate in this conversation.