Advertisement
Guest User

buy-vid

a guest
Oct 21st, 2021
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. <?php
  2. ob_start();
  3.  
  4. if (IS_LOGGED == false) {
  5. $data = array('status' => 400, 'error' => 'Not logged in');
  6. echo json_encode($data);
  7. exit();
  8. }
  9.  
  10. if (!empty($_POST['id'])) {
  11.  
  12. if (!is_array($_POST['id'])) {
  13. $id_array[] = $_POST['id'];
  14. } else {
  15. $id_array = $_POST['id'];
  16. }
  17.  
  18. $db->where('name', 'rent_price');
  19. $db_cost = $db->getOne('config');
  20. $video_cost = (float)$db_cost->value;
  21.  
  22. // the number of submitted videos - used to determine if all records were inserted
  23. $count_video = count($id_array);
  24. $user_id = $user->id;
  25.  
  26. $wallet = (float)str_replace(',', '', $user->wallet);
  27. $balance = (float)str_replace(',', '', $user->balance);
  28.  
  29. // add up the video prices
  30. $amount = 0;
  31. foreach ($id_array as $id) {
  32.  
  33. $video_id = (int)PT_Secure($id);
  34.  
  35. // get video data
  36. $video = $db->where('id', $id)->getOne(T_VIDEOS);
  37.  
  38. // add the video play price if any, or the default price
  39. $amount += $video->rent_price?$video->rent_price:$video_cost;
  40. }
  41.  
  42. // determine if the user has enough credits
  43. if( ($wallet >= $amount) OR ($balance + $wallet >= $amount) ) {
  44.  
  45. $db->startTransaction();
  46.  
  47. $inserted_records = 0;
  48.  
  49. foreach ($id_array as $id){
  50.  
  51. $video_id = (int)PT_Secure($id);
  52.  
  53. // get video data
  54. $video = $db->where('id', $id)->getOne(T_VIDEOS);
  55.  
  56. // use the video play price if any, or the default price
  57. $video_cost_new = $video->rent_price?$video->rent_price:$video_cost;
  58.  
  59. // credit the user 50% of the video cost
  60. $up_amount = $video_cost_new *0.50;
  61.  
  62. // credit the website 50% of the video cost
  63. $site_add_amount = $video_cost_new *0.50;
  64.  
  65. $time_start = microtime(true);
  66.  
  67. // add data to table
  68. $insert_buy = $db->insert('videos_transactions', [
  69. 'user_id' => $video->user_id,//seller
  70. 'paid_id' => $user_id,//buyer
  71. 'video_id' => $video_id,
  72. 'amount' => (string)$video_cost_new,
  73. 'admin_com' => $site_add_amount,//admin commission
  74. 'time' => $time_start,
  75. 'type' => rent,
  76. 'session_key' => $_SESSION['session_key']
  77. ]);
  78.  
  79.  
  80. // count successful inserted records
  81. if ($insert_buy) {
  82. $inserted_records++;
  83. }
  84.  
  85. $up_user_start = $db->where('id', $video->user_id)->getOne(T_USERS);
  86.  
  87.  
  88. $uploader_account = $up_user_start->balance+$up_amount;
  89.  
  90. $db->where('id', $video->user_id);
  91. $update_balance = $db->update(T_USERS, [
  92. 'balance' => $uploader_account,
  93. ]);
  94.  
  95.  
  96. // check if video-buyer is the video-uploader
  97. if ($video->user_id == $user_id) {
  98. $balance = $uploader_account;
  99. }
  100. }
  101.  
  102.  
  103. $money_amount = $up_amount *0.50;
  104.  
  105. $up_user_start = $db->where('id', $video->user_id)->getOne(T_USERS);
  106.  
  107. // add to the money
  108. $uploader_account = $up_user_start->money+$money_amount;
  109.  
  110. // update the record
  111. $db->where('id', $video->user_id);
  112. $update_money = $db->update(T_USERS, [
  113. 'money' => $uploader_account,
  114. ]);
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123. $update_wallet = null;
  124. $update_user_balance = null;
  125.  
  126.  
  127. if($wallet >= $amount){
  128.  
  129. $wallet = (string)($wallet - $amount);
  130. $db->where('id', $user_id);
  131. $update_wallet = $db->update(T_USERS, [
  132. 'wallet' => $wallet
  133. ]);
  134.  
  135. }else if ($wallet + $balance >= $amount) {
  136. // take money first from wallet and/or balance
  137. $balance = (string)($balance - ($amount - $wallet));
  138. $wallet = (string)($wallet - $wallet);
  139. $db->where('id', $user_id);
  140. $update_user_balance = $db->update(T_USERS, [
  141. 'balance' => $balance
  142. ]);
  143. $update_wallet = $db->update(T_USERS, [
  144. 'wallet' => $wallet
  145. ]);
  146. } else {
  147. echo json_encode([
  148. 'status' => 400,
  149. 'error_num' => 1,
  150. 'error' => 'Not enough money'
  151. ]);
  152. exit();
  153. }
  154.  
  155. // if all the video records were inserted and the current user's wallet was updated, commit the changes
  156. if (($inserted_records == $count_video) && ($update_wallet OR $update_user_balance) ) {
  157. $db->commit();
  158.  
  159. echo json_encode([
  160. 'status' => 200
  161. ]);
  162. exit();
  163. } else {
  164. $db->rollback();
  165.  
  166. echo json_encode([
  167. 'status' => 400,
  168. '_data' => [
  169. 'inserted_records' => $inserted_records,
  170. 'count_video' => $count_video,
  171. 'update_wallet' => $update_wallet,
  172. 'update_user_balance' => $update_user_balance
  173. ],
  174. 'error' => 'Buy process error'
  175. ]);
  176. exit();
  177. }
  178.  
  179. } else {
  180.  
  181. echo json_encode([
  182. 'status' => 400,
  183. 'error_num' => 1,
  184. 'error' => 'Not enough money'
  185. ]);
  186. exit();
  187. }
  188.  
  189. } else {
  190.  
  191. echo json_encode([
  192. 'status' => 400,
  193. 'error' => 'Bad Request, Invalid or missing parameter'
  194. ]);
  195. exit();
  196.  
  197. }
  198.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement