Home > Mobile >  I am getting an error of undefined variable defined within the if statement block
I am getting an error of undefined variable defined within the if statement block

Time:02-24

Hello everyone!Am having an error below:

Undefined variable: c2bTransaction {"userId":12,"exception":"[object] (ErrorException(code: 0): Undefined variable: c2bTransaction at BugController.php:140 I have been trying to figure out what is failing with the below codeAny help will be highly appreciated.

public function paysomething(Request $request)
{


    ///$c2bTransaction =[];
    if(Session::has('order_id')) {
        $combined_order = CombinedOrder::find(Session::get('combined_order_id'));

        $request->Msisdn   = (substr($request->Msisdn, 0, 1) == ' ') ? str_replace(' ', '', $request->Msisdn) : $request->Msisdn;
        $request->Msisdn   = (substr($request->Msisdn, 0, 1) == '0') ? preg_replace('/^0/', '254', $request->Msisdn) : $request->Msisdn;

        $c2bTransaction   = STK::send($request->Msisdn, round($order->grand_total), $request->BillRefNumber);

        if(array_key_exists('errorMessage', $c2bTransaction)) {
            flash($c2bTransaction['errorMessage'])->error();
            return redirect(route('home'));
        }

        // dd($c2bTransaction);

        $combined_order->request    = $c2bTransaction['MerchantRequestID'];
        $combined_order->save();
        //$c2bTransaction = $mpesa->c2b(env('MPESA_SHORT_CODE'), $request->CommandID, $order->grand_total, $request->Msisdn, $request->BillRefNumber);
    } else if(Session::has('payment_type') && Session::get('payment_type') == 'wallet_payment') {
        $request->Msisdn   = (substr($request->Msisdn, 0, 1) == ' ') ? str_replace(' ', '', $request->Msisdn) : $request->Msisdn;
        $request->Msisdn   = (substr($request->Msisdn, 0, 1) == '0') ? preg_replace('/^0/', '254', $request->Msisdn) : $request->Msisdn;

        $c2bTransaction   = STK::send($request->Msisdn, Session::get('payment_data')['amount'], $request->BillRefNumber);
        //$c2bTransaction = $mpesa->c2b(env('MPESA_SHORT_CODE'), $request->CommandID, Session::get('payment_data')['amount'], $request->Msisdn, $request->BillRefNumber);
    } else if(Session::has('payment_type') && Session::get('payment_type') == 'customer_package_payment') {
        $payment_data = Session::get('payment_data');
        $customer_package_id = $payment_data['customer_package_id'];
        $customer_package_price = CustomerPackage::findOrFail($customer_package_id)->amount;

        $c2bTransaction   = STK::send($request->Msisdn, $customer_package_price, $request->BillRefNumber);
        //$c2bTransaction = $mpesa->c2b(env('MPESA_SHORT_CODE'), $request->CommandID, $customer_package_price, $request->Msisdn, $request->BillRefNumber);
    } else if(Session::has('payment_type') && Session::get('payment_type') == 'seller_package_payment') {
        $payment_data = Session::get('payment_data');
        $seller_package_id = $payment_data['seller_package_id'];
        $seller_package_price = \App\SellerPackage::findOrFail($seller_package_id)->amount;

        $c2bTransaction   = STK::send($request->Msisdn, $seller_package_price, $request->BillRefNumber);
        //$c2bTransaction = $mpesa->c2b(env('MPESA_SHORT_CODE'), $request->CommandID, $seller_package_price, $request->Msisdn, $request->BillRefNumber);

    }
    $payment_type = Session::get('payment_type');

    $payment = $c2bTransaction;

    try{

        if($c2bTransaction['ResponseCode'] != 0){
            // fail or cancel or incomplete
            Session::forget('payment_data');
            flash(translate('Payment incomplete'))->error();
            return redirect()->route('home');

        }
        else {
            if ($payment_type == 'cart_payment') {
                $checkoutController = new CheckoutController;
                return $checkoutController->checkout_done(session()->get('combined_order_id'), json_encode($payment));
            }

            if ($payment_type == 'wallet_payment') {
                $walletController = new WalletController;
                return $walletController->wallet_payment_done(session()->get('payment_data'), json_encode($payment));
            }

            if ($payment_type == 'customer_package_payment') {
                $customer_package_controller = new CustomerPackageController;
                return $customer_package_controller->purchase_payment_done(session()->get('payment_data'), json_encode($payment));
            }
            if($payment_type == 'seller_package_payment') {
                $seller_package_controller = new \App\Http\Controllers\SellerPackageController;
                return $seller_package_controller->purchase_payment_done(session()->get('payment_data'), json_encode($payment));
            }
        }
    }
    catch (\Exception $e) {
        flash(translate('Payment failed'))->error();
        return redirect()->route('home');
    }

}

CodePudding user response:

you should uncomment $c2bTransaction staring of if statement

CodePudding user response:

We need more details to help you.

But, as we can see in the code that you posted, the variable c2bTransaction is undefined because the line were you declare that variable is commented, and probably your method is not accessing your if condition, that's why you should declare the variable before the if statement.

  • Related