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

Lower unit amount conversion based on currency passed #381

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
56 changes: 56 additions & 0 deletions src/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Razorpay\Api;

use Requests;
use WpOrg\Requests\Hooks;

class Utility
{
const SHA256 = 'sha256';
Expand Down Expand Up @@ -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;
}
}
47 changes: 47 additions & 0 deletions tests/AmountToLowerUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Razorpay\Tests;

use Razorpay\Api\Request;

class AmountToLowerUnitTest extends TestCase
{


public function setUp(): void
{
parent::setUp();
}

/**
* Two Decimal Currency Amount Coversion
*/
public function testTwoDecimalAmount()
{
$lowerUnitAmount = $this->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);
}


}
6 changes: 6 additions & 0 deletions tests/CoverageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,12 @@ public function testUtilityCoverage(){
$utility->testPaymentVerification();
$utility->testPaymentLinkVerification();
$utility->testSubscriptionVerification();

$currencyConversion = new AmountToLowerUnitTest();
$currencyConversion->setup();
$currencyConversion->testTwoDecimalAmount();
$currencyConversion->testThreeDecimalAmount();
$currencyConversion->testZeroDecimalAmount();
}

/**
Expand Down
Loading