Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PW-1520: added formatAmount function and test #113

Merged
merged 3 commits into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, '', '');
}
}
7 changes: 7 additions & 0 deletions tests/Util/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,11 @@ public function testSha256() {
));
$this->assertEquals("YtbpYcrdbvk0RSVwTwENMzomS0LYtiItMwXhI5tohXs=", $signature);
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we add 3 test cases for the 3 different options ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}