Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return Paystack::getAuthorizationUrl($data)->redirectNow(); #172

Open
felixkaleke opened this issue Apr 3, 2023 · 5 comments
Open

return Paystack::getAuthorizationUrl($data)->redirectNow(); #172

felixkaleke opened this issue Apr 3, 2023 · 5 comments

Comments

@felixkaleke
Copy link

This is not redirecting as its supposed to redirect , what might be the issue?, I have my secret and public key in my files correctly

@Chuby11
Copy link

Chuby11 commented Apr 30, 2023

same here. I have everything set up and the function isn't redirecting my website to the paystack website. Can someone help, if this problem has already been solved?

@goodmuyis
Copy link

This is not redirecting as its supposed to redirect , what might be the issue?, I have my secret and public key in my files correctly

Still working here. Next time always remember to share your code

@habeeblht17
Copy link

Hello,
Am having problem redirecting to paystack payment gateway. Am having this error -> cURL error 3: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for /transaction/initialize

Here is the code below.

public function paystackPayment(Request $request)
{
try {
$order = $this->placeOrder('paystack');
$order->paystack_reference_number = $request->reference;
$order->save();
return Paystack::getAuthorizationUrl()->redirectNow();
dd();
} catch (\Exception $e) {
$this->showToastrMessage('error', 'The paystack token has expired. Please refresh the page and try again.');
return redirect()->back();
}
}

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;
}

}

@goodmuyis
Copy link

goodmuyis commented Jun 4, 2023

@habeeblht17 what is your Laravel version

Paystack::getAuthorizationUrl()->redirectNow();
The line above expects payment data in two ways
1=> Through the submitted form data, if this is your case use it like this
return Paystack::getAuthorizationUrl()->redirectNow();

2=> Pass the data from your controller instead of a form, is this is the case then you need to build your array day and pass the array to the getAuthorizationUrl() method. example below

$data = array(
        "amount" => 700 * 100,
        "reference" => '4g4g5485g8545jg8gj',
        "email" => '[email protected]',
        "currency" => "NGN",
        "orderID" => 23456,
    );

return Paystack::getAuthorizationUrl($data)->redirectNow();

You missed the guide on the info page

@habeeblht17
Copy link

habeeblht17 commented Jun 4, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants