diff --git a/src/Utility.php b/src/Utility.php index 867e9591..e166073f 100644 --- a/src/Utility.php +++ b/src/Utility.php @@ -2,6 +2,9 @@ namespace Razorpay\Api; +use Requests; +use WpOrg\Requests\Hooks; + class Utility { const SHA256 = 'sha256'; @@ -88,4 +91,57 @@ private function hashEquals($expectedSignature, $actualSignature) return false; } + + public function amountToLowerUnit($amount, $currency): int + { + $hooks = new Hooks(); + + $request = new Request(); + + $hooks->register('curl.before_send', array($request, 'setCurlSslOpts')); + + $options = array( + 'auth' => array(Api::getKey(), Api::getSecret()), + 'hook' => $hooks, + 'timeout' => 60 + ); + + $headers = []; + + $url = 'https://express.razorpay.com/v1/currency-list'; + + $response = Requests::request($url, $headers, [], 'GET', $options); + + if (file_exists(dirname(__FILE__) . '/rzp_currency_list.json') === true) + { + if (filemtime(dirname(__FILE__) . '/rzp_currency_list.json') < (time() - 24*60*60)) + { + file_put_contents(dirname(__FILE__) . '/rzp_currency_list.json', $response->body); + } + } + else + { + file_put_contents(dirname(__FILE__) . '/rzp_currency_list.json', $response->body); + } + + $currencyListJson = file_get_contents(dirname(__FILE__) . '/rzp_currency_list.json'); + + $currencyListArray = json_decode($currencyListJson, 1); + + $lowerAmount = null; + + if (array_key_exists($currency, $currencyListArray['currency_list'])) + { + $exponent = $currencyListArray['currency_list'][$currency]['Exponent']; + + $lowerAmount = (int) round($amount * pow(10, $exponent)); + } + + if (empty($lowerAmount)) + { + $lowerAmount = 0; + } + + return $lowerAmount; + } } diff --git a/tests/AmountToLowerUnitTest.php b/tests/AmountToLowerUnitTest.php new file mode 100644 index 00000000..7941319b --- /dev/null +++ b/tests/AmountToLowerUnitTest.php @@ -0,0 +1,47 @@ +api->utility->amountToLowerUnit(97.93, 'INR'); + + $this->assertEquals(9793, $lowerUnitAmount); + } + + /** + * Three Decimal Currency Amount Coversion + */ + public function testThreeDecimalAmount() + { + $lowerUnitAmount = $this->api->utility->amountToLowerUnit(97.937, 'KWD'); + + $this->assertEquals(97937, $lowerUnitAmount); + } + + /** + * Zero Decimal Currency Amount Coversion + */ + public function testZeroDecimalAmount() + { + $lowerUnitAmount = $this->api->utility->amountToLowerUnit(973, 'JPY'); + + $this->assertEquals(973, $lowerUnitAmount); + } + + +} \ No newline at end of file diff --git a/tests/CoverageTest.php b/tests/CoverageTest.php index 1467ca0b..e35b1572 100644 --- a/tests/CoverageTest.php +++ b/tests/CoverageTest.php @@ -384,6 +384,12 @@ public function testUtilityCoverage(){ $utility->testPaymentVerification(); $utility->testPaymentLinkVerification(); $utility->testSubscriptionVerification(); + + $currencyConversion = new AmountToLowerUnitTest(); + $currencyConversion->setup(); + $currencyConversion->testTwoDecimalAmount(); + $currencyConversion->testThreeDecimalAmount(); + $currencyConversion->testZeroDecimalAmount(); } /**