Some kind of math problem

Discussion in 'PHP' started by Jeremy Benson, Feb 9, 2021.

  1. #1
    I've got this script that releases and "invoice" which is really a payment. The user receiving the payment gets the amount -7%. 30% of what's left goes into a reserve account. The other 70% is split between myself and a partner. I have a problem in the code. I'm the one sending and receiving the invoice. $200.

    My account history table shows all the right data.

    $186 received for the $200 invoice. $4.90 received as the split of the 70%. My partner gets $4.90 and the reserve account gets $4.20 (fraud protection, ect.) The problem is in my actual account balance.

    Jeremy: $204.90
    Partner: $4.90
    Reserve: $4.20

    My account is up and extra $14, which is strangely the amount of the entire 7%. But I don't know if it's related. I don't know if it's math, php, or mysql.

    // trying instant release
                        // status must be open or release hold
                        if($invoice['status'] == 'open' || $invoice['status'] == 'release hold')
                        {
                         
                            // release the invoice
                              $sqlAccount = $db->prepare('UPDATE `'.$cashAccountsTable.'` SET `balance` = `balance` + ? WHERE `userToken` = ?');
                              $sqlAccount->execute(array($invoice['amount'], $invoice['invoiceTo']));
                           
                              $token = bin2hex(openssl_random_pseudo_bytes(12));
                                $tokenInserted = false;
                                                
                            while(!$tokenInserted)
                            {
                                                    
                               $stmToken = $db->prepare('SELECT `token` FROM `'.$accountHistoryTable.'` WHERE `token` = ?');
                               $stmToken->execute(array($token));
                               $retrievedToken = $stmToken->fetchColumn();
                                            
                               if(is_bool($retrievedToken))
                               {
                                
                                   $releaseAmount = 0;
                                   $companyFee = $invoice['amount'] * 0.07;
                                   $releaseAmount = round($invoice['amount'] - $companyFee, 2);
                                   $reserveAmount = round($companyFee * 0.3, 2);
                                   $companyEarnings = round($companyFee - $reserveAmount, 2);
                                   $jeremyEarning = round($companyEarnings / 2, 2);
                                   $partnerEarning = $jeremyEarning;
                                
                                    // update account history
                                      $sqlHistory = $db->prepare('INSERT INTO `'.$accountHistoryTable.'`(`userToken`, `transactionType`,
                                      `transactionAmount`, `date`, `token`) VALUES (?, ?, ?, ?, ?)');
                                      $sqlHistory->execute(array($invoice['invoiceTo'],
                                      'invoice earned',
                                      $releaseAmount,
                                      date('Y-m-d H:i:s'),
                                      $token));
                                   
                                      // update the invoice status to released
                                   
                                      $sqlUpdateInvoice = $db->prepare('UPDATE `'.$invoicesTable.'` SET `status` = ? WHERE `token` = ?');
                                      $sqlUpdateInvoice->execute(array('released', $invoice['token']));
                                   
                                      // insert jeremy earning
                                   
                                       $sqlAccount = $db->prepare('UPDATE `'.$cashAccountsTable.'` SET `balance` = `balance` + ? WHERE `userToken` = ?');
                                       $sqlAccount->execute(array($jeremyEarning, $jeremyUserToken));
                                   
                                            // update transaction history
                                   
                                         $token = bin2hex(openssl_random_pseudo_bytes(12));
                                            $tokenInserted = false;
                                                            
                                        while(!$tokenInserted)
                                        {
                                                                
                                           $stmToken = $db->prepare('SELECT `token` FROM `'.$accountHistoryTable.'` WHERE `token` = ?');
                                           $stmToken->execute(array($token));
                                           $retrievedToken = $stmToken->fetchColumn();
                                                        
                                           if(is_bool($retrievedToken))
                                           {
                                            
                                                $sqlHistory = $db->prepare('INSERT INTO `'.$accountHistoryTable.'`(`userToken`, `transactionType`,
                                                  `transactionAmount`, `date`, `token`) VALUES (?, ?, ?, ?, ?)');
                                                  $sqlHistory->execute(array($jeremyUserToken,
                                                  'invoice comission',
                                                  $jeremyEarning,
                                                  date('Y-m-d H:i:s'),
                                                  $token));
                                             
                                               $tokenInserted = true;
                                           }else{
                                            
                                               $token = bin2hex(openssl_random_pseudo_bytes(12));
                                            
                                           }
                                        
                                        
                                        }
                                   
                                      // insert partner earning
                                   
                                        $sqlAccount = $db->prepare('UPDATE `'.$cashAccountsTable.'` SET `balance` = `balance` + ? WHERE `userToken` = ?');
                                        $sqlAccount->execute(array($partnerEarning, $partnerUserToken));
                                     
                                            // update transaction history
                                         
                                            $token = bin2hex(openssl_random_pseudo_bytes(12));
                                            $tokenInserted = false;
                                                            
                                        while(!$tokenInserted)
                                        {
                                                                
                                           $stmToken = $db->prepare('SELECT `token` FROM `'.$accountHistoryTable.'` WHERE `token` = ?');
                                           $stmToken->execute(array($token));
                                           $retrievedToken = $stmToken->fetchColumn();
                                                        
                                           if(is_bool($retrievedToken))
                                           {
                                            
                                                $sqlHistory = $db->prepare('INSERT INTO `'.$accountHistoryTable.'`(`userToken`, `transactionType`,
                                                  `transactionAmount`, `date`, `token`) VALUES (?, ?, ?, ?, ?)');
                                                  $sqlHistory->execute(array($partnerUserToken,
                                                  'invoice comission',
                                                  $partnerEarning,
                                                  date('Y-m-d H:i:s'),
                                                  $token));
                                   
                                               $tokenInserted = true;
                                           }else{
                                            
                                               $token = bin2hex(openssl_random_pseudo_bytes(12));
                                            
                                           }
                                        
                                        
                                        }
                                       
                                      // insert reserve amount earning
                                   
                                          $sqlAccount = $db->prepare('UPDATE `'.$cashAccountsTable.'` SET `balance` = `balance` + ? WHERE `userToken` = ?');
                                          $sqlAccount->execute(array($reserveAmount, $companyReserveToken));
                                       
                                          // update transaction history
                                         
                                            $token = bin2hex(openssl_random_pseudo_bytes(12));
                                            $tokenInserted = false;
                                                            
                                        while(!$tokenInserted)
                                        {
                                                                
                                           $stmToken = $db->prepare('SELECT `token` FROM `'.$accountHistoryTable.'` WHERE `token` = ?');
                                           $stmToken->execute(array($token));
                                           $retrievedToken = $stmToken->fetchColumn();
                                                        
                                           if(is_bool($retrievedToken))
                                           {
                                            
                                                $sqlHistory = $db->prepare('INSERT INTO `'.$accountHistoryTable.'`(`userToken`, `transactionType`,
                                                  `transactionAmount`, `date`, `token`) VALUES (?, ?, ?, ?, ?)');
                                                  $sqlHistory->execute(array($companyReserveToken,
                                                  'invoice comission',
                                                  $reserveAmount,
                                                  date('Y-m-d H:i:s'),
                                                  $token));
                                   
                                               $tokenInserted = true;
                                           }else{
                                            
                                               $token = bin2hex(openssl_random_pseudo_bytes(12));
                                            
                                           }
                                        
                                        
                                        }
                                   
                                      $tokenInserted = true;
                                   
                               }else{
                                
                                   $token = bin2hex(openssl_random_pseudo_bytes(12));
                                
                               }
                            
                            }
                         
                         
                        }else{
                         
                            switch($invoice['status'])
                            {
                             
                                case 'disabled':
                                    header('Location: ../secure/dashboard-manage-tasks.php?error='.urlencode('this invoice is admin disabled').'');
                                    exit;
                                break;
                                case 'released':
                                    header('Location: ../secure/dashboard-manage-tasks.php');
                                    exit;
                                break;
                             
                            }
                         
                         
                        }
    
    PHP:
     
    Last edited: Feb 10, 2021
    Jeremy Benson, Feb 9, 2021 IP
  2. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #2
    I solved this, sorry. Dumb mistake. All day and week code stuff. It's actually the dumbest mistake. And I even looked for this multiple times.

    $sqlAccount->execute(array($invoice['amount'], $invoice['invoiceTo']));
    
    Code (markup):
    I actually updated my account the entire invoice. How I did not see this while looking for it I don't know.
     
    Jeremy Benson, Feb 10, 2021 IP
  3. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,278
    Likes Received:
    1,696
    Best Answers:
    31
    Trophy Points:
    475
    #3
    We've all been there. Glad you made it work.
     
    qwikad.com, Feb 10, 2021 IP