Help inserting Amount into Balance column

This php web video script that I’m using calculates the cost of the video, and then upon a purchase inserts the amount of the price into the video/owner/users’ ‘wallet’ account (the column in the users table titled ‘wallet"). I believe this file code (below) performs that function. I’d like help with modifying it so that instead of the purchase amount going into the video/owner/users’ ‘wallet’, the price is inserted into the video/owner/users’ ‘balance’ account (the column in the users table titled ‘balance’). I believe that function starts at around line 60 (marked below). I look forward to your suggestions/assistance.

<?php
ob_start();
if (IS_LOGGED == false) {
    $data = array('status' => 400, 'error' => 'Not logged in');
    echo json_encode($data);
    exit();
}

if (!empty($_POST['id'])) {

    if (!is_array($_POST['id'])) {
        $id_array[] = $_POST['id'];
    } else {
        $id_array = $_POST['id'];
    }

    // get cost video
    $db->where('name', 'video_play_price');
    $db_cost = $db->getOne('config');
    $video_cost = (float)$db_cost->value;

    $count_video = count($id_array);
    $user_id = $user->id;
    $wallet = (float)str_replace(',', '', $user->wallet);


	$amout = 0;
	foreach ($id_array as $id) {
            $video_id = (int)PT_Secure($id);

            // get video data
            $video = $db->where('id', $id)->getOne(T_VIDEOS);
			$amout += $video->video_play_price?$video->video_play_price:$video_cost;
	}

//   $amout = $video_cost * $count_video;

    $charge = ( $video_cost *0.50 );


    if ($wallet >= $amout) {

        $new_wallet = (string)($wallet - $amout);

        $db->startTransaction();

        $inserted_records = 0;
        foreach ($id_array as $id) {
            $video_id = (int)PT_Secure($id);


		// $uploader_amount = $video_cost - $charge; //100 - 20% = 80

            // get video data
            $video = $db->where('id', $id)->getOne(T_VIDEOS);


			
			**$video_cost_new = $video->video_play_price?$video->video_play_price:$video_cost;**
	
			$uploader_amount = ( $video_cost_new *0.50 );
            // add data to paid table
            $insert_buy = $db->insert('u_paid_videos', [
                'id_user' => $user_id,
                'id_video' => $video_id,
                'session_key' => $_SESSION['session_key'],
                'video_play_price' => (string)$video_cost,
                'video_title' => $video->title,
                'user_id_uploaded' => $video->user_id,
                //'up_credit'=>$video_cost,
                'up_credit'=>$uploader_amount,
            ]);

            if ($insert_buy) { $inserted_records++; }
            //add wallet users' video
        $userwallet = $db->where('id', $video->user_id)->getOne(T_USERS);

        //$videouserwallet = $userwallet->wallet+$video_cost;
        $videouserwallet = $userwallet->wallet+$uploader_amount;
        $db->where('id', $video->user_id);
        $update_wallet = $db->update(T_USERS, [
          // 'wallet' => $videouserwallet,
            'wallet' => number_format($videouserwallet, 2, '.', ''),
        ]);
        }

        $db->where('id', $user_id);
        $update_wallet = $db->update(T_USERS, [
            'wallet' => $new_wallet,
        ]);


        if (($inserted_records == $count_video) && $update_wallet) {
            $db->commit();

            echo json_encode([
                'status' => 200
            ]);
            exit();
        } else {
            $db->rollback();

            echo json_encode([
                'status' => 400,
                'error' => 'Buy process error'
            ]);
            exit();
        }

    } else {

        echo json_encode([
            'status' => 400,
            'error_num' => 1,
            'error' => 'Not enough money'
        ]);
        exit();

    }

} else {

    echo json_encode([
        'status' => 400,
        'error' => 'Bad Request, Invalid or missing parameter'
    ]);
    exit();

echo('$video_play_price: '.$video_play_price.PHP_EOL);
echo('$charge: '.$charge.PHP_EOL);
echo('$amout: '.$amout.PHP_EOL);
$uploader_amount = $video_play_price - $charge;
$uploader_amount = $amout - $charge;
exit;


echo "$uploader_amount";
}

Immediate thought: Have you tried find-replacing the word ‘wallet’ with the word ‘balance’?

Thanks for your reply, any suggestion is appreciated.
The function I’m trying to accomplish is to have the money coming out of a Users (purchaser) ‘wallet’ and going into a video/owner (seller) ‘balance’.
As a test, I did replace all words “wallet” with the word “balance” and I received an error stating ‘not enough money" to purchase. The test purchaser has a ‘balance’ of zero. So, when I replaced wallet with balance in the code, that tells me that somewhere in there the code is now trying to (incorrectly) take money out of the purchasers’ ‘balance’. So, since I didn’t write this code, and am not clear on what line does what, any additional help with putting money into the sellers’ balance (and out of the purchasers’ ‘wallet’) will be appreciated.appreciated.

Ah see, now we’re getting more specificity.

Well, based on that, and looking through your code, that line would be here:

        $videouserwallet = $userwallet->wallet+$uploader_amount;
        $db->where('id', $video->user_id);
        $update_wallet = $db->update(T_USERS, [
          // 'wallet' => $videouserwallet,
            'wallet' => number_format($videouserwallet, 2, '.', ''),
        ]);
        }

pluck the word wallet out of that section and make it ‘balance’. Specifically, this bit:

and this bit:

are the important ones.

Thanks so much for your kind reply. Your specific help worked successfully. Much appreciated.
As I said initially, this php web video script that I’m using calculates the cost of the video. Any uploaded video is priced at what the Admin has set in admin panel, but after the upload the uploader can change the price. All that works successfully. However, I’d like to add that the price set by Admin is the minimum price, so uploader can’t price it below that (currently an uploader can change to any price - from his logged-in account). I don’t know if the file code in my initial posting is where to add that function. And I don’t if I should start a new thread for this help request. The admin-panel code pertaining to that is this:

<div class="form-line">
<input type="text" name="video_play_price" class="form-control" value="{{CONFIG video_play_price}}">
<label class="form-label">Cost Video</label>
</div>

I look forward to any additional guidance. Thanks again

Well I would use type=“number” instead of type=“text”, for starters (do note the ‘step’ and ‘min’ attributes there).

The purchase is the right time to add the validation of that value - basically here:

take the maximum of the value provided from the database and the user’s input to use as the price.

Much thanks again for your help/reply.
However, I’m not clear on what you’re referring to when you say “do note the ‘step’ and ‘min’ attributes there”.

Thanks also for explaining where a minimum price can be included, but I don’t know how to code “take the maximum of the value provided from the database and the user’s input to use as the price”. Any additional help will be welcomed. Thanks again

max


You already have this in your code:


You have an example of taking the input from the form already in your code also. It starts on this line:

Thank you again for your help.
As I didn’t write this file code, I’m not sure how to take what you’ve kindly pointed out and include some new the code in there to say ‘a price can’t be below the Admin’s set price’.
Any further guidance will be appreciated.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.