1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help with checking 'wallet' balance or other balance for purchase

Discussion in 'PHP' started by chrisj, Feb 4, 2019.

  1. #1
    Currently, with the php web script that I’m using, this code works successfully (where the ‘wallet’ amount is used to buy videos) here's a portion of the file buy-video.php code:

    // 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; // copy the user id for some reason
    
        $wallet = (float)str_replace(',', '', $user->wallet);
    
        $amount = 0;
        foreach ($id_array as $id) {
    
            $video_id = (int)PT_Secure($id);
    
            // get video data
            $video = $db->where('id', $id)->getOne(T_VIDEOS);
    
            $amount += $video->video_play_price?$video->video_play_price:$video_cost;
        }
    
        if ($wallet >= $amount) {
    
            $wallet = (string)($wallet - $amount);
    
            $db->startTransaction();
    PHP:
    When videos are purchased the uploader of the video gets some earnings. I’m trying to modify it so that when a Users' ‘wallet’ amount isn’t enough to make the purchase, the ‘earnings’ of the User (if any) then can be used for the purchase. So, I tried this without success:

    
    if ($wallet >= $amount) {    
    $wallet = (string)($wallet - $amount);     }
    elseif ($wallet + $earnings >= $amount) {    
    $earnings = (string)($earnings - $amount);    
    $db->startTransaction();
    
    PHP:
    By ‘without success’, I mean that the same message appears as if just the ‘wallet’ doesn’t have enough “Not Enough Money” - when I test - by having 3 in ‘wallet’ and 3 in earnings (equals a total of 6) but try to purchase something that costs 5.

    Any ideas suggestion will be appreciated
     
    chrisj, Feb 4, 2019 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    A few issues I immediately see. You're casting your wallet to a string when it is numerical so should be (float) and not (string).

    I would dump your variable before you are comparing the wallet balance to the amount to see what's not working correct.

    //Debug these
    var_dump($wallet);
    var_dump($amount);
    if ($wallet >= $amount) {
    
    $wallet = (float)($wallet - $amount);
    Code (markup):
    Edit, actually, I think you need to check for earnings first, and then check if the wallet and earnings is greater than the amount. The logic you're using in this is going to produce erratic results.
     
    jestep, Feb 5, 2019 IP
  3. chrisj

    chrisj Well-Known Member

    Messages:
    606
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    I tried your suggestion with no results I could see.
    Currently, I have this working to some degree:

            if($wallet + $earnings >= $amount) {
            $wallet = (string)($wallet - $amount);
            $wallet = 0;
            $earnings = (string)($earnings - $amount);
    
            $db->startTransaction();
    PHP:
    Presently, this proceeds as a ‘successful’ transaction, but brings ‘wallet’ to 0, and ‘earnings’ is deducted from. Any curly bracket added causes a message from the js file “Something went wrong. Please try again later!!”, as does adding any “else”, any additional “if”, or “elseif”, Here's part of the js code:

    function PT_MultipleBuyVideo() {
        var checked = getSelectedVideos();
        if (!checked) { return false; }
    
        swal({
            title: "",
            type: "info",
            html:"Simply proceed to purchase " + countSelectedVideos() + " video(s) at a total cost of " + countTotalCredits() +" credits",
            showCancelButton: true,
            cancelButtonText: "Close",
            customClass: 'sweetalert-lg',
            confirmButtonText:'Proceed'
        }).then(function(){
    
            $.ajax({
                url: PT_Ajax_Requests_File() + 'aj/buy-video',
                type: 'POST',
                dataType: 'json',
                data: {id:checked},
            }).done(function(data){
                if (data.status == 200) {
                    for (var i = 0; i < checked.length; i++) {
                        var button = $("button[data-action='multiple_select_button'][data-id='" + checked[i] + "']")
                        buttonMultipleSelectingStyle(button, 'purchased');
                    }
    
                    swal({
                        title: "Success",
                        type: "success",
                        html:"",
                        showCancelButton: true,
                        cancelButtonText: "Close",
                        customClass: 'sweetalert-lg',
                        confirmButtonText:'Go To Video(s)'
                    }).then(function(){
                        window.location.href='/paid-list';
                    });
    
                } else {
                    if (data.error_num == 1) {
                        swal(
                            'Error!',
                            'Not enough money(test)',
                            'error'
                        );
                    } else {
                        swal(
                            'Error!',
                            'Something went wrong. Please try again later!',
                            'error'
                        );
                    }
                }
            }).fail(function() {
                swal(
                    'Error!',
                    'Something went wrong. Please try again later!!',
                    'error'
                );
            })
        });
    }
    Code (JavaScript):
    Any additional suggestions/ideas will be welcomed.
     
    Last edited: Feb 5, 2019
    chrisj, Feb 5, 2019 IP
  4. chrisj

    chrisj Well-Known Member

    Messages:
    606
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #4
    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’


    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
                    ]);
                }
            }
    Code (markup):

    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)…

    I have tried this revision without success:

    
    if($wallet + earnings >= $amount){
    
                $wallet = (string)($wallet - $amount);
                $db->where('id', $user_id);
                $update_wallet = $db->update(T_USERS, [
                    'wallet' => $wallet
                ]);
            }else{
    
                // Check if user has enough balance amount to purchase video
                if( ($wallet <=1) and ($balance >= $amount) ) {
                //if($balance >= $amount){
                    $balance = (string)($balance+ wallet - $amount);
    
                    $db->where('id', $user_id);
                    $update_user_balance = $db->update(T_USERS, [
                        'balance' => $balance
                    ]);
                }
            }
    
    Code (markup):
    any additional help is appreciated.
     
    chrisj, Feb 7, 2019 IP