diff --git a/src/Adyen/Util/Util.php b/src/Adyen/Util/Util.php index 6401baaea..8915fa917 100644 --- a/src/Adyen/Util/Util.php +++ b/src/Adyen/Util/Util.php @@ -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, '', ''); + } } diff --git a/tests/ExceptionTest.php b/tests/ExceptionTest.php index 04f1e0b15..494caf630 100644 --- a/tests/ExceptionTest.php +++ b/tests/ExceptionTest.php @@ -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()); } } diff --git a/tests/Util/UtilTest.php b/tests/Util/UtilTest.php index 757d5e39b..4ad9e1f88 100644 --- a/tests/Util/UtilTest.php +++ b/tests/Util/UtilTest.php @@ -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); + } }