jaxdevil Posted November 2, 2007 Share Posted November 2, 2007 Below is the code I am using for my payment processing data capture before relaying to the cc gateway. I need to find out how to add 10% to the total in the script (the $sum), the 10% is the 'buyers premium' (this is for a live physical location auction house) and then after adding that 10% I need to add %7 sales tax onto that and send the data to the merchant gateway with the total updated with the 10% and then the 7%. The basic formula is as follows.... Lets assume the total for the customer is $100 to begin with. Here is what needs to be added.. $100 (add the buyers premium of 10%) + 10% = $110 (then add sales tax) + %7 = $117.70 Below is the code I am using now, I appreciate any help.. <?php $db_host = "localhost"; $db_user = "xxx_xxx"; $db_pwd = "xxxxxx"; $db_name = "xxx_xxx"; mysql_connect($db_host, $db_user, $db_pwd); mysql_select_db($db_name); ?> <?php $sql = "SELECT * FROM vendor WHERE id=$bidder"; $query = mysql_query($sql); echo "Bidder Number: " .$_GET[bidder]. "<br>"; while($row = mysql_fetch_array($query)) { echo "Name: " .$row['name']. "<br>"; echo "Address: " .$row['address1']. "<br>"; echo "City: " .$row['city']. "<br>"; echo "State: " .$row['province']. "<br>"; echo "Postal Code: " .$row['p_code']. "<br>"; echo "Country: " .$row['country']. "<br>"; } ?> <?php if ($_GET[method]==cc){ echo "<form method=\"GET\" action=\"cccapture.php\">"; } else if ($_GET[method]==check){ echo "<form method=\"GET\" action=\"checkcapture.php\">"; } else if ($_GET[method]==cash){ echo "<form method=\"GET\" action=\"cashdbupdate.php\">"; } ?> <?php $sql = "SELECT * FROM checkout WHERE bidnum=$bidder AND paid='no'"; $query = mysql_query($sql); $sum = array(); while($row = mysql_fetch_array($query)) { $sum[] = $row['amt']; $di = $row['id']; echo "Lot Number " .$row['lotnum']. ": $" .$row['amt']. "<br>"; } $sum = number_format(array_sum($sum),2); echo "<input type=\"hidden\" name=\"bidder_number\" value=\"$bidder\">"; echo "<input type=\"hidden\" name=\"idx\" value=\"$di\">"; echo "<input type=\"hidden\" name=\"amount\" value=\"$sum\">Total Due: " .$sum; ?> <br> </div> <div style="padding-left:185px;"> <?php if ($_GET[method]==cc){ echo "<input type=\"submit\" value=\"Process CC Payment\" name=\"submit\">"; } else if ($_GET[method]==check){ echo "<input type=\"submit\" value=\"Process Check Payment\" name=\"submit\">"; } else if ($_GET[method]==cash){ echo "<input type=\"submit\" value=\"Process Cash Payment\" name=\"submit\">"; } ?> </form> Quote Link to comment https://forums.phpfreaks.com/topic/75847-solved-add-10-to-a-sum-then-add-7-sales-tax-to-that-sum/ Share on other sites More sharing options...
burtybob Posted November 2, 2007 Share Posted November 2, 2007 you would need to do something like $sum=100 $suma=$sum*1.10; $sumb=$suma*1.07; Which SHOULD if the maths is working give you the $sumb value being 117.70, inside the phptags <?php $sum=100 $suma=$sum*1.10; $sumb=$suma*1.07; // This is the last part and the variable you will want to pass onto the processing form. $sql = "SELECT * FROM vendor WHERE id=$bidder"; $query = mysql_query($sql); echo "Bidder Number: " .$_GET[bidder]. "<br>"; while($row = mysql_fetch_array($query)) { echo "Name: " .$row['name']. "<br>"; echo "Address: " .$row['address1']. "<br>"; echo "City: " .$row['city']. "<br>"; echo "State: " .$row['province']. "<br>"; echo "Postal Code: " .$row['p_code']. "<br>"; echo "Country: " .$row['country']. "<br>"; } ?> <?php if ($_GET[method]==cc){ echo "<form method=\"GET\" action=\"cccapture.php\">"; } else if ($_GET[method]==check){ echo "<form method=\"GET\" action=\"checkcapture.php\">"; } else if ($_GET[method]==cash){ echo "<form method=\"GET\" action=\"cashdbupdate.php\">"; } ?> <?php $sql = "SELECT * FROM checkout WHERE bidnum=$bidder AND paid='no'"; $query = mysql_query($sql); $sum = array(); while($row = mysql_fetch_array($query)) { $sum[] = $row['amt']; $di = $row['id']; echo "Lot Number " .$row['lotnum']. ": $" .$row['amt']. "<br>"; } $sum = number_format(array_sum($sum),2); echo "<input type=\"hidden\" name=\"bidder_number\" value=\"$bidder\">"; echo "<input type=\"hidden\" name=\"idx\" value=\"$di\">"; echo "<input type=\"hidden\" name=\"amount\" value=\"$sum\">Total Due: " .$sum; ?> <br> </div> <div style="padding-left:185px;"> <?php if ($_GET[method]==cc){ echo "<input type=\"submit\" value=\"Process CC Payment\" name=\"submit\">"; } else if ($_GET[method]==check){ echo "<input type=\"submit\" value=\"Process Check Payment\" name=\"submit\">"; } else if ($_GET[method]==cash){ echo "<input type=\"submit\" value=\"Process Cash Payment\" name=\"submit\">"; } ?> </form> Please if anyone sees a problem feel free to correct but as far as i can see that is the quickest and simplest way. Quote Link to comment https://forums.phpfreaks.com/topic/75847-solved-add-10-to-a-sum-then-add-7-sales-tax-to-that-sum/#findComment-383873 Share on other sites More sharing options...
otuatail Posted November 2, 2007 Share Posted November 2, 2007 if you are looping through the table you dont need $sum[] = $row['amt']; $sum += $row['amt']; then Do arithmatic and do a format for the display $sum *= 1.10; $sum *= 1.07; money_format('%i' , $sum); Quote Link to comment https://forums.phpfreaks.com/topic/75847-solved-add-10-to-a-sum-then-add-7-sales-tax-to-that-sum/#findComment-383882 Share on other sites More sharing options...
GingerRobot Posted November 2, 2007 Share Posted November 2, 2007 Well, im assuming you need to perform this multiplication on the $sum variable used here: $sum = number_format(array_sum($sum),2); In which case, this should do: $sum = number_format(array_sum($sum*1.1*1.07),2); Burtybob: There's no need to use so many variables. The multiplication doesn't need to be done separately. Quote Link to comment https://forums.phpfreaks.com/topic/75847-solved-add-10-to-a-sum-then-add-7-sales-tax-to-that-sum/#findComment-383883 Share on other sites More sharing options...
burtybob Posted November 2, 2007 Share Posted November 2, 2007 very true but for reading and debugging sense sometimes it is easier to use an extra variable and see what is happening but i will bear that in mind thank you ginger robot Quote Link to comment https://forums.phpfreaks.com/topic/75847-solved-add-10-to-a-sum-then-add-7-sales-tax-to-that-sum/#findComment-383885 Share on other sites More sharing options...
otuatail Posted November 2, 2007 Share Posted November 2, 2007 that works his way but why use an array when you can simply add up on the fly $sum = number_format($sum*1.1*1.07,2); only one variable needed here. Quote Link to comment https://forums.phpfreaks.com/topic/75847-solved-add-10-to-a-sum-then-add-7-sales-tax-to-that-sum/#findComment-383887 Share on other sites More sharing options...
jaxdevil Posted November 3, 2007 Author Share Posted November 3, 2007 I must be doing something wrong, none of the above codes are working for me. Here is a copy of the php functions on that page... <?php $db_host = "localhost"; $db_user = "xxx_xxx"; $db_pwd = "xxxxxx"; $db_name = "auction_asset"; mysql_connect($db_host, $db_user, $db_pwd); mysql_select_db($db_name); ?> <?php $sql = "SELECT * FROM vendor WHERE id=$bidder"; $query = mysql_query($sql); echo "Bidder Number: " .$_GET[bidder]. "<br>"; while($row = mysql_fetch_array($query)) { echo "Name: " .$row['name']. "<br>"; echo "Address: " .$row['address1']. "<br>"; echo "City: " .$row['city']. "<br>"; echo "State: " .$row['province']. "<br>"; echo "Postal Code: " .$row['p_code']. "<br>"; echo "Country: " .$row['country']. "<br>"; echo "Exempt: "; if ($row['exempt']==1) {echo "Not Exempt";} else if ($row['exempt']==0) {echo "Exempt";} echo "<br>"; } ?> <?php if ($_GET[method]==cc){ echo "<form method=\"GET\" action=\"cccapture.php\">"; } else if ($_GET[method]==check){ echo "<form method=\"GET\" action=\"checkcapture.php\">"; } else if ($_GET[method]==cash){ echo "<form method=\"GET\" action=\"cashdbupdate.php\">"; } ?> <?php $sql = "SELECT * FROM checkout WHERE bidnum=$bidder AND paid='no'"; $query = mysql_query($sql); $sum = array(); while($row = mysql_fetch_array($query)) { $sum[] = $row['amt']; $di = $row['id']; echo "Lot Number " .$row['lotnum']. ": $" .$row['amt']. "<br>"; } $sum = number_format(array_sum($sum*1.1*1.07),2); echo "<input type=\"hidden\" name=\"bidder_number\" value=\"$bidder\">"; echo "<input type=\"hidden\" name=\"idx\" value=\"$di\">"; echo "<input type=\"hidden\" name=\"amount\" value=\"$sum\">Total Bid Amounts: " .$sum; ?> <br> </div> <div style="padding-left:185px;"> <?php if ($_GET[method]==cc){ echo "<input type=\"submit\" value=\"Process CC Payment\" name=\"submit\">"; } else if ($_GET[method]==check){ echo "<input type=\"submit\" value=\"Process Check Payment\" name=\"submit\">"; } else if ($_GET[method]==cash){ echo "<input type=\"submit\" value=\"Process Cash Payment\" name=\"submit\">"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/75847-solved-add-10-to-a-sum-then-add-7-sales-tax-to-that-sum/#findComment-383988 Share on other sites More sharing options...
Barand Posted November 3, 2007 Share Posted November 3, 2007 try $sum = number_format(array_sum($sum)*1.1*1.07,2); Quote Link to comment https://forums.phpfreaks.com/topic/75847-solved-add-10-to-a-sum-then-add-7-sales-tax-to-that-sum/#findComment-383993 Share on other sites More sharing options...
jaxdevil Posted November 3, 2007 Author Share Posted November 3, 2007 Ok, I found something that is the right direction, someone can easily figure this out (I am obviously retarded at this point). the following function WILL calculate the tax amount, the only problem is it has a code in it that does not pull the variable $sum and I have to type the total in, which will not work for what I am trying to do. Anyone know how to make the $sum filed work on that last line that says Tax($sum); ? I am copying both the code that DOES return the correct percentage (first copy and paste below) but the value for $sum has to be manually typed in, and I am copying the second code that DOES NOT calculate the percentage because where it says Tax($sum) the value for $sum is not being put in as it should. Please help function Tax($sum){ $tax=($sum*10)/100; echo "Tax for ".$sum." is :".$tax; return; } Tax(1007); function Tax($sum){ $tax=($sum*10)/100; echo "Tax for ".$sum." is :".$tax; return; } Tax($sum); Quote Link to comment https://forums.phpfreaks.com/topic/75847-solved-add-10-to-a-sum-then-add-7-sales-tax-to-that-sum/#findComment-383994 Share on other sites More sharing options...
jaxdevil Posted November 3, 2007 Author Share Posted November 3, 2007 Thanks Barand! You saved my project!! I greatly appreciate it, the script works correctly now. I do have one more issue with this script... I made an if/elseif statement to only add the buyer premium and not the tax if the customer is listed in our database as being a reseller with a tax exempt certificate on file. The problem is it will not display the tax portion at all now, just the buyer premium (the 10%), which is the final 'elseif' statement that only adds the buyer premium if the database has them set as 'Not Tax Exempt', the begining 'if' statement does not ever display, even on buyers records who are listed in the databse as 'Tax Exempt' here is the code.... <?php $sql = "SELECT * FROM checkout WHERE bidnum=$bidder AND paid='no'"; $query = mysql_query($sql); $sum = array(); $sum2 = array(); $sum3 = array(); while($row = mysql_fetch_array($query)) { $sum[] = $row['amt']; $sum2[] = $row['amt']; $sum3[] = $row['amt']; $di = $row['id']; echo "Lot Number " .$row['lotnum']. ": $" .$row['amt']. "<br>"; } $sum = number_format(array_sum($sum),2); echo "Bids Total: " .$sum; echo "<br>"; if ($row['exempt']==1) {$sum2 = number_format(array_sum($sum2)*1.1,2); echo "Total+Premium: " .$sum2; echo "<br>"; $sum3 = number_format(array_sum($sum3)*1.1*1.07,2); echo "<input type=\"hidden\" name=\"bidder_number\" value=\"$bidder\">"; echo "<input type=\"hidden\" name=\"idx\" value=\"$di\">"; echo "<input type=\"hidden\" name=\"amount\" value=\"$sum3\">Total+Premium+Tax: " .$sum3;} else if ($row['exempt']==0) {$sum2 = number_format(array_sum($sum2)*1.1,2); echo "<input type=\"hidden\" name=\"bidder_number\" value=\"$bidder\">"; echo "<input type=\"hidden\" name=\"idx\" value=\"$di\">"; echo "<input type=\"hidden\" name=\"amount\" value=\"$sum2\">Total+Premium: " .$sum2;} ?> Quote Link to comment https://forums.phpfreaks.com/topic/75847-solved-add-10-to-a-sum-then-add-7-sales-tax-to-that-sum/#findComment-384005 Share on other sites More sharing options...
jaxdevil Posted November 3, 2007 Author Share Posted November 3, 2007 i figured it out guys..here is the code... if($exempt == "1"){ $sum2 = number_format(array_sum($sum2)*1.1,2); echo "Total+Premium: " .$sum2; echo "<br>"; $sum3 = number_format(array_sum($sum3)*1.1*1.07,2); echo "<input type=\"hidden\" name=\"bidder_number\" value=\"$bidder\">"; echo "<input type=\"hidden\" name=\"idx\" value=\"$di\">"; echo "<input type=\"hidden\" name=\"amount\" value=\"$sum3\">Total+Premium+Tax: " .$sum3; } else { {$sum2 = number_format(array_sum($sum2)*1.1,2); echo "<input type=\"hidden\" name=\"bidder_number\" value=\"$bidder\">"; echo "<input type=\"hidden\" name=\"idx\" value=\"$di\">"; echo "<input type=\"hidden\" name=\"amount\" value=\"$sum2\">Total+Premium: " .$sum2;} } ?> Quote Link to comment https://forums.phpfreaks.com/topic/75847-solved-add-10-to-a-sum-then-add-7-sales-tax-to-that-sum/#findComment-384017 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.