unicodeveloper / laravel-paystack

:credit_card: :package: :moneybag: Laravel 6, 7, 8, 9, 10 and 11 Package for Paystack
https://paystack.co
MIT License
615 stars 313 forks source link

Unable to redirect to paystack payment gateway. #174

Closed habeeblht17 closed 1 year ago

habeeblht17 commented 1 year ago

Am have this cURL error 3: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for /transaction/initialize error when redirecting to paystack payment gateway. How can I solve this error? Below is the code:

/**

and

private function placeOrder($payment_method) { $carts = CartManagement::whereUserId(@Auth::user()->id)->get(); $order = new Order(); $order->user_id = Auth::user()->id; $order->order_number = rand(100000, 999999); $order->sub_total = $carts->sum('price'); $order->discount = $carts->sum('discount'); $order->platform_charge = get_platform_charge($carts->sum('price')); $order->current_currency = get_currency_code(); $order->grand_total = $order->sub_total + $order->platform_charge; $order->payment_method = $payment_method;

    $payment_currency = '';
    $conversion_rate = '';

    if ($payment_method == 'paypal') {
        $payment_currency = get_option('paypal_currency');
        $conversion_rate = get_option('paypal_conversion_rate') ? get_option('paypal_conversion_rate') : 0;
    } elseif ($payment_method == 'stripe') {
        $payment_currency = get_option('stripe_currency');
        $conversion_rate = get_option('stripe_conversion_rate') ? get_option('stripe_conversion_rate') : 0;
    } elseif ($payment_method == 'bank') {
        $payment_currency = get_option('bank_currency');
        $conversion_rate = get_option('bank_conversion_rate') ? get_option('bank_conversion_rate') : 0;
    } elseif ($payment_method == 'mollie') {
        $payment_currency = get_option('mollie_currency');
        $conversion_rate = get_option('mollie_conversion_rate') ? get_option('mollie_conversion_rate') : 0;
    } elseif ($payment_method == 'instamojo') {
        $payment_currency = get_option('im_currency');
        $conversion_rate = get_option('im_conversion_rate') ? get_option('im_conversion_rate') : 0;
    } elseif ($payment_method == 'paystack') {
        $payment_currency = get_option('paystack_currency');
        $conversion_rate = get_option('paystack_conversion_rate') ? get_option('paystack_conversion_rate') : 0;
    }

    $order->payment_currency = $payment_currency;
    $order->conversion_rate = $conversion_rate;
    if ($conversion_rate) {
        $order->grand_total_with_conversation_rate = ($order->sub_total + $order->platform_charge) * $conversion_rate;
    }

    $order->save();

    foreach ($carts as $cart) {
        if ($cart->course_id) {
            $order_item = new Order_item();
            $order_item->order_id = $order->id;
            $order_item->user_id = Auth::id();
            $order_item->course_id = $cart->course_id;
            $order_item->owner_user_id = $cart->course ? $cart->course->user_id : null;
            $order_item->unit_price = $cart->price;
            if (get_option('sell_commission')) {
                $order_item->admin_commission = admin_sell_commission($cart->price);
                $order_item->owner_balance = $cart->price - admin_sell_commission($cart->price);
                $order_item->sell_commission = get_option('sell_commission');
            } else {
                $order_item->owner_balance = $cart->price;
            }

            $order_item->save();
        } elseif ($cart->bundle_id) {
            /*
             If bundle course add. we only calculate all things in order_items 1 time.
                and all bundle courses save in order_items table.
             If any course of bundle already purchased with paid. Those course won't be added again.
             */
            $order_item = new Order_item();
            $order_item->order_id = $order->id;
            $order_item->user_id = Auth::user()->id;
            $order_item->bundle_id = $cart->bundle_id;
            $order_item->owner_user_id = $cart->bundle ? $cart->bundle->user_id : null;
            $order_item->unit_price = $cart->price;
            if (get_option('sell_commission')) {
                $order_item->admin_commission = admin_sell_commission($cart->price);
                $order_item->owner_balance = $cart->price - admin_sell_commission($cart->price);
                $order_item->sell_commission = get_option('sell_commission');
            } else {
                $order_item->owner_balance = $cart->price;
            }
            $order_item->type = 3; //bundle course
            $order_item->save();

            /*
             * All bundle course added but not calculate balance, commission etc
             */
            foreach ($cart->bundle_course_ids ?? [] as $bundle_course_id) {
                /*
                need to add bundle course in order list
                Old paid course check with bundle course
                */

                $paidOrderIds = Order::where('user_id', auth()->id())->where('payment_status', 'paid')->pluck('id')->toArray();
                $freeOrderIds = Order::where('user_id', auth()->id())->where('payment_status', 'free')->pluck('id')->toArray();
                $orderIds = array_merge($paidOrderIds, $freeOrderIds);
                $orderCourseIds = Order_item::whereIn('order_id', $orderIds)->pluck('course_id')->toArray();

                if (in_array($bundle_course_id, $orderCourseIds) == false){
                    $order_item = new Order_item();
                    $order_item->order_id = $order->id;
                    $order_item->user_id = Auth::user()->id;
                    $order_item->bundle_id = $cart->bundle_id;
                    $order_item->course_id = $bundle_course_id;
                    $order_item->owner_user_id = $cart->bundle->user_id;
                    $order_item->type = 3; //bundle course
                    $order_item->save();
                }
            }

        } elseif ($cart->consultation_slot_id) {
            $order_item = new Order_item();
            $order_item->order_id = $order->id;
            $order_item->user_id = Auth::id();
            $order_item->owner_user_id = is_array($cart['consultation_details']) ? $cart['consultation_details'][0]->instructor_user_id : null;
            $order_item->consultation_slot_id = $cart->consultation_slot_id;
            $order_item->consultation_date = $cart->consultation_date;
            $order_item->unit_price = $cart->price;
            if (get_option('sell_commission')) {
                $order_item->admin_commission = admin_sell_commission($cart->price);
                $order_item->owner_balance = $cart->price - admin_sell_commission($cart->price);
                $order_item->sell_commission = get_option('sell_commission');
            } else {
                $order_item->owner_balance = $cart->price;
            }
            $order_item->type = 4;
            $order_item->save();

            //Start:: Need to add Booking History
            $booking = new BookingHistory();
            $booking->order_id = $order->id;
            $booking->order_item_id = $order_item->id;
            $booking->instructor_user_id = is_array($cart['consultation_details']) ? $cart['consultation_details'][0]->instructor_user_id : null;
            $booking->student_user_id = Auth::id();
            $booking->consultation_slot_id = $cart->consultation_slot_id;
            $booking->date = is_array($cart['consultation_details']) ? $cart['consultation_details'][0]->date : null;
            $booking->day = is_array($cart['consultation_details']) ? $cart['consultation_details'][0]->day : null;
            $booking->time = is_array($cart['consultation_details']) ? $cart['consultation_details'][0]->time : null;
            $booking->duration = is_array($cart['consultation_details']) ? $cart['consultation_details'][0]->duration : null;
            $booking->type = $cart->consultation_available_type;
            $booking->status = 0; //Pending
            $booking->save();

            //End:: Add Booking History
        }

    }
    CartManagement::whereUserId(@Auth::user()->id)->delete();
    return $order;
}

Need assistance on this.

habeeblht17 commented 1 year ago

The issue arise because am using the {{ csrf_field() }} {{-- works only when using laravel 5.1, 5.2 --}} in my view page which is only supported in laravel 5 version. When i make use of this , it works perfectly.