Skip to content

Commit

Permalink
Issue #16
Browse files Browse the repository at this point in the history
  • Loading branch information
judgej committed Dec 8, 2015
1 parent c0fecf1 commit 2af0794
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/Academe/SagePay/Helper/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static function checkCurrency($currency)
public static function formatAmount($amount, $currency)
{
// We need a numeric value for the amount, so make sure it is, even if it is a number in a string.
if ( ! is_numeric($amount)) $amount = 0;
//if ( ! is_numeric($amount)) $amount = 0;

// Get the minor unit of the currency - the number of digits after the decimal point.
// SagePay requires the amount to be padded out to the exact number of decimal places,
Expand All @@ -48,7 +48,21 @@ public static function formatAmount($amount, $currency)
// If the minor unit is null, then the currency code is not valid.
$minor_unit = \Academe\SagePay\Metadata\Iso4217::minorUnit($currency);

return (isset($minor_unit) ? number_format($amount, $minor_unit, '.', '') : null);
if ( ! isset($minor_unit)) {
throw new Exception\InvalidArgumentException("Invalid currency code '{$currency}'");
}

// Do a regex check, if a string.
if (is_string($amount)) {
if ( ! preg_match('/^[0-9][0-9,]*(\.[0-9]{0,' . $minor_unit . '})?$/', $amount)) {
throw new Exception\InvalidArgumentException("Invalid amount format '{$amount}'");
}

// Remove any comma thousands separators.
$amount = str_replace(',', '', $amount);
}

return (isset($minor_unit) ? number_format((float)$amount, $minor_unit, '.', '') : null);
}
}

0 comments on commit 2af0794

Please sign in to comment.