Help with adding deduction from user account

Currently, the php web script that I’m using, where a user can purchase a video, works successfully. I’m trying to modify the script so that if the price is zero, that will mean that the video uploader will pay for the purchase, which is 1 credit. So, I’d like help adding a line where if price is zero then uploader account gets a deduction of 1.

Here’s the current code:

<?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
	// get the default video price, to use if there is no per video play price
	$db->where('name', 'video_play_price');
	$db_cost = $db->getOne('config');
	$video_cost = (float)$db_cost->value;

	// the number of submitted videos - used to determine if all records were inserted
	$count_video = count($id_array);
	$user_id = $user->id;

	$wallet = (float)str_replace(',', '', $user->wallet);
	$balance = (float)str_replace(',', '', $user->balance);

	// add up the video prices
	$amount = 0;
	foreach ($id_array as $id) {

		$video_id = (int)PT_Secure($id);

		// get video data
		$video = $db->where('id', $id)->getOne(T_VIDEOS);
		// add the video play price if any, or the default price
		$amount += $video->video_play_price?$video->video_play_price:$video_cost;
	}

	// determine if the user has enough credits
	if( ($wallet >= $amount) OR ($balance + $wallet >= $amount) ) {

		$db->startTransaction();

		$inserted_records = 0;

		foreach ($id_array as $id){

			$video_id = (int)PT_Secure($id);

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

			// use the video play price if any, or the default price
			$video_cost_new = $video->video_play_price?$video->video_play_price:$video_cost;

			// credit the user 50% of the video cost
			$up_amount = $video_cost_new *0.50;

			// credit the website 50% of the video cost
			$site_add_amount = $video_cost_new *0.50;

			$time_start = microtime(true);

		// add data to paid table
		$insert_buy = $db->insert('paid_videos', [
		'id_user' => $user_id,
		'video_play_price' => (string)$video_cost_new,
		'id_video' => $video_id,
		'user_id_uploaded' => $video->user_id, // the user who uploaded the video
		'video_title' => $video->title, // storing the title
		'earned_amount' => $up_amount,
		'time' => $time_start,
		'short_id' => $video->short_id,
		'session_key' => $_SESSION['session_key']
		]);


			// count successful inserted records
			if ($insert_buy) {
				$inserted_records++;
			}

			//update the 'balance' of the user who uploaded the video
			// get the user's record
			$up_user_start = $db->where('id', $video->user_id)->getOne(T_USERS);

			// add to the balance
			$uploader_account = $up_user_start->balance+$up_amount;

			// update the record
			$db->where('id', $video->user_id);
			$update_balance = $db->update(T_USERS, [
			'balance' => number_format($uploader_account, 1, '.', ''),
			]);

			// check if video-buyer is the video-uploader
			if ($video->user_id == $user_id) {
			$balance = $uploader_account;
			}
			}

etc.

I look forward to any assistance

This condition makes no real-world sense. Someone could keep going though and purchasing a bunch of zero price videos and drain the accounts of the video up-loaders.

If you do something like this, credits would disappear from accounts without any indication of why. This is one of the reasons you have been told a number of times that the recording of credit/money amounts must be done by inserting a new row in an accounting table for each transaction that affects the amount. Someone can then look at the list of transactions for their account and see all the amounts that have been added or subtracted, when it occurred, and why it occurred. Short-version: If you are doing this for real, the users to a site that cannot show them where their credits/money has gone to, will abandon the site as being untrustworthy.

Whatever you decide you want your code to do, for each video that is purchased, you must calculate the amounts that will get added or subtracted from the current user’s account, the uploader’s account, and the system’s account, then simply execute the queries needed to record these amounts.

This section of code

// add data to paid table
		$insert_buy = $db->insert('paid_videos', [
		'id_user' => $user_id,
		'video_play_price' => (string)$video_cost_new,
		'id_video' => $video_id,
		'user_id_uploaded' => $video->user_id, // the user who uploaded the video
		'video_title' => $video->title, // storing the title
		'earned_amount' => $up_amount,
		'time' => $time_start,
		'short_id' => $video->short_id,
		'session_key' => $_SESSION['session_key']
		]);

appears to be the part that is updating a transactions table.

I think my question would be how does the “1 credit” that the uploader is to be charged relate to the “balance” - is the balance in credits, or actual monetary amounts?

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