The 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 a video below that (currently an uploader can change to any price - from his logged-in account).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>
And the php code for buying videos is below. I believe adding in a minimum price formula, should be included around line 20? (in Bold)
Any additional help with the formula code will be appreciated.
<?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;
}
$charge = ( $video_cost *0.50 );
if ($wallet >= $amout) {
$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->balance+$uploader_amount;
$db->where('id', $video->user_id);
$update_balance = $db->update(T_USERS, [
// 'wallet' => $videouserwallet,
'balance' => number_format($videouserwallet, 1, '.', ''),
]);
}
$db->where('id', $user_id);
//$update_wallet = $db->update(T_USERS, [
$update_wallet = $db->update(T_USERS, [
'wallet' => $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";
}