How to delete a cart after successful payment in Laravel

In this tutorial, I’m going to learn how to delete or remove a cart when payment is successful. so follow this tutorial in this tutorial we have mentioned it in a very easy way.

This is the main code:-

Code:-

 $id = Auth::user()->id;
                $cart = addcart::where('admin_id', $id)->get();
                foreach ($cart as $cart) {
                    $cart->isSuccessful = true;
                    $cart->delete();
                }

How to implement it in Store Function:-

if ($response->isSuccessful()) {

                Log::info("response paypal success m kya aa rha h");

                $arr = $response->getData();

                $payment = new paypal();
                $payment->payment_id = $arr['id'];
                $payment->payer_id = $arr['payer']['payer_info']['payer_id'];
                $payment->payer_email = $arr['payer']['payer_info']['email'];
                $payment->amount = $arr['transactions'][0]['amount']['total'];
                $payment->currency = env('PAYPAL_CURRENCY');
                $payment->payment_status = $arr['state'];
                $payment->admin_id = $request->session()->get('admin_id');
                $payment->user_name = $request->session()->get('user_name');
                $payment->slug = $request->session()->get('slug');
                $payment->admin_email = $request->session()->get('admin_email');
                $payment->org_slug = $request->session()->get('org_slug');
                
                $influencerEmails = $request->session()->get('influencer_email', []);
                $influencerNames = $request->session()->get('influencer_name', []);
                if (!empty($influencerEmails)) {

                    $influencers = [];
                    for ($i = 0; $i < count($influencerEmails); $i++) {
                        $influencers[] = [
                            'influencer_email' => $influencerEmails[$i],
                            // 'influencer_name' => $influencerNames[$i]
                        ];
                    }
                    $payment->influencer_email = json_encode($influencers);
                }
                if (!empty($influencerNames) ) {

                    $influencersname = [];
                    for ($i = 0; $i < count($influencerNames); $i++) {
                        $influencersname[] = [
                            // 'influencer_email' => $influencerEmails[$i],
                            'influencer_name' => $influencerNames[$i]
                        ];
                    }
                    $payment->influencer_name = json_encode($influencersname);
                }

                $payment->save();
                // session()->forget('addcart');

                $id = Auth::user()->id;
                $cart = addcart::where('admin_id', $id)->get();
                foreach ($cart as $cart) {
                    $cart->isSuccessful = true;
                    $cart->delete();
                }
                // Store the admin_id in the session
                // $request->session()->put('admin_id', $request->input('admin_id'));
                // return "Payment is Successfull. Your Transaction Id is : " . $arr['id'];
                // return back()->with("success", "Payment is Successfull. Your Transaction Id is : " . $arr['id']);
                return redirect()->route('carts')->with("success", "Payment is Successfull. Your Transaction Id is : " . $arr['id']);
            } 

Hopefully, It will help You !!!!!!

Related Posts

Exploring Financial Operations Workflows in Modern Cloud Environments

Introduction The Certified FinOps Professional is the definitive benchmark for experts looking to master the intersection of finance, engineering, and business. As organizations transition from traditional data…

Read More

Strategic Certified FinOps Engineer integrates governance with cloud operations

Introduction The shift to cloud computing has fundamentally altered how businesses manage infrastructure, but it has also introduced significant financial complexities that many engineering teams struggle to…

Read More

Certified FinOps Manager Knowledge for Cloud Financial Governance

Introduction The shift toward cloud-native infrastructure has brought undeniable speed, but it has also introduced significant financial complexity. The Certified FinOps Manager is a professional designation designed…

Read More

Smart Career Growth Through Certified FinOps Architect Learning Journey

Introduction The Certified FinOps Architect is a professional certification designed to help engineers, cloud professionals, and managers optimize cloud financial operations and cost efficiency. This guide is…

Read More

CDOM – Certified DataOps Manager Learning Path for Modern Data Professionals

Introduction The CDOM – Certified DataOps Manager is a professional designation designed to bridge the gap between data engineering and operational excellence. This guide is written for…

Read More

Professional development journey using CDOA – Certified DataOps Architect

Introduction The CDOA – Certified DataOps Architect is a professional designation designed to address the unique challenges of managing and scaling data delivery in cloud-native environments. This…

Read More