<?php 

public function merchantTopupStoreOld(Request $request){
        $this->validate($request, [
            'merchant'=> 'required',
            'amount' => 'required|numeric|min:1'
        ]);
        $org_id = $request->session()->get('current_org_user.org_id');

        $organisation = Models\Organisation::find($org_id);
        $prev_balance = $organisation->balance;
        $merchant = Models\Merchant::findOrFail($request->merchant);
        
        $current_balance = $merchant->balance;

        if ( $request->amount > $organisation->balance ) {
            return response()->json([
                'code' => 3,
                'status' => 'failed',
                'message' => 'Insufficient Balance!'
            ]);
        }
        
        // $discount = 1 + ($request->discount/100);
        // $new_amount = (float)$request->amount * $discount;

        $topup = new Models\MerchantTopUp;
        $topup->amount = $request->amount;
        $topup->merchant_id = $request->merchant;
        $topup->org_id = $org_id;
        $topup->transaction_id = random_int(10000000, 99999999);;
        $topup->org_user_id = $request->session()->get('current_org_user.id');
        $topup->created_by = $request->session()->get('current_org_user.id');
        $topup->payment_method = 'cash';
        $save = $topup->save();

        //Update Merchant Amt
        
        $retval = Models\Merchant::where('id', $request->merchant)->increment('balance', $request->amount);
        $result = Models\Organisation::where('id', $org_id)->decrement('balance', $request->amount);

        if ($save == true && $retval == true && $result) {
            $desc = [
                'title'=>'Org User Added new Top Up',
                'data'=>'MWK'. $request->amount .' for ' . $merchant->fullname,
                'type'=>'org_user topup',
            ];
            $activity = new ActivityLog;
            $activity->user_id = $request->session()->get('current_org_user.id');
            $activity->user_type = 'organisation';
            $activity->description = json_encode($desc);
            $activity->save();
            $smsgateway = new Smsgateway; 
            
            $merchant = Models\Merchant::findOrFail($request->merchant);

            $merchant_phone = $merchant->phone;
            $merchant_name = $merchant->fullname;
            $merchant_new_balance = $merchant->balance;
            $msg = "Hello $merchant_name, your account has been credited with $request->amount Kwacha and your new balance is : $merchant_new_balance";
            if(env('APP_ENV') !== 'local'){
                $message_result = $smsgateway->sendSmsNew($merchant_phone, $msg);
            }
            \Log::info($msg);
            return response()->json([
                'code' => 1,
                'status' => 'success',
                'message' => 'Merchant Top Up Successfully completed!'
            ]);

        } else {
            return response()->json([
                'code' => 3,
                'status' => 'failed',
                'message' => 'Merchant Top Up failed!'
            ]);
        }
    }
     ?>