#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2008
    Posts
    67
    Rep Power
    21

    Help with deducting earnings ‘balance’ after checking ‘wallet’


    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 earnings “balance”.).

    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 earnings “balance” amount,
    and then use the purchase amount from the earnings "balance",
    and if both are empty, then the “not enough money” appears.

    I don't seem to have the proper code (or logic) yet, because I still see "not enough money" message.
    So, this (after checking wallet) must not be taking 'wallet + balance to make the purchase amount:

    Code:
    		// 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
    			]);
    			// if not enough in wallet add wallet to balance to make purchase
    			} else {
    			if($wallet + $balance >= $amount){
    		    $wallet = (string)($wallet - $amount);
    			$balance = (string)($balance + $wallet);
    			$wallet = '0';
    							$db->where('id', $user_id);
    							$update_user_balance = $db->update(T_USERS, [
    								'balance' => $balance
    								]);
    			}
    			}
    any help will be appreciated.
  2. #2
  3. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Posts
    4,204
    Rep Power
    2012
    Where do you see 'not enough moeny'? You dont have any output in the code.

    The validation should/could be like this:
    PHP Code:
    // Check if user has enough wallet amount to purchase video
    if($wallet >= $amount){
        
    // subtract $amount from $wallet
    } else if($wallet $balance >= $amount){
        
    // subtract $amount from $wallet 
        // subtract reamining from $balance
    } else {
        
    // not enough moeny

    Please notice that you are only updating balance in second if-part. You need to update wallet as well.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2008
    Posts
    67
    Rep Power
    21
    Thanks so much for your reply.
    I’ve added the ability where the script first checks the ‘wallet’ amount, and if empty will then check the earnings ‘balance’, and use amount from the earnings ‘balance’, for the purchase, and if neither has enough, then the “not enough money” message appears.

    However, if the minimum cost of a purchase is ‘2’ and the ‘wallet’ has ‘1’ left over, it will never get used.
    So, I’m trying to code it so, after checking , the ‘wallet’, and finds it doesn’t have enough, it will combine the ‘wallet’ with the earnings ‘balance’, to check if there is enough. If enough > purchase. If not enough show “not enough money”. But this code keeps showing ‘not enough money’ message, even though the ‘wallet’ has ‘4’, and the earnings ‘balance’ has ‘4’, and the purchase amount is ‘6’. I was hoping the code would combine 4 + 4 = 8 and then deduct the 6 (purchase amount).

    Can you please look at my code and tell me what might be incorrect? Or suggest something that will work?


    Code:
    		if($wallet >= $amount){
    
    			$wallet = (string)($wallet - $amount);
    			$db->where('id', $user_id);
    			$update_wallet = $db->update(T_USERS, [
    				'wallet' => $wallet
    			]);
    
    }else{
    
    
    if($balance + $wallet >= $amount){
    
    $wallet = (string)($amount - $wallet);
    //$balance = (string)($balance + $wallet);
    $balance = (string)($balance + $wallet - $amount);
    //$balance = (string)($balance - $amount);
    //$wallet = '0';
    
    $db->where('id', $user_id);
    $update_user_balance = $db->update(T_USERS, [
    'balance' => $balance
    ]);
    }
  6. #4
  7. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Posts
    4,204
    Rep Power
    2012
    Your code in first post should be working:
    PHP Code:
    // Test values
    $amount 9;
    $wallet 4;
    $balance 4;

    if(
    $wallet >= $amount){

      
    $wallet $wallet $amount;
      

    }else if(
    $balance $wallet >= $amount){

        
    $wallet $wallet $amount;
        
    $balance $balance $wallet;
        
    $wallet 0;   
    } else {
      echo 
    "No money"
    }

    // print values 
    echo $amount ;
    echo 
    $wallet ;
    echo 
    $balance 
    But leave out all those "(string)" cast. Amount, Wallet and Balance should all be numeric values.

IMN logo majestic logo threadwatch logo seochat tools logo