Skip to content

Commit

Permalink
Merge pull request #113 from Adyen/PW-1520
Browse files Browse the repository at this point in the history
PW-1520: added formatAmount function and test
  • Loading branch information
Aleffio committed Jul 19, 2019
2 parents 62e20f2 + a9e2c7e commit 8a78d9b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
42 changes: 42 additions & 0 deletions src/Adyen/Util/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,46 @@ public static function calculateSha256Signature($hmacKey, $params)
$merchantSig = base64_encode(hash_hmac('sha256', $signData, pack("H*", $hmacKey), true));
return $merchantSig;
}

/**
* Return the formatted currency. Adyen accepts the currency in multiple formats.
* @param $amount
* @param $currency
* @return int
*/
public static function formatAmount($amount, $currency)
{
switch ($currency) {
case "CVE":
case "DJF":
case "GNF":
case "IDR":
case "JPY":
case "KMF":
case "KRW":
case "PYG":
case "RWF":
case "UGX":
case "VND":
case "VUV":
case "XAF":
case "XOF":
case "XPF":
$format = 0;
break;
case "BHD":
case "IQD":
case "JOD":
case "KWD":
case "LYD":
case "OMR":
case "TND":
$format = 3;
break;
default:
$format = 2;
}

return (int)number_format($amount, $format, '', '');
}
}
8 changes: 4 additions & 4 deletions tests/ExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ public function testExceptionMissingUsernamePassword()
$e = null;
try {
$result = $service->listRecurringDetails($params);
} catch (\Exception $e){

} catch (\Exception $e) {
}

// check if exception is correct
$this->assertEquals('Adyen\ConnectionException', get_class($e));
$this->assertEquals("Probably your Web Service username and/or password is incorrect\n(Network error [errno 0]: )", $e->getMessage());
$this->assertEquals('Adyen\AdyenException', get_class($e));
$this->assertEquals("HTTP Status Response - Unauthorized", $e->getMessage());
$this->assertEquals('0', $e->getCode());
$this->assertEquals('401', $e->getStatus());
}
}
21 changes: 21 additions & 0 deletions tests/Util/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,25 @@ public function testSha256() {
));
$this->assertEquals("YtbpYcrdbvk0RSVwTwENMzomS0LYtiItMwXhI5tohXs=", $signature);
}

public function testFormatAmountThreeDecimals() {
$amount = 15.021;
$currency = "TND";
$formattedAmount = Util::formatAmount($amount, $currency);
$this->assertEquals(15021, $formattedAmount);
}

public function testFormatAmountTwoDecimals() {
$amount = 15.02;
$currency = "EUR";
$formattedAmount = Util::formatAmount($amount, $currency);
$this->assertEquals(1502, $formattedAmount);
}

public function testFormatAmountZeroDecimals() {
$amount = 15021;
$currency = "IDR";
$formattedAmount = Util::formatAmount($amount, $currency);
$this->assertEquals(15021, $formattedAmount);
}
}

0 comments on commit 8a78d9b

Please sign in to comment.