From 4ea2df05eb482c0ebeb35c2412a5a47ee356ff8d Mon Sep 17 00:00:00 2001 From: Ram Prakash Singh Date: Mon, 18 Nov 2024 17:51:39 +0530 Subject: [PATCH 1/4] lower unit amount conversion based on currency passed --- src/Utility.php | 56 +++++++++++++++++++++++++++++++++ tests/AmountToLowerUnitTest.php | 47 +++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 tests/AmountToLowerUnitTest.php 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 From 00c8c50a0496465f480ccc04b34cc874d7442946 Mon Sep 17 00:00:00 2001 From: Ram Prakash Singh Date: Mon, 18 Nov 2024 18:11:08 +0530 Subject: [PATCH 2/4] lower unit amount conversion based on currency passed --- tests/CoverageTest.php | 6 ++++++ 1 file changed, 6 insertions(+) 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(); } /** From d3bc451dfd2c7358188830c7601b88e34552b9da Mon Sep 17 00:00:00 2001 From: Ram Prakash Singh Date: Mon, 18 Nov 2024 18:21:53 +0530 Subject: [PATCH 3/4] lower unit amount conversion based on currency passed --- tests/AmountToLowerUnitTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/AmountToLowerUnitTest.php b/tests/AmountToLowerUnitTest.php index 7941319b..6002105d 100644 --- a/tests/AmountToLowerUnitTest.php +++ b/tests/AmountToLowerUnitTest.php @@ -19,7 +19,7 @@ public function setUp(): void public function testTwoDecimalAmount() { $lowerUnitAmount = $this->api->utility->amountToLowerUnit(97.93, 'INR'); - + echo 'testTwoDecimalAmount:' . $lowerUnitAmount; $this->assertEquals(9793, $lowerUnitAmount); } @@ -29,7 +29,7 @@ public function testTwoDecimalAmount() public function testThreeDecimalAmount() { $lowerUnitAmount = $this->api->utility->amountToLowerUnit(97.937, 'KWD'); - + echo 'testTwoDecimalAmount:' . $lowerUnitAmount; $this->assertEquals(97937, $lowerUnitAmount); } @@ -39,7 +39,7 @@ public function testThreeDecimalAmount() public function testZeroDecimalAmount() { $lowerUnitAmount = $this->api->utility->amountToLowerUnit(973, 'JPY'); - + echo 'testTwoDecimalAmount:' . $lowerUnitAmount; $this->assertEquals(973, $lowerUnitAmount); } From 2b140fd11c77e299a0e7396c131563c3356fd72c Mon Sep 17 00:00:00 2001 From: Ram Prakash Singh Date: Mon, 18 Nov 2024 18:35:29 +0530 Subject: [PATCH 4/4] lower unit amount conversion based on currency passed --- tests/AmountToLowerUnitTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/AmountToLowerUnitTest.php b/tests/AmountToLowerUnitTest.php index 6002105d..7941319b 100644 --- a/tests/AmountToLowerUnitTest.php +++ b/tests/AmountToLowerUnitTest.php @@ -19,7 +19,7 @@ public function setUp(): void public function testTwoDecimalAmount() { $lowerUnitAmount = $this->api->utility->amountToLowerUnit(97.93, 'INR'); - echo 'testTwoDecimalAmount:' . $lowerUnitAmount; + $this->assertEquals(9793, $lowerUnitAmount); } @@ -29,7 +29,7 @@ public function testTwoDecimalAmount() public function testThreeDecimalAmount() { $lowerUnitAmount = $this->api->utility->amountToLowerUnit(97.937, 'KWD'); - echo 'testTwoDecimalAmount:' . $lowerUnitAmount; + $this->assertEquals(97937, $lowerUnitAmount); } @@ -39,7 +39,7 @@ public function testThreeDecimalAmount() public function testZeroDecimalAmount() { $lowerUnitAmount = $this->api->utility->amountToLowerUnit(973, 'JPY'); - echo 'testTwoDecimalAmount:' . $lowerUnitAmount; + $this->assertEquals(973, $lowerUnitAmount); }