Help with checking 'wallet2' after 'wallet'

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 710 Views - Last Post: 07 February 2019 - 04:50 PM Rate Topic: -----

#16 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2241
  • View blog
  • Posts: 6,792
  • Joined: 15-January 14

Re: Help with checking 'wallet2' after 'wallet'

Posted 05 February 2019 - 11:33 AM

If you see the message "Not enough money(test)", then data.status is not 200, and data.error_num is 1. Look at the if statements in the Javascript code. That's what it's checking for.
Was This Post Helpful? 0
  • +
  • -

#17 chrisjchrisj   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 04-February 19

Re: Help with checking 'wallet2' after 'wallet'

Posted 05 February 2019 - 02:07 PM

Thank you for your reply.
You may be right, but I don't know what to look for in the js. The f12 does show status 200.
Any additional help is appreciated
Was This Post Helpful? 0
  • +
  • -

#18 chrisjchrisj   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 04-February 19

Re: Help with checking 'wallet2' after 'wallet'

Posted 05 February 2019 - 04:09 PM

Possibly, $wallet2 needs to be validated on the database. Can you comment on how I can try that?
Was This Post Helpful? 0
  • +
  • -

#19 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2241
  • View blog
  • Posts: 6,792
  • Joined: 15-January 14

Re: Help with checking 'wallet2' after 'wallet'

Posted 05 February 2019 - 04:24 PM

Quote

The f12 does show status 200.

Not the response status, your PHP code is sending back a value called "status" and that is what your Javascript code is checking, that's what data.status is. It's coming from PHP. The error message you showed only gets shown if data.status is not 200, and if data.error_num is 1. So, that's what PHP is sending. It's sending a status that is not 200, and a value for error_num of 1. So, if you want to know why that Javascript is showing that, then look at the PHP code to figure out what status it is sending, and why.

Quote

Possibly, $wallet2 needs to be validated on the database. Can you comment on how I can try that?

I don't know what you mean.
Was This Post Helpful? 0
  • +
  • -

#20 chrisjchrisj   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 04-February 19

Re: Help with checking 'wallet2' after 'wallet'

Posted 06 February 2019 - 11:20 AM

Thank you for your reply. I am sure you are correct. I have looked over the code and can't tell what might be causing that status. Maybe, if you get a chance you can look. this is it:

<?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;

	// the number of submitted videos - used to determine if all records were inserted
	$count_video = count($id_array);

	$wallet = (float)str_replace(',', '', $user->wallet);
	$wallet2 = (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;
	}

		if($wallet + $wallet2 >= $amount) {
		$wallet = (string)($wallet - $amount);
		$wallet2 = (string)($wallet2 - $amount);

		// insert/update all the following queries as a group?
		$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;

			// 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, // the cost at the time of purchase // this is the default video cost not the $video_cost_new
				'video_title' => $video->title, // storing the title
				'user_id_uploaded' => $video->user_id, // the user who uploaded the video
				]);

			// count successful inserted records
			if ($insert_buy) {
				$inserted_records++;
			}

			//update the 'balance' of the user who uploaded the video
			// get the user's record
			$userwallet = $db->where('id', $video->user_id)->getOne(T_USERS);

			$uploader_amount = $video_cost_new *0.50;

			// add to the balance
			$videouserwallet = $userwallet->wallet2+$uploader_amount;
			// update the record
			$db->where('id', $video->user_id);
			$update_balance = $db->update(T_USERS, [
				'wallet2' => number_format($videouserwallet, 1, '.', ''),
			]);
		}


		// update the current user's wallet amount
		$db->where('id', $user_id);
		$update_wallet = $db->update(T_USERS, [
			'wallet' => $wallet,

		]);

		// 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) {
			$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('$amount: '.$amount.PHP_EOL);
	$uploader_amount = $video_play_price - $charge;
	$uploader_amount = $amount - $charge;
	exit;

echo "$uploader_amount";

}










any additional ideas will be appreciated
Was This Post Helpful? 0
  • +
  • -

#21 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2241
  • View blog
  • Posts: 6,792
  • Joined: 15-January 14

Re: Help with checking 'wallet2' after 'wallet'

Posted 06 February 2019 - 12:38 PM

Why can't you tell, can you understand the code? Can you read it? Look:

		echo json_encode([
			'status' => 400,
			'error_num' => 1,
			'error' => 'Not enough money'
		]);

There you go, a status that is not 200, and error_num is 1. That's what Javascript is checking for in the if statements, right? Well, there it is. So, find that part in the PHP code and look at the if/else structure that it is in so that you can see when PHP sends that, and why.

If you don't understand what the code is doing, then set PHP up to use an error log, and then use the error_log function to write whatever information you want to the log, so you can check the log to see what the code is doing. Print out the values of variables that you're using, etc. Get the code to tell you what it's doing and why.
Was This Post Helpful? 0
  • +
  • -

#22 chrisjchrisj   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 04-February 19

Re: Help with checking 'wallet2' after 'wallet'

Posted 07 February 2019 - 02:54 PM

Thanks for your reply and help. Much appreciated.
This seems to work successfully, where a purchase uses the amount in ‘wallet’ first, and then uses the amount in ‘earnings’ if there is not enough in ‘wallet’:

// Check if user has enough wallet amount to purchase video
		if($wallet >= $amount){

			$wallet = (string)($wallet - $amount);
			$db->where('id', $user_id);
			$update_wallet = $db->update(T_USERS, [
				'wallet' => $wallet
			]);
		}else{

			// Check if user has enough earnings amount to purchase video
			if($earnings >= $amount){
				$earnings = (string)($earnings - $amount);
	
				$db->where('id', $user_id);
				$update_user_balance = $db->update(T_USERS, [
					'earnings' => $earnings
				]);
			}
		}



however, if all videos cost 2 (or higher), and there is “1” left in the ‘wallet’, it will never get used. I am looking to see how I can make more like if ‘wallet’ is zero then deduct from ‘earnings’ (or wallet + earnings = amount)…

Any ideas/suggestions will be appreciated.
Was This Post Helpful? 0
  • +
  • -

#23 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2241
  • View blog
  • Posts: 6,792
  • Joined: 15-January 14

Re: Help with checking 'wallet2' after 'wallet'

Posted 07 February 2019 - 03:06 PM

Just write out the logic you want, in English. That description should have a lot of "ifs" in it. Then write the code to represent the same logic.

The first step is figuring out exactly what you want your code to do.
Was This Post Helpful? 0
  • +
  • -

#24 chrisjchrisj   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 04-February 19

Re: Help with checking 'wallet2' after 'wallet'

Posted 07 February 2019 - 04:14 PM

Thanks for your message/advice.
I tried this revision without success.
(wallet2 has been renamed to 'earnings' - from the earlier posting)

	// Check if user has enough wallet amount to purchase video
		if($wallet >= $amount){

			$wallet = (string)($wallet - $amount);
			$db->where('id', $user_id);
			$update_wallet = $db->update(T_USERS, [
				'wallet' => $wallet
			]);
		}else{

			// Check if user has enough earnings amount to purchase video
			if( ($wallet <=1) and ($balance >= $amount) ) {
			//if($balance >= $amount){
				$earnings = (string)($earnings + wallet - $amount);

				$db->where('id', $user_id);
				$update_user_balance = $db->update(T_USERS, [
					'earnings' => $earnings
				]);
			}
		}




Any additional guidance is appreciated
Was This Post Helpful? 0
  • +
  • -

#25 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2241
  • View blog
  • Posts: 6,792
  • Joined: 15-January 14

Re: Help with checking 'wallet2' after 'wallet'

Posted 07 February 2019 - 04:50 PM

My guidance is really general, and I'd still say the same thing. If it doesn't do what you want it to do, then write out, again, in English, a description of exactly how you want it to work. That's still the first step.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2