How to delete a cart after successful payment in Laravel

Posted by

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 !!!!!!