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