-2

I am having a scenario where I get response from payment gateway in which I have total. Like 1250.00. Now I cross check this total with my cart value which I get from database. Issue is some time

if($cartTotal == $paymentTotal)

fails most of the time it works but for certain times it fail. I am not sure whats the Issue.

I have tried trim, type casting, === strcmp but somehow it fails for certain transactions. I have type casted the values to int, float, string.

Can anybody point me in right direction ? Any help is much appreciated.

SAM
  • 611
  • 2
  • 16
  • 30

2 Answers2

0

You can compare the float variables like this:

if(abs($cartTotal-$paymentTotal) > 0.00001)
Samuil Banti
  • 1,527
  • 12
  • 23
-1

Commas are an example of things that can mess up PHP's number handling

Try this function to cleanse your numbers:

/**
 * Clean up any numbers
 * Remove any characters apart from numbers and point (.)
 * Then explicitly cast to a float
 * @param $amount
 * @return float
 */
function numberSanitise($amount)
{
    $amount = floatval(preg_replace('%[^0-9.]%', '', $amount));
    return $amount;
}
edmondscommerce
  • 1,843
  • 11
  • 20