Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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'];
- }
- $db->where('name', 'rent_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->rent_price?$video->rent_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->rent_price?$video->rent_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 table
- $insert_buy = $db->insert('videos_transactions', [
- 'user_id' => $video->user_id,//seller
- 'paid_id' => $user_id,//buyer
- 'video_id' => $video_id,
- 'amount' => (string)$video_cost_new,
- 'admin_com' => $site_add_amount,//admin commission
- 'time' => $time_start,
- 'type' => rent,
- 'session_key' => $_SESSION['session_key']
- ]);
- // count successful inserted records
- if ($insert_buy) {
- $inserted_records++;
- }
- $up_user_start = $db->where('id', $video->user_id)->getOne(T_USERS);
- $uploader_account = $up_user_start->balance+$up_amount;
- $db->where('id', $video->user_id);
- $update_balance = $db->update(T_USERS, [
- 'balance' => $uploader_account,
- ]);
- // check if video-buyer is the video-uploader
- if ($video->user_id == $user_id) {
- $balance = $uploader_account;
- }
- }
- $money_amount = $up_amount *0.50;
- $up_user_start = $db->where('id', $video->user_id)->getOne(T_USERS);
- // add to the money
- $uploader_account = $up_user_start->money+$money_amount;
- // update the record
- $db->where('id', $video->user_id);
- $update_money = $db->update(T_USERS, [
- 'money' => $uploader_account,
- ]);
- $update_wallet = null;
- $update_user_balance = null;
- if($wallet >= $amount){
- $wallet = (string)($wallet - $amount);
- $db->where('id', $user_id);
- $update_wallet = $db->update(T_USERS, [
- 'wallet' => $wallet
- ]);
- }else if ($wallet + $balance >= $amount) {
- // take money first from wallet and/or balance
- $balance = (string)($balance - ($amount - $wallet));
- $wallet = (string)($wallet - $wallet);
- $db->where('id', $user_id);
- $update_user_balance = $db->update(T_USERS, [
- 'balance' => $balance
- ]);
- $update_wallet = $db->update(T_USERS, [
- 'wallet' => $wallet
- ]);
- } else {
- echo json_encode([
- 'status' => 400,
- 'error_num' => 1,
- 'error' => 'Not enough money'
- ]);
- exit();
- }
- // if all the video records were inserted and the current user's wallet was updated, commit the changes
- if (($inserted_records == $count_video) && ($update_wallet OR $update_user_balance) ) {
- $db->commit();
- echo json_encode([
- 'status' => 200
- ]);
- exit();
- } else {
- $db->rollback();
- echo json_encode([
- 'status' => 400,
- '_data' => [
- 'inserted_records' => $inserted_records,
- 'count_video' => $count_video,
- 'update_wallet' => $update_wallet,
- 'update_user_balance' => $update_user_balance
- ],
- '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();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement