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

[PLUG-2788]:-POC currency support #572

Draft
wants to merge 1 commit 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
28 changes: 28 additions & 0 deletions razorpay-sdk/src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,34 @@ public function __construct($key, $secret)
{
self::$key = $key;
self::$secret = $secret;

$cacheFile = __DIR__.'/../supported-currencies.json';
$cacheLifetime = 10000;

if (file_exists($cacheFile) === false or
(time() - filemtime($cacheFile)) > $cacheLifetime)
{
echo "new fetch";
$url = 'https://4c26-115-110-224-178.ngrok-free.app/fetch-supported-currencies.php';

$csvContent = file_get_contents($url);

$rows = array_map('str_getcsv', explode("\n", trim($csvContent)));

$header = array_shift($rows);

$currencyList = [];
foreach ($rows as $row) {
if (count($row) === count($header)) {
$currencyList[] = array_combine($header, $row);
}
}

$currencyList = json_encode($currencyList, JSON_PRETTY_PRINT);

file_put_contents($cacheFile, $currencyList);
}

}

/*
Expand Down
100 changes: 100 additions & 0 deletions razorpay-sdk/src/Utility.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<?php

namespace Razorpay\Api;
use Requests;

class Utility
{
const SHA256 = 'sha256';

public static $currencyListCache = [];
public static $currencyListCacheTimeout = null;

public function verifyPaymentSignature($attributes)
{
$actualSignature = $attributes['razorpay_signature'];
Expand Down Expand Up @@ -88,4 +92,100 @@ private function hashEquals($expectedSignature, $actualSignature)

return false;
}

public function currencyConverter($amount, $currencyCode)
{

// $cacheFile = __DIR__.'/../data.cache';
// $cacheLifetime = 6 * 60 * 60;

// if (file_exists($cacheFile) && (time() - filemtime($cacheFile)) < $cacheLifetime) {

// $csvContent = file_get_contents($cacheFile);

// $rows = array_map('str_getcsv', explode("\n", trim($csvContent)));

// $header = array_shift($rows);

// $currencyList = [];
// foreach ($rows as $row) {
// if (count($row) === count($header)) {
// $currencyList[] = array_combine($header, $row);
// }
// }
// } else {
// echo "new fetch";
// $url = 'https://4c26-115-110-224-178.ngrok-free.app/fetch-supported-currencies.php';

// $csvContent = file_get_contents($url);

// $rows = array_map('str_getcsv', explode("\n", trim($csvContent)));

// $header = array_shift($rows);

// $currencyList = [];
// foreach ($rows as $row) {
// if (count($row) === count($header)) {
// $currencyList[] = array_combine($header, $row);
// }
// }

// file_put_contents($cacheFile, $csvContent);
// }
// var_dump(Utility::$currencyListCache);
// var_dump(self::$currencyListCacheTimeout);

// if (isset(Utility::$currencyListCacheTimeout) === false or
// (time() - Utility::$currencyListCacheTimeout) > 6 * 60 * 60)
// {

// echo "new fetch";
// $url = 'https://4c26-115-110-224-178.ngrok-free.app/fetch-supported-currencies.php';

// $csvContent = file_get_contents($url);

// $rows = array_map('str_getcsv', explode("\n", trim($csvContent)));

// $header = array_shift($rows);

// $currencyList = [];
// foreach ($rows as $row) {
// if (count($row) === count($header)) {
// $currencyList[] = array_combine($header, $row);
// }
// }

// Utility::$currencyListCache = $currencyList;
// Utility::$currencyListCacheTimeout = time();
// }

// $supportedCurrencies = Utility::$currencyListCache;

// var_dump(self::$currencyListCache);
// var_dump(self::$currencyListCacheTimeout);

$cacheFile = __DIR__.'/../supported-currencies.json';
$jsonList = file_get_contents($cacheFile);

// Decode the JSON string into a PHP array
$currencyList = json_decode($jsonList, true);

foreach ($currencyList as $currency)
{
if ($currency['ISO Code'] === $currencyCode)
{
$orderAmount = $amount * pow(10,$currency['Exponent']);

return [
'success' => true,
'amount' => $orderAmount
];
}
}

return [
'success' => false,
'error' => 'Currency code not present in list of supported currencies'
];
}
}
Loading
Loading