I am using a php web video script which allows Users to purchase videos successfully. The purchases are made from the amount available in the Users’ “wallet” (the User can also earn compensation, which gets added to his “wallet2”).
When there’s not enough available in the “wallet” for the purchase, the script checks and displays a message “not enough money”. I’d like help adding the ability where the script first checks the “wallet” amount, and if empty will then check the “wallet2” amount, and use a required amount from the wallet2, and if both are empty, then the “not enough money” appears. Can you please suggest what I might try? Here’s the portion of the code that I believe needs the modification:
// 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;
}
// $amout = $video_cost * $count_video;
$charge = ( $video_cost *0.50 );
if ($wallet >= $amout) {
//$new_wallet = (string)($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,
]);
if ($insert_buy) { $inserted_records++; }
//add wallet users' video
$userwallet = $db->where('id', $video->user_id)->getOne(T_USERS);
$videouserwallet = $userwallet->wallet2+$uploader_amount;
$db->where('id', $video->user_id);
$update_wallet2 = $db->update(T_USERS, [
'wallet2' => number_format($videouserwallet, 1, '.', ''),
]);
}
$db->where('id', $user_id);
$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();
any help will be appreciated