Skip to content

Commit

Permalink
POC currency support
Browse files Browse the repository at this point in the history
  • Loading branch information
yashgit891 committed Sep 19, 2024
1 parent 1cb7781 commit 9fef3f0
Show file tree
Hide file tree
Showing 4 changed files with 798 additions and 7 deletions.
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

0 comments on commit 9fef3f0

Please sign in to comment.