Skip to content

Commit

Permalink
Merge pull request #58 from razorpay/PO-143
Browse files Browse the repository at this point in the history
added support for multiple currencies
  • Loading branch information
rohitcbr authored Jul 11, 2024
2 parents 8447537 + 4bf92b0 commit a8774c4
Show file tree
Hide file tree
Showing 4 changed files with 628 additions and 12 deletions.
8 changes: 8 additions & 0 deletions includes/razorpay-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,12 @@ function displayPaymentAction()
{
$this->template->displayPaymentAction();
}

/**
* Currency field of settings page
**/
function displayCurrencyAction()
{
$this->template->displayCurrencyAction();
}
}
33 changes: 27 additions & 6 deletions razorpay-quick-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function __construct()
$this->keyID = get_option('key_id_field');
$this->keySecret = get_option('key_secret_field');
$this->paymentAction = get_option('payment_action_field');
$this->currencyAction = get_option('currency_action_field');

// The checkout function is released when the pay now button is clicked
$this->liveurl = 'https://checkout.razorpay.com/v1/checkout.js';
Expand Down Expand Up @@ -112,7 +113,7 @@ function generateRazorpayOrderForm()

$metadata = get_post_meta($pageID);

$amount = (int) (number_format($metadata['amount'][0] * 100, 0, ".", ""));
$amount = (int) (number_format($metadata['amount'][0] * pow(10, (int)$this->getCurrencyObject()['exponent']), 0, ".", ""));

if (isset($this->keyID) && isset($this->keySecret) && $amount!=null)
{
Expand Down Expand Up @@ -149,7 +150,7 @@ function razorpayOrderCreationResponse()
}
else
{
$amount = (int) (number_format($metadata['amount'][0] * 100, 0, ".", ""));
$amount = (int) (number_format($metadata['amount'][0] * pow(10, (int)$this->getCurrencyObject()['exponent']), 0, ".", ""));

$productInfo = $this->getProductDecription($metadata, $pageID);

Expand Down Expand Up @@ -189,7 +190,7 @@ function razorpayOrderCreationResponse()
'key' => $this->keyID,
'name' => $name,
'amount' => $amount,
'currency' => 'INR',
'currency' => $this->currencyAction,
'description' => $productInfo,
'order_id' => $razorpayOrder['id'],
'notes' => [
Expand Down Expand Up @@ -254,7 +255,7 @@ function getOrderCreationData($orderID, $amount)
$data = array(
'receipt' => $orderID,
'amount' => $amount,
'currency' => 'INR',
'currency' => $this->currencyAction,
'payment_capture' => ($this->paymentAction === 'authorize') ? 0 : 1
);

Expand All @@ -270,7 +271,7 @@ function wpCheckRazorpayResponse()

if (!empty($attributes))
{
$amount = $_SESSION['rzp_QP_amount'] / 100; // paise to rupees
$amount = $_SESSION['rzp_QP_amount'] / pow(10, (int)$this->getCurrencyObject()['exponent']); // paise to rupees

$api = new Api($this->keyID, $this->keySecret);

Expand All @@ -290,7 +291,7 @@ function wpCheckRazorpayResponse()
if ($success === true)
{
$this->message = "Thank you for shopping with us. Your account has been charged and your transaction is successful. We will be processing your order soon."
. "<br><br>" . "Transaction ID : " . esc_html($attributes['razorpay_payment_id']) . "<br><br>" . "Order Amount: $amount";
. "<br><br>" . "Transaction ID : " . esc_html($attributes['razorpay_payment_id']) . "<br><br>" . "Order Amount: " . get_option('currency_action_field') . " " . $amount;
}
else
{
Expand All @@ -302,6 +303,26 @@ function wpCheckRazorpayResponse()
session_write_close();
}

function getCurrencyObject()
{
$supported_currencies = json_decode(file_get_contents(__DIR__ . "/supported-currencies.json"), true)['supported-currencies'];

$currency_code = get_option('currency_action_field');

$currency_object = null;

foreach($supported_currencies as $supported_currency)
{
if ($supported_currency['iso_code'] === $currency_code)
{
$currency_object = $supported_currency;
break;
}
}

return $currency_object;
}

protected function getPostAttributes()
{
if (isset($_REQUEST['rzp_QP_form_submit']))
Expand Down
Loading

0 comments on commit a8774c4

Please sign in to comment.