2

I am integrating Payment Gateway with my PHP application. I am facing an issue during solving some basic small equation via JavaScript or PHP. actually my payment gateway takes some percentages from principal amount as they have own rules.

I want the amount being deducted and add it during payment initiates when user makes payment. For more clarity, below is my example - Suppose user purchases an item for amount $100 at the payment gateway, their payment will be calculated with the formula below:-

var amount = '10352';
var percent = amount * '2.5' / '100';
var charge = percent + parseFloat('5');
var TAX = charge * '18' / '100';
var totalcharge = charge + TAX;
var okAmount = parseFloat(amount) + parseFloat(totalcharge);
//output result is *10663.28* which is correct.

but when I trying to do subtraction, Its not showing the correct answer in PHP.

$amount ="10663.28";
$percent = $amount * '2.5' / '100';
$charge = $percent + '5';
$GST = $charge * '18' / '100';
$totalcharge = $charge + $GST;
$totalAmount = $amount - $totalcharge;
but the answer is not == 10352 even I try my best to apply at the same formula.

Please let me know what the issue is and what i might be doing wrong. What i want to know is How I make 10663.28 to 10352 again in PHP. Thank you so much. Note: This is not my question and it already applied by me

Mr Khan
  • 2,139
  • 1
  • 7
  • 22

2 Answers2

1

I made it work like that:

$amount = 10663.28;
$totalcharge = $amount * 0.0291921325550365;
$totalAmount = round($amount - $totalcharge,2);
//$totalAmount = 10352

It's a pure Mathematics problem and you need to find what's the percentage of "totalcharge" that is being taken on every transactions.

Divide the $totalAmount with the $totalcharge and you get 0.0291921325550365.

With that figure you now know that 2.919213255% is taken from every transactions.

Hence 10663.28 * 0.0291921325550365 = 311.284 which is "totalcharge".

And 10663.28 - 311.284 rounded up equals to 10352.

I would also suggest not to write numbers with apostrophe as these are not strings but integers and add parentheses to separate the different calculations, so your original formula should looks like that:

$amount = 10352;
$percent = ($amount * 2.5) / 100;
$charge = $percent + 5;
$GST = ($charge * 18) / 100;
$totalcharge = $charge + $GST;
$totalAmount = $amount - $totalcharge;
Ange Loron
  • 1,093
  • 1
  • 11
  • 23
  • Answer not come even I use your code but wrong answer - `$amount = '10663.28'; $percent = ($amount * 2.5) / 100; $charge = $percent + 5; $GST = ($charge * 18) / 100; $totalcharge = $charge + $GST; $totalAmount = number_format($amount - $totalcharge, 2); echo $totalAmount; //10,342.81 actual answer shoulb be 10352` – Shubham Singh Mar 16 '20 at 02:47
  • @ShubhamSingh I edited my answer as there was a typo, on the bottom code I am just doing a few edits on your original code. The one you are interested in is the first code that I wrote. – Ange Loron Mar 16 '20 at 07:39
  • Just take that piece of code and then it works: $amount = 10663.28; $totalcharge = $amount * 0.0291921325550365; $totalAmount = round($amount - $totalcharge,2); – Ange Loron Mar 16 '20 at 08:39
0

I think you won't get what you want based on your current parameter values. You need to change the value of any of your parameter, I got the correct result by changing the value of GST;

$('#amount, #percent_val, #charge_val, #gst_val').change(function(){
  CalculatePrice()
});

$('#btnCalc').click(function(){
  CalculatePrice()
});

function CalculatePrice(){
  var $amount = $('#amount').val();
  var $percent = parseFloat($amount) * $('#percent_val').val() / 100;
  var $charge = parseFloat($percent) + $('#charge_val').val();
  var $GST = parseFloat($charge) * $('#gst_val').val() / 100;
  var $totalcharge = parseFloat($charge) + parseFloat($GST);
  var $totalAmount = parseFloat($amount) - parseFloat($totalcharge);

  $('#percent').val(parseFloat($percent).toFixed(2));
  $('#charge').val(parseFloat($charge).toFixed(2));
  $('#gst').val(parseFloat($GST).toFixed(2));
  $('#totalcharge').val(parseFloat($totalcharge).toFixed(2));
  $('#totalamount').val(parseFloat($amount - $totalcharge).toFixed(2));
}

CalculatePrice()
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td>Amount</td>
    <td></td>
    <td><input id="amount" type="number" value="10663.28"></td>
  </tr>
  <tr>
    <td>Percent</td>
    <td><input id="percent_val" type="number" value="2.5"></td>
    <td><input id="percent" readonly type="number"></td>
  </tr>
  <tr>
    <td>Charge</td>
    <td><input id="charge_val" type="number" value="5"></td>
    <td><input id="charge" readonly type="number"></td>
  </tr>
  <tr>
    <td>GST</td>
    <td><input id="gst_val" type="number" value="16.765"></td>
    <td><input id="gst" readonly type="number"></td>
  </tr>
  <tr>
    <td>Total Charge</td>
    <td></td>
    <td><input id="totalcharge" readonly type="number"></td>
  </tr>
  <tr>
    <td>Total Amount</td>
    <td></td>
    <td><input id="totalamount" readonly type="number"></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td><button id="btnCalc">Calculate</button></td>
  </tr>
<table>

Try to play your parameters to get your desired result.

gilbertdim
  • 357
  • 2
  • 11