how to fix the preflight error in Lavavel V9.5

25 Views Asked by At

I have an order controller that redirects users to stripe's checkout once an order has been placed I have configured the config/cors.php and added the handleCors to the top of the middleware as shown below


cors.php file 
<?php

return [

    'paths' => ['sanctum/csrf-cookie','create/order'],

    'allowed_methods' => ['*'],

    'allowed_origins' => [ 
        'http://127.0.0.1:8000',  // laravel's server
        'http://localhost:5173'  // vue's server
     ],

    'allowed_origins_patterns' => ['*'],

    'allowed_headers' => ['*'],

    'exposed_headers' => [],

    'max_age' => 0,

    'supports_credentials' => true,

];

kernel.php

protected $middleware = [
        // \App\Http\Middleware\TrustHosts::class,
        \Illuminate\Http\Middleware\HandleCors::class,
        \App\Http\Middleware\TrustProxies::class,
        \App\Http\Middleware\PreventRequestsDuringMaintenance::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        
    ];

function to create an order

 public function create(Request $request){

        $validatedData = $request->validate([
            'firstname' => 'required|max:10|min:3',
            'lastname' => 'required|max:10|min:3',
            'email' => 'required|email',
            'phonenumber' => 'required',
            'county' => 'required|min:5|max:20',
            'subcounty' => 'required|min:5|max:20',
            'ward' => 'required|min:5|max:20',
            'amount' => 'required|numeric',
        ]);
    
        // creating delivery details 
        $deliveryDetails = [
            'firstname' => $validatedData['firstname'],
            'lastname' => $validatedData['lastname'],
            'email' => $validatedData['email'],
            'phone_number' => $validatedData['phonenumber'],
            'county' => $validatedData['county'],
            'sub_county' => $validatedData['subcounty'],
            'ward' => $validatedData['ward'],
        ];
    
        // getting users orders
        $orders = $request->input('order');

        $orderItems = [];

        try {
            DB::beginTransaction();

                $order = Order::create([
                    'amount' => $validatedData['amount'],
                    'tracking_id' => Str::random(10),
                ]);
                
                $deliveryDetails['order_id'] = $order->id;

                DeliveryDetails::create($deliveryDetails);
    
                foreach ($orders as $orderData) {
                    $productID = $orderData['productID'];

                    foreach ($orderData['order_details'] as $detail) {
                        $detail['product_id'] = $productID;
                        $detail['order_id'] = $order->id;
                        $orderItems [] = $detail;
                    }
                }

             OrderItems::insert($orderItems);

             $stripe_session = $this->Stripe_session($orders);

             //DB::commit();

             return redirect()->away($stripe_session->url);
        } 
        catch (\Throwable $th) {
            DB::rollBack();
            return back()->with('message', 'An error occurred. Try again later.');
        }
    }

Am I missing anything that causes the preflight error? An help will be appreciated.

An trying to redirect the user to stripe checkout for payment

0

There are 0 best solutions below